Loops

Whenever a portion of the program is about to be repeated the same but with different values of some of its variables, we use loops.

For example:

There are 3 kinds of loops in MEHRANN language:

Whenever the number of iterations is deterministic, we usually use the first one, otherwise when the number of loops is dependent on a condition we use the second or third one.
The major difference between the last two is that "Do.....While" loops are run at least once.

Structure of For-Next Loops:

For <var_name> = <start_value> To <End_value> [Step <s>]
    .......
    .......
    .......
    .......
    .......
    .......
Next


 

Example (1): 

Integer i
For i = 1 To 10
    Println "i=" + i
Next


Example (2): 

Integer i , total_odds
total_odds = 0
For i = 1 To 100 Step 2
    total_odds = total_odds + i
Next
Println "total Odds= " + total_odds

 

 

Structure of Do- While Loops:

Do
    .......
    .......
    .......
    .......
    .......
    .......
While <Conditions>

 

Example : 

Integer i
i = 0
Do
    Println "i=" + i + "    ,    Sin(i)=" + SinD(i)
    i=i+1
While SinD(i)<0.8

 

 

Structure of While-Wend Loops:

While <Conditions>
    .......
    .......
    .......
    .......
    .......
    .......
Wend

Example : 

Integer i
i = 0
While SinD(i)<0.8
    Println "i=" + i + "    ,    Sin(i)=" + SinD(i)
    i=i+1
Wend