More Control Flow
Conditional Jumps

The while statement is designed to do an action repeatedly. While the while statement's condition is true, it keeps executing its clause. When the condition becomes false, it skips the clause.
i = 11
while i > 0
    print i     //Counts down from 10 to 0.
print "Done!"
The for statement is similar to the while statement except that it takes a different format.
for <initialization>; <condition>; <change>
The initialization statement is executed once before the for loop begins. The condition is checked so that the for loop can keep running (the for loop stops running once the condition is false), and the change statement occurs every time the for loop reaches the end of its clause.
for i = 10; i >= 0; i--
    print i    //Counts down from 10 to 0
print "Done!"
There's a special variation of the for loop known as the for each loop, which goes across each item in an iterable. An iterable is a variable that holds a series of items, such as a list or a string. Strings are variables that hold text. Text values are enclosed in double quotes, as we've seen many times before.
x = "hello"                               //This is a string.
y = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]    //This is a list of numbers.
for each i of y
    print i     //Counts down from 10 to 0.
for each character of x
    print character    //Prints every character in "hello".
Lists are enclosed in square brackets and have elements separated by commas. We will learn more about them later.

There's a simplified version of the for loop known as the repeat loop.
repeat 5
    print "Hi"   //Prints Hi five times.

i = 10
z = 0
repeat i
    z++          //Counts up from 1 to 10.
    print z
repeat <number> will repeat its clause <number> times. If you use a variable after the repeat keyword, the variable is not modified. Instead, the repeat loop repeats its clause for the number of times indicated by the current value of the variable.

Switch statements are a special case of the if ... else if ... else construction.
x = 3

switch x
    case 1
        print "x is one."
    case 2, 3, 4
        print "x is in the set {2, 3, 4}."
    case 5
        print "x is five."
        print "x is not an integer between 1 and 5."
switch <number> will search all cases in the switch clause for the corresponding number, and executes the corresponding case.
If there are no matching cases, the default case is executed.
Unlike other programming languages, in Mint cases do not "fall through", and therefore don't need to be terminated with a break statement.

I also included a simplified version of the if statement known as the conditional or ternary operator. Here it is:
var = "hi" == "hi" yield 100 otherwise 200
print var
t = 0 != 0 ? 1 : 2
print t
You may either use yield ... otherwise or the ?: operator to perform a conditional.

Previous Lesson: Basic Control Flow
Next Lesson: Lists
Table of Contents