What is the for loop?

December 30th, 2007 by uCertify Leave a reply »

The for loop is used to execute a statement or a series of statements inside the loop block as long as a particular condition remains true. It comprises three parts: initialization, condition, and increment.

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 tests before the statement inside the loop block is executed. If the condition is true, the statement is executed. But if the condition is false, the statement is skipped and the next statement following the for loop will be executed.

The increment portion is usually an expression that increments or decrements the loop control variable.

The syntax of the for loop is as follows:

for (initialization; condition; increment) {
statement(s);
}

The loop performs the following 4 steps until the condition returns false:

  1. When the loop starts, the initialization expression is executed.
  2. The condition is evaluated. If condition returns true, go to step 3. Otherwise, execute the next statement following the for loop.
  3. Run the statement inside the loop block.
  4. The increment expression executes and then go back to step 2.

In the following example, the for loop executes till the value of the variable rs is less than 20.

for(rs=1, x=20; rs<20; rs++){
trace(”I have Rs. “+x+” in my pocket. I can expend Rs. “+rs+” of them.”);
}

Click here to get free 9A0-058 Adobe Flash CS3 ACE exam practice questions. Best exam simulation Adobe 9A0-311 download free trial.
Like this article? Share it with others
If you like this article, please leave a comment or subscribe this blog via RSS or via e-mail, Bookmark and share through your network. Click the AddThis button below. Thanks.
  • Share/Bookmark
Advertisement

Leave a Reply

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