While Loop

'while' is a looping attribute which is used to perform a set of instructions until a given condition is satisfied.

SRON provides 'while' attribute to work on while loop. To provide a condition, 'condition' attribute is used.

Syntax of 'condition':

condition :  condition_value

Examples:

Correct Syntax:
    condition : false
or
    condition : ~{ val != 10 }~

---------------------

Wrong Syntax:
    condition : ()
or
    condition : (true)
or
    condition : ([1,2,3,4,5])

'condition' attribute requires a 'Bool' type value, if a 'Bool' type value is not found, then error/exception will be thrown.


Sample Code to print 0 to 5 numbers.

{
    name : MAIN

    Int i

    while : {
        condition : ~{ i < 6 }~

        println(i)

        i += 1
    }
}

OUTPUT :

0 1 2 3 4 5


Sample Code to print even number between 0 and 15.

{
    name : MAIN

    Int i = 2

    while : {
        condition : ~{ i < 15 }~

        if : {
            condition : ~{ i % 2 == 0 }~
            print(i, ' ')
        }
        
        i += 1
    }
}

OUTPUT :

2 4 6 8 10 12 14


Last updated