Loops in Java

Following are three important statements to loop a set of statements in Java. 1) For 2) While 3) Do.. While

For Statement

Syntax --> for(initialization; conditon ; increment) { statements; } Output i value is 1 i value is 2 End of the for loop. EXPALANATION First Iteration When for loop started executing, it set the variable i to 1 and check the condition i < 3 ( in this case it is 1 < 3 , which is true). Since the condition become true. statements inside the for loop will get executed and println will display string "i value is 1". Second Iternation Control again comes to for statement and i value get incremented by i++ statement by 1 ( 1 to 2) and check the condition i < 3 ( in this case i vlaue is 2) 2 < 3 which is true. Since the condition becomes true, statements inside the for loop will get executed and println will display string "i value is 2". Third Iternation Control again goes to for statement and i value get incremented by i++ statement by 1 (2 to 3) and check the condition i < 3 (in this iteration i vlaud is 3) 3 < 3 which is false. Since the condition is false, control will move to next statement followed by for loop and "End of the for loop" string will get displayed.