Sunday, April 4, 2010

While Loops

The while Loop

Syntax:


(click image to view)

The example below shows a loop that starts with x=1. The loop will continue to run as long as x is less than, or equal to 3. And x will increase by 1 each time the loop runs:


(click image to view)

############################################################
The Ouput:

The number is 1
The number is 2
The number is 3
############################################################



The do..while Loop

Syntax:


(click image to view)

The example below shows a loop that starts with x=1. It will increment x with 1, and write the output. Then the condition is checked. If condition is true, then the loop will continue to run as long as the x is less than, or equal to 3:


(click image to view)

############################################################
The Ouput:

The number is 2
The number is 3
The number is 4
############################################################

No comments:

Post a Comment