save up to 40%

Loop Statements

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.

Loop Statements

Rating:

Loops are the statements that are used to run a set of statements several times until a condition is true. These statements can also be used to extract the elements of an array. A basic loop statement has three parts: the condition, the list of statements to be repeated, and a counter update that controls how many times the loop executes.

There are four types of loops, for loop, for..in loop, while loop and do..while loop.

The for loop: 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 for loop is created by using the for keyword. The syntax of using the for loop is as follows:

for(initialization; condition; increment/decrement)
{
   //statements
}


In the above syntax, the initialization refers to the value that starts the loop. The condition is the conditional statement that is used to control the loop. If the condition is true, the loop continues. If the condition is false, the loop is terminated and the control passes to the next statement that follows the loop block. The increment or decrement is used to run the loop in forward or backward direction.

The for loop performs the following four 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.

The following code will demonstrate the use of the for loop:

var a;
for(a=1; a<=10; a++)
{
   trace(a);
}


The above code will display a series from 1 to 10 on the output window.

The for in loop: The for in loop is the variation of the for loop. It is used to extract the properties of an object and elements of an array.

The syntax of using the for in loop is as follows:

for(counter in object)
{
   //statements
}


The following code will demonstrate the use of for in the loop:

var obj= new Object();
obj.name="Jason";
var result="";
var I
for(i in obj)
{
   result+=obj.name+"."+i+"="+obj[i]+ "<br>";
}
trace(result);


The above code will display "Jason.name=Jason<br>" on the output window.

The while loop: The while loop is used to run a statement or a series of statements as long as a condition remains true. The loop tests the condition before the statement is executed. If the condition is true, the statement is executed. But if the condition is false, the statement is skipped and the statements following the end of the while loop is executed.

In this loop the variable needs to be initialized before the start of the loop, and the variable increments each time the statement is executed. When the value of the variable reaches a specified value, the condition returns false and the loop ends.

The syntax of the while loop is as follows:

while(condition)
{
   //statements
}


The loop performs the following steps:

  1. Evaluate the condition.

  2. If condition returns true, go to step 3. Otherwise, execute the statements following the end of the while loop.

  3. Execute the loop statement.

  4. Go back to step 1.

The following example will demonstrate the use of the while loop:

var num=0;
while(num<=10)
{
   trace(num);
}


The above statement will display a series from 1 to 10 on the output window.

The do-while loop: The do-while loop is a statement that is used to run a statement or a set of statements several times until a condition is met. The do-while loop starts before checking the condition. After its first execution, it checks the condition and runs the loop until the condition is true. If the condition becomes false, the loop is terminated and the control goes to the next statement that follows. The statement ensures that the statements are executed at least once.

In the do while loop, the variable needs to be initialized and increments each time the statements are executed. When the value of variables reaches a specified value, the condition returns false and the loop ends.

The syntax of the do-while loop is as follows:

do
{
    //statements
} while(condition)


The loop performs the following four steps:

  1. Execute the statements inside the loop.

  2. Evaluate the condition.

  3. If the condition returns true, go to step 4. Otherwise, execute the next line following the end of the do while loop.

  4. Go back to step 1.

The following example will demonstrate the use of the do-while loop:

var num=0;
do
{
   trace(num);
}while(num<=10)


The above statement will display a series from 1 to 10 on the output window.

The break statement: The break statement is used to terminate a loop according to the given condition inside the loop. To stop a loop statement at a point, the break keyword is placed at that point. The loop statement continues to run until it reaches the point where the interpreter hits the break keyword. It skips the rest of the statements and executes the statement following the loop statement.

The syntax of using the break statement is as follows:

for(initialization; condition; increment/decrement)
{
   //statements
   if(condition)
   {
      break;
   }
}


The following code will demonstrate the use of the break statement:

var a;
for(a=1; a<=10; a++)
{
   trace(a);
   if(a==5)
   {
      break;
   }
}


The above code will display a series from 1 to 5 on the output window.

The continue statement: The continue statement breaks the loop and starts the loop again with the next value. To break a loop statement at a point, the continue keyword is placed at that point.

The syntax of using the continue statement is as follows:

for(initialization; condition; increment/decrement)
{
   //statements
   if(condition)
   {
      continue
   }
}


The following example will demonstrate the use of the continue statement:

var a;
for(a=0;a<10;a++)
{
   if(a==5)
   {
      continue;
   }
   trace(a);
}


In the above code, the series from 1 to 4 will be displayed on the output window, the loop will break at 5, and again the series from 6 to 10 will be displayed.

The with statement: The with statement allows users to write shorter code related to an object. The properties and methods of that object can be invoked without writing the lengthy object reference. This can help to reduce the file size and also to check and debug the code.

The syntax of using the with statement is as follows:

with(object)
{
   //statements
}


The following example will demonstrate the use of the with statement:

with(myMovie)
{
   _x= 135;
   _y=452;
}


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.