-3

This is a very simple for loop:

for(int i=0;i<=100;i++)
{
    System.out.println(i);
}

I know how it mostly works, but I don't understand how the i++ works at the end: its supposed to add 1, if I'm correct, but when it prints out the i, it prints out 0 and then 1.

Why doesn't it just start out with 1 because of the i++? Why does it still just print out the original value instead of the i++ value?

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Boy Boy
  • 33
  • 5
  • 1
    The for-loop always starts with the value you initialized with(int i=0;) then checks the condition(i<=100;)-->then enters the loop-->executes the available statements-->then increments the value of variable(i++;) and then repeats the previous steps. – Amitesh Rai Aug 17 '14 at 02:41

6 Answers6

7

A for loop works as follows:

  1. Initialization is done (int i=0 in your case; only executed once)
  2. Condition is checked (i<=100 here), if condition is false leave the loop
  3. Code within the braces is executed (System.out.println(i); in your case)
  4. Update statement is executed (i++)
  5. Goto 2.
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
MrTux
  • 32,350
  • 30
  • 109
  • 146
2

It's similar to this while loop :

{
    int i = 0;
    while (i <= 100) {
        System.out.println(i);
        i++;
    }
}

i is incremented only at the end of each iteration.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • 4
    (Caveats: Except that `i` is bound to the scope of loop. `continue` wouldn't have the same effect in both loops; it would skip over `i++` in the `while` case.) – cdhowie Aug 17 '14 at 02:19
  • @cdhowie That's a good point. – Eran Aug 17 '14 at 02:19
2

Because the increment is evaluated after the first execution of the loop body. This is by design, and remember that programmers generally treat 0 as the first number. For example, with arrays and String(s) the first element is 0.

The Java Tutorial on The for Statement says,

The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the "for loop" because of the way in which it repeatedly loops until a particular condition is satisfied. The general form of the for statement can be expressed as follows:

for (initialization; termination; increment) {
  statement(s)
}

When using this version of the for statement, keep in mind that:

The initialization expression initializes the loop; it's executed once, as the loop begins. When the termination expression evaluates to false, the loop terminates. The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

Here it is how it works

enter image description here

The for statement is used to repeat a block of statements enclosed in curly braces. An increment counter is usually used to increment and terminate the loop. The for statement is useful for any repetitive operation, and is often used in combination with arrays to operate on collections of data/pins.

There are three parts to the for loop header:

for (initialization; condition; increment / decrement) {

//statement(s);

}

The initialization happens first and exactly once. Each time through the loop, the condition is tested; if it's true, the statement block, and the increment is executed, then the condition is tested again. When the condition becomes false, the loop ends.

Credits : For

Parimal Raj
  • 20,189
  • 9
  • 73
  • 110
-1

If you use ++ operator as prefix like: ++var; then, the value of operand is increased by 1 then, only it is returned but, if you use ++ as postfix like: var++; then, the value of operand is returned first then, only it is increased by 1.

For example,

class Example
{
    public static void main(String[] args)
    {
        int var = 1;    
        System.out.println(var++);
        System.out.println("\n" + ++var);    
    }
}

the following program prints

1
3

In the prefix form, the increment or decrement takes place before the value is used in expression evaluation, so the value of the expression is different from the value of the operand. In the postfix form, the increment or decrement takes place after the value is used in expression evaluation, so the value of the expression is the same as the value of the operand.

-1

Think of the i++ as the statement that says "Use then increment"
The value of i will first be used in the loop and then incremented by 1. So the loop is executed once, then the declaration and incremental statements checked.

shryesh.k
  • 51
  • 1
  • 7