Monday, 14 March 2016

Class 8 - Repetition Programming(For)

Loops

If we want to repeat a set of statements , looping statements are used.
There are two types :
  • For ... Next
  • Do ....Loop

For ... Next

Probably this is the easiest of the looping statements. The general form is given as

For variable=initial value to final value step value
--------------
-------------
Next variable

1. Display the numbers from 1 to 10.

Dim a as integer
For a=1 to 10
Print a
Next a

Note :
  • Step value is used to increment or decrement . In the above program it should have been Step 1, since the difference between two numbers is 1. But Step 1 is the default value so no need to write it.
2. Display the even numbers from 5 to 50

Dim a as integer
For a=6 to 50 step 2
Print a
Next a

3. Display the following numbers:
     100 
     90
     80
     ....
     ....
     10


Dim a as integer
For a=100 to 10 step -10
Print a
Next a

Note :
Here the numbers are in descending order , therefore step -10. 
 

No comments:

Post a Comment