4

How is the following for loop different from original?
Why after initialization, it checks for the condition?

Why such behavior and what are other such tricky behaviors of for loop?

class FooBar
{
static public void main(String... args)
{
    for (int x = 1; ++x < 10;)
    {
        System.out.println(x);  // it starts printing from 2

    }
}
}

Output:
2
3
4
5
6
7
8
9

Shubham Kharde
  • 228
  • 2
  • 10

8 Answers8

3

++x increments the value of x. As a result, two things are happening:

  1. The value being checked for the for-loop condition (i.e., the inequality), is already incremented, and
  2. This incremented value is what gets printed within the loop.

If you want values from 1 to 9, use the standard loop:

for (int x = 1; x < 10; ++x) { ... }

The effects of prefix and postfix operators has been discussed and explained in detail elsewhere, this post, for example.

Community
  • 1
  • 1
Chthonic Project
  • 8,216
  • 1
  • 43
  • 92
2

The increment of x is combined with the condition. Normally, the increment is performed in the third section of the for loop, which is a statement executed at the end of an iteration.

However, the condition is checked at the beginning of the loop. It's an expression, not a statement, but as you can see, that doesn't stop someone from inserting a side-effect -- the pre-increment of x.

Because x starts out as 1, the condition increments it to 2 before the first iteration.

rgettman
  • 176,041
  • 30
  • 275
  • 357
1

This is not for-loop trickery, it's the way the prefix and postfix operator works that you're using on x.

++x - Increment x then evaluate its value

x++ - Evaluate the value of x and then increment it.

So, the loop with this definition:

for (int x = 1; ++x < 10;)

executes like this:

  1. Initialize x to 1
  2. Increment x by 1
  3. Evaluate if x < 10. If yes, iterate loop. If no, end loop.

What you want is this:

for (int x = 1; x < 10; x++)
  1. Initialize x to 1
  2. Evaluate if x < 10. If yes, iterate loop. If no, end loop.
  3. Increment x by 1
sma
  • 9,449
  • 8
  • 51
  • 80
1

Each for-loop (except enhanced for loop) has the following structure -

for(initialization; conditionChecking; increment/decrement){

   //body

}

Your for-loop also has this structure with the incremetn/decrement portion empty. But to continue the for loop you have to change index (here x) of for-loop. If it is done some how then you can leave the increment/decrement portion empty. If you write the for-loop like this then it also fine -

for(int x=0; x<10; ){
   //do something
   x++;
}  

You also may leave one/more portion (even the body) of a for-loop empty, like this -

int x=0;
for(;x<10;x++); //empty body; do nothing; just increment x
Razib
  • 10,965
  • 11
  • 53
  • 80
1

A for loop is valid with one or more of it's some portion leaving empty. In the your code the increment portion is empty. You have done your increment of index x by using ++x>10. So it is valid. There is no difference from the regular for loop, it's just a way of representing the for loop in different way.

KajolK
  • 203
  • 1
  • 3
  • 9
1

The Original way of a for loop you may have in you mind:

for(initialization; condition; inc/dec/divide..)

And what you mentioned: for (initialization; Inc & check; nothing)

What does it mean? : for(some blaBla; any Bla; some, more, bla)

You may have any expression any where. The Original version is just a convention we follow. Go ahead an write a printing statement or a bitwise operator instead of some blaBla try it out!

Update:

Why :

  for (int x = 1; x++ < 10;)
      {
           System.out.println(x);

      } 

Prints 2..10? For the answer first try to execute :

   for (int x = 1; ++x < 10;)
          {
               System.out.println(x);

          } 

It prints from 2..9

You should understand how pre/post fix operators work. x++ in the loop increments the value but not immediately. It increments after x++ < 10; Where as ++x < 10; first increments and then rest everything happens.

So, when the x value is 9, 9(++)< 10 -> 9 is less ? yeah, now 9++ occurs.It then increments x. but ++x increments 1st so ++9< 10 -> 10<10 NO.

joey rohan
  • 3,505
  • 5
  • 33
  • 70
1

You can write your code as:

x = 1;
while (++x < 10)
{
    System.out.println(x);
}

You expect the following:

x = 1;
do
{
    System.out.println(x);
}
while (++x < 10);

This is the difference.

The following does what you expect:

for (int x = 1; x < 10; x++)
{
    System.out.println(x);
}
Konrad
  • 355
  • 4
  • 16
0

TL;DR of it all is that ++x is a pre increment operator. When you use ++x, x will be incremented before any other allied operation. That is why your code prints out 2 first instead of 1 and stops when ++x becomes 10

Shreyas Chavan
  • 1,079
  • 1
  • 7
  • 17