for

for loop

repeat a block of codes a fixed number of times

  1. The following will loop through 3 times
  2. for n from 1 to 3
    {
    	echo "n = " + n
    }
    
    • The block of codes to be repeated must be enclosed in a pair of curly brackets { and }
    • Output is as follows:
    • n = 1
      n = 2
      n = 3
      
  3. You can also write the above using the javascript syntax:
  4. for (n=1; n<=3; n++)
    {
    	echo "n = " + n
    }
    

    Output is the same as above.

  5. You can also increment by values other than 1. E.g. the following lists the even numbers:
  6. for (n=0; n<10; n+=2)
    {
    	echo "n = " + n
    }
    

    Output

    n = 0
    n = 2
    n = 4
    n = 6
    n = 8
    

Comments (0)

There are no comments posted here yet

Leave your comments

Posting comment as a guest. Sign up or login to your account.
Attachments (0 / 3)
Share Your Location