What is the for loop?
What is the for loop?
Rating:
The for loop is the most versatile looping construct. It is used to continuously execute a block of code until a particular condition is satisfied. It comprises three parts: initialization, condition, and iteration.
The initialization portion is generally an expression that sets the value of the loop control variable. The loop control variable acts as a counter and controls the execution of the loop. The initialization portion executes only once. The condition portion must be a boolean expression. It usually tests the loop control variable against a target value and hence works as a loop terminator. The iteration portion is usually an expression that increments or decrements the loop control variable.
The general form of the for loop is:
//body of the loop
}
All the three components, i.e., initialization, condition, and iteration are optional. In case there is only a single statement in the body of the loop, the curly braces can be omitted.
The for loop executes in the following three steps:
- When the loop first starts, the initialization expression is executed and then the control is transferred to step 2.
- The condition is evaluated. If the condition evaluates to true, the body of the loop executes and the program control is transferred to step 3. If the condition evaluates to false, the loop terminates.
- The iteration expression executes and then the control is transferred to step 2.
Rating:
Other articles
- What is CDC in J2ME?
- What is the HttpConnection interface?
- What is the playTone() method?
- What are the differences between stateless and stateful session beans?
- What is multithreading?