English

Explain the working of the FOR statement with the help of an example program.

Advertisements

Question

Explain the working of the FOR statement with the help of an example program.

Explain
Advertisements

Solution

Working of the for Statement:

  1. Initialization: The loop control variable is initialized with a starting value. This step is executed only once.
  2. Condition: The specified condition is checked before each iteration. If the condition is true, the loop body is executed.
  3. Execution: The statements inside the for loop are executed when the condition is true.
  4. Updation: After each iteration, the loop control variable is updated, and the condition is checked again.
  5. 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
shaalaa.com
  Is there an error in this question or solution?
Chapter 7: Statements and their Scope - EXERCISE [Page 163]

APPEARS IN

Dheeraj Mehrotra Computer Science [English] Class 12
Chapter 7 Statements and their Scope
EXERCISE | Q 5. | Page 163
Advertisements
Share
Notifications



      Forgot password?
Use app×