//for loop:
- //We can initialize the variable, check condition and increment/decrement value.
- //It consists of four parts:
- //1.Initialization:
- //It is the initial condition which is executed once when the loop starts.
- //Here, we can initialize the variable, or we can use an already initialized variable. It is an optional condition.
- //2.Condition:
- //It is the second condition which is executed each time to test the condition of the loop.
- //It continues execution until the condition is false.
- //It must return boolean value either true or false. It is an optional condition.
- //3.Increment/Decrement:
- //It increments or decrements the variable value. It is an optional condition.
- //4.Statement:
- //The statement of the loop is executed each time until the second condition is false.
//syntax:
for (initialization;
condition; updation)
{
//Body of the Loop
// statements we want to execute
}
//How does a For loop execute?
- //1.Initialization is done
- //2.The flow jumps to Condition
- //3.Condition is tested.
- //i.If Condition is true, the flow goes into the Body
- //ii.If Condition is 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.
//Flowchart:-
=========================================================================
//Program
I:-
package loops_in_java;
public class ForLoop
{
public static void main(String[] args)
{
for (int a=2; a<=20; a=a+2)
{
System.out.println(a); //table
of 2
}
}
}
2
4
6
8
10
12
14
16
18
20
=========================================================================
0 Comments