Computer Applications
Explain for loop with an example.
Java Iterative Stmts
ICSE 2005
78 Likes
Answer
for loop is an entry-controlled loop. Below is an example of for loop:
for (int i = 1; i <= 12; i++) {
int a = 2 * i;
System.out.println("2 x " + i + "\t= " + a);
}
This for loop prints the table of 2 till 12. int i = 1
is the initialization part of the for loop, it is executed only once when the loop gets executed for the first time. i <= 12
is the condition part of the for loop, it is executed before the start of each iteration. Loop iterates as long as this condition remains true. Once it becomes false, execution of the loop is stopped. i++
is the update part of the for loop. It is executed at the end of each iteration.
Answered By
56 Likes