Ring/Lessons/Control Structures

Control Structures
In this chapter we are going to learn about the control structures provided by the Ring programming language.

Branching

 * If Statement

Syntax:

Example:


 * Switch Statement

Syntax:

Example:

Looping

 * While Loop

Syntax:

Example:


 * For Loop

Syntax:

Example:

Example:

Example:

Example:


 * For in Loop

Syntax:

Example:

Using The Step option with For in
We can use the Step option with For in to skip number of items in each iteration

Example:

Using For in to modify lists
When we use (For in) we get items by reference.

This means that we can read/edit items inside the loop. Example:

Do Again Loop
Syntax:

Example:

Exit Command
Used to go outside one or more of loops.

Syntax:

Example:

Exit from two loops
The next example presents how to use the exit command to exit from two loops in one jump.

Example:


 * Loop Command

Used to jump to the next iteration in the loop.

Syntax:

Example:

Exit/Loop inside sub functions
While we are inside a loop, we can call a function then use the exit and/or loop command inside that function and the command will work on the outer loop.

Example:

Short-circuit evaluation
The logical operators and/or follow the `short-circuit evaluation `_.

If the first argument of the AND operator is zero, then there is no need to evaluate the second argument and the result will be zero.

If the first argument of the OR operator is one, then there is no need to evaluate the second argument and the result will be one.

Example:

Example:

Example:

Comments about evaluation

 * True, False, nl & NULL are variables defined by the language


 * True = 1


 * False = 0


 * nl = new line


 * NULL = empty string = ""


 * Everything evaluates to true except 0 (False).

Example: