Personal tools
You are here: Home Members mpate1's Home Lab6-Loop
Document Actions

Lab6-Loop

by Mit Patel last modified 2006-02-21 11:12 PM

Loops Have:

  1. Some kind of initialization
  2. A body
  3. And a loop terminating condition

    Otherwise, a non terminating loop is known as infinite loop and you will think your machine is broken.

While:

while(condition) {

...statements for execution;

}

The condition of while loop has to be a boolean expresion
  1. If true: the statements in the body of the loop will continue to execute.
  2. Until the condition becomes false
  3. If false: while loop terminates. This is caused by something in the body of the loop.

In while loop you can't determine how many times the loop would run.

Do:

do {

...statements;

} while (condition);

Do loop starts out with statements instead of condition:

Thus, the body of the do loop executes before the condition.

  1. So, initially the body of do loop is executed
  2. Then the condition of a while loop is evaluated
  3. And when the condition becomes false the loop stops otherwise body of do loop will keep executing.

In do loop, the execution takes place at least once.

For:

for(initialization; condition; iteration){

...body (statements) of the loop;

}

Unlike do loop, the condition for for loop is tested before execution:
  1. First you initialize the vairable
  2. Then you set the condition according to which the loop will execute
  3. And finally the iteration is set: How many times you want the loop to execute?

In for loop, you can set the number of times you want the body to be executed.


Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: