How does a For loop work?

  1. Control falls into the for loop. Initialization is done
  2. The flow jumps to Condition
  3. Condition is tested. 
    • If the Condition yields true, the flow goes into the Body
    • If the Condition yields false, the flow goes outside the loop
  4. The statements inside the body of the loop get executed.
  5. The flow goes to the Updation
  6. Updation takes place and the flow goes to Step 3 again
  7. The for loop has ended and the flow has gone outside.

For Loop in Java

Loops in Java come into use when we need to repeatedly execute a block of statements. Java for loop provides a concise way of writing the loop structure. The for statement consumes the initialization, condition, and increment/decrement in one line thereby providing a shorter, easy-to-debug structure of looping. Let us understand Java for loop with Examples.

 

Syntax:  

for (initialization expr; test expr; update exp)
{
// body of the loop
// statements we want to execute
}

Similar Reads

Parts of Java For Loop

Java for loop is divided into various parts as mentioned below:...

How does a For loop work?

Control falls into the for loop. Initialization is done The flow jumps to Condition Condition is tested.  If the Condition yields true, the flow goes into the Body If the Condition yields false, the flow goes outside the loop The statements inside the body of the loop get executed. The flow goes to the Updation Updation takes place and the flow goes to Step 3 again The for loop has ended and the flow has gone outside....

Flow Chart For “for loop in Java”

Flow chart for loop in Java...

Examples of Java For loop

Example 1: (This program will print 1 to 10)...

Nested For Loop in Java

...

Java For-Each Loop

...

Java Infinite for Loop

...

FAQs for Java for Loop

Java Nested For Loop is a concept of using a for loop inside another for loop(Similar to that of using nested if-else). Let us understand this with an example mentioned below:...