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

No comments:

Post a Comment