What is the while 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 while loop?

Rating:

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.

In he following example, the while loop is executed till the value of the variable x is less than 10.

stop();
x=1
while(x<10){
trace("X is equal to " +x); // output: 1,2,3,4,5,6,7,8,9
x++ // same as x = x + 1
}


Rating:



Other articles

Click here to Article home

 
uCertify.com | Our Company | Articles | Privacy | Security | Contact Us
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.