Explain the working of the FOR statement with the help of an example program.
Advertisements
प्रश्न
Explain the working of the FOR statement with the help of an example program.
स्पष्ट करा
Advertisements
उत्तर
Working of the for Statement:
- Initialization: The loop control variable is initialized with a starting value. This step is executed only once.
- Condition: The specified condition is checked before each iteration. If the condition is
true, the loop body is executed. - Execution: The statements inside the
forloop are executed when the condition is true. - Updation: After each iteration, the loop control variable is updated, and the condition is checked again.
- Termination: When the condition becomes
false, the loop terminates, and control passes to the next statement.
Example Program:
class ForExample
{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
System.out.println(i);
}
}
}
Output:
1
2
3
4
5
2
3
4
5
shaalaa.com
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
