FORLOOPS
for (int i = 10; i <= 20 ; ++i){
System.out.println (i) ;
}This iterates until a particular condition is satisfied.
The number of times the code block is repeated is controlled by a logical expression .
Lets look at the syntax of the for loop
for ( initialExpression ; condition ; updateExpression ){
// body of the loop
}Three parts are seperated by semicolons .A variable declared in a for loop can’t be used beyond the for loop . The code block will continue to be repeated as long as the condition is true, and it will stop as soon as the condition becomes false.
Lets talk about Initialization Section
This part is executed only once, at the beginning of the loop . It usually declares and initializes a loop control variable . As a example => int i = 0
Next what is the Condition Section ?
The condition expression (Boolean) is checked before each iteration. If it evaluates to true, the loop body is executed. If it false, the loop terminates. (or will not start at beginning)
Get Swapna Munasinghe’s stories in your inbox
Join Medium for free to get updates from this writer.
As examples,
for ( int i = 1; i <= 5; ++i){
// print;
}1 < 5 This is true, so the loop body will be executed.
for (int i = 10, i <= 5 ; ++i ){
//print ;
} 10 > 5 this is a false so the loop will be terminated.
At last we are going to talk about Update Expression.
This part is executed after each iteration of the loop body. It usually increments or decrements the loop control variable.
In Initialization section we can declare more than one variable of the same type . They must separate with commas.
for ( int x = 5, y = 3; y>3 ; y++){
//codes
}
In Condition Expression we can have only one condition expression .
In Update Expression we can update or change more than one variable.
for (int x = 10 ; y => 3; y++,x--){
//code
} A for loop which have a condition never evaluates to false it called Infinite Loop.
for (int i = 20 ; i <= 45 ;++i){
//codes
}As a beginner of the software field this is my blog about for loops. Lets talk further about infinite loop, while loop , break statement and continue statement via another blog.






