Showing posts with label condition. Show all posts
Showing posts with label condition. Show all posts

Sunday, April 4, 2010

For Loops

The for Loop

Syntax:


(click image to view)

Description of parameters:
  • init - used to set a counter.
  • condition - evaluated for each loop iteration. If it evaluate to true, the loops will continue. Otherwise, the loops will end.
  • increment - used to increment the counter.
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. x will increase by 1 each time the loop runs:


(click image to view)

############################################################
The Output:

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



The foreach Loop

Syntax:


(click image to view)

The example below shows a loop that will print the values of the given array:


(click image to view)

############################################################
The Output:

satu
dua
tiga
############################################################

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
############################################################