A for loop is a control statement using which the programmer can give instructions to the computer to execute a set of statement repeatedly as long as specified condition is satisfied. It is required to specify how many times a set of statements have to be executed, it is also called counter controlled loop.
Syntex :-
for(initialization; condition; increment/decrement){
//statement or code to be executed
}
Q.) Program to print a text 5 times ?
class Main {
public static void main(String[] args) {
int n = 5;
// for loop
for (int i = 1; i <= n; ++i) {
System.out.println("Hello World");
}
}
}
Output
Hello World Hello World Hello World Hello World Hello World