for loops in java and values of i

for loop java example
java while loop example
java loop programs for practice
for each loop java
java for loop array
for loop programs in java with output
enhanced for loop in java example
java for loop list

In Horstmann's textbook(Big Java Late Objects) on java, for a for loop such as :

for (i = 0; i <= 5; i++)

Horstmann says that values of i for this for loop is 0 1 2 3 4 5 .

However, it seems to me that the value of i should end at 6 since the loop is entered when the i has the value 5 . What am I not understanding about this loops could someone explain me?

(PS. I am sorry if the question is too basic and thus not allowed in this platform.)

edit about the duplicate suggestion: My question is not a duplicate of the suggested link because the suggested link is about the execution of the for loop, mine is about the updating of the variable i , after the end of the execution. I understand that if I would add a System.out.print(i) statement the output will be 0 1 2 3 4 5 because i gets updated after the execution statement (in this case the print statement)

You are correct that the value of i will be 6 after the loop has terminated, but perhaps Horstmann meant the values of i inside the loop?

int i;
for (i = 0; i <= 5; i++) {
    System.out.println("Value of i IN loop: " + i);
}
System.out.println("Value of i AFTER loop: " + i);

Output:

Value of i IN loop: 0
Value of i IN loop: 1
Value of i IN loop: 2
Value of i IN loop: 3
Value of i IN loop: 4
Value of i IN loop: 5
Value of i AFTER loop: 6

And it is customary to declare the "counter" for the loop in the loop unless the value when terminating is needed afterwards.

for (int i = 0; i <= 5; i++) {
}
// i is not available here

For loop in Java with example, is satisfied. In Java we have three types of basic loops: for, while. Syntax of for loop: for(initialization; condition ; increment/decrement) { statement(s); }  In addition to changing the value of i, they also return the value of i, either before adding one (i++) or after adding one (++i). In a loop the third component is a piece of code that is executed after each iteration. for (int i=0; i<10; i++) The value of that part is not used, so the above is just the same as. for(int i=0; i<10; i = i+1) or

Here is the anatomy of a for loop in Java (similar applies to C/C++ and a few other languages as well)

for (int i=0; i <= 5; ++i)

int i=0     initial condition; happens before the loop starts
i <= 5      check is performed BEFORE each iteration of the loop
++i         loop variable is incremented AFTER each iteration

So, your for loop would iterate 5 times, and at the end of the fifth iteration, i would be incremented to 6. At that point, the i <= 5 check would happen one last time, and it would fail, since 6 is greater than 5.

To convince yourself of all this, run the following Java code:

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

You will see that the value of i after the loop in fact is 6.

The for Statement (The Java™ Tutorials > Learning the Java , The Java Tutorials have been written for JDK 8. Examples and The for statement provides a compact way to iterate over a range of values. Programmers often  The Java for loop is a control flow statement that iterates a part of the programs multiple times. The Java while loop is a control flow statement that executes a part of the programs repeatedly on the basis of given boolean condition. The Java do while loop is a control flow statement that executes a part of the programs at least once and the further execution depends upon the given boolean condition.

This loop

for ( i = 0; i <=5; i ++)

is like

int i = 0;

while (i <= 5){  // Exits when i > 5

   ...

   i++;
} // goes back to while-loop check

java for complete beginners - for loops, You saw in the last section that one way to "tell" Java not to execute every line is by using IF Statement to section off areas of code. Another way to interrupt the  Sum = 500500. Here, we have a variable named sum. Its initial value is 0. Inside for loop, we have initialized a variable named i with value 1. In each iteration of for loop, the sum variable is assigned value: sum + i. the value of i is increased by 1. The loop continues until the value of i is greater then 1000.

The value of I will end at 5, Because you are doing i <= 5 It checks if I is greater or equal to 5 and then ends the loop. hence making it end at 5 and not 6.

Loops in Java, It must return a boolean value. It is also an Entry Control Loop as the condition is checked prior to the execution of the loop statements. Statement execution: Once​  The For Loop. The for loop has the following syntax: for ( statement 1; statement 2; statement 3) {. // code block to be executed. } Statement 1 is executed (one time) before the execution of the code block. Statement 2 defines the condition for executing the code block.

Well the man is right! it iterates from 0 to 5, because... 1. the condition is „i <= 5" 2. Java is 0 indexed, or to be exact by declaring and initialising the variable i = 0, it will start iterating from 0 until the condition returns false. The decisive point here is the conditional pressure inside the for-loop You understand?

Java For Loop, Statement 3 increases a value (i++) each time the code block in the loop has been executed. Another Example. This example will only print even values between 0  The Java Loop: for. The working process of a for loop is similar to the while loop, only the structure is different. Unlike the while loop, the layout of the control variable, loop condition, and the increment of the control statement can be stated in a single line of code, such as in the following example.

Java For Loop Examples, However: This for-loop example scans each element in the values array. It stops (​breaks) when a negative one element is found. Java program that uses break  For loop in Java with example. Loops are used to execute a set of statements repeatedly until a particular condition is satisfied. In Java we have three types of basic loops: for, while and do-while. In this tutorial we will learn how to use “ for loop ” in Java.

Loops in Java, It must return boolean value either true or false. It is an optional condition. Statement: The statement of the loop is executed each time until the second condition is  Loops in Java Looping in programming languages is a feature which facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true. Java provides three ways for executing the loops.

Java for Loop (With Examples), Here, initially, the value of i is 1. So the test expression evaluates to true for the first time. Hence, the print statement is executed. Now the update expression is  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) }

Comments
  • Possible duplicate of How does a for loop check its conditions in Java?
  • Yes, but the accepted answer in the duplicate also states that the update of i is executed after the execution of the body (before the termination condition). The author of that question originally asked why the loop printed i=5 when the condition was set to i<5 (which was because the author had combined the print statement with the increment part.
  • That is a compelling answer, and I am willing to delete this question, due to it being a duplicate. However I also think that I should not because @Roger Lindsjö's answer is more illuminating the ones that were given as an answer to the suggested post. In that case it appears to me as though there is a tie, and I am willing to act in accordance to a tie breaking suggestion .
  • I think you should consider this the difference between what is observable (highest value of i seen inside the loop body is 5) and what happens 'under the hood' (i becomes 6 so the loop terminates)
  • This makes a lot of sense, afterall it reminded me that the i variable only exists inside the foor loop and "dies" when the loop is no longer executed.
  • This is more detailed answer then mine, Nice job