What is the for loop?

Are you preparing for IT certification? With practice questions, study notes, interactive quizzes, tips and technical articles, uCertify PrepKits ensure that you get a solid grasp of core technical concepts to ace your certification exam in first attempt.

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:

for(initialization; condition; iteration){
//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:

  1. When the loop first starts, the initialization expression is executed and then the control is transferred to step 2.

  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.

  3. The iteration expression executes and then the control is transferred to step 2.


Rating:



Other articles

Click here to Article home

 
uCertify.com | Our Company | Articles | Privacy | Security | Contact Us | News and Press Release | uCertify India
MCSE: MCSA, MCTS, MCITP    JAVA Certification: SCJP, SCWCD Cisco Certification: CCNA, CCENT, A+, Network+, Security+
Oracle Certification: OCP 9i, OCP 10g, OCA 9i, OCA 10g CIW foundation    EC-212-32    CISSP    Photoshop ACE    Adobe Flash ACE
© 2008 uCertify.com. All rights reserved. All trademarks are the property of their respective owners.