Break & Continue

There are many scenarios in programming when you have to exit or restart a loop based upon a certain condition or scenario.

SRON provides 'break' and 'continue' keyword to control and manipulate the control flow of loops. Both of these keywords can only be used inside the scope of 'for', 'foreach' or 'while' attribute.

'break' keyword is used to shift the control flow of program outside the scope of loop. Let's take a look on usage of 'break' keyword. Below is a sample code:

{
    name : MAIN
    for : {
        range : (Int i = 1, 7)
        if : {
            condition : ~{ i == 5 }~
            println("Breaking the loop")
            break
        }
        println(i)
    }
    println("Outside the Loop!")
}

OUTPUT :

1 2 3 4 Breaking the loop Outside the Loop!


Now, let's see how 'continue' keyword. It is used to shift the control flow of program to the starting of the loop. See the below sample code for it:

{
    name : MAIN
    Int i = 10
    while : {
        condition : ~{ i < 20 }~
        if : {
            condition : ~{ i % 5 == 0 }~
            i += 1
            println("continuing loop")
            continue
        }
        i += 1
        println(i)
    }
    println("Outside the Loop!")
}

OUTPUT :

continuing loop 11 12 13 14 continuing loop 16 17 18 19 Outside the Loop!


Last updated