Basic Control Flow
Conditional Jumps

Important: It is now time to start putting code in your own .mint files. You will find that the interactive interpreter doesn't handle multi-line control flow structures.

The when statement is the simplest of the control flow statements.
When a when statement's condition is true, it executes the next line. Otherwise it skips the next line.
If you have ever used the languages Java, C, or C++, you should know that a Mint
when statement is equivalent to an if statement with no curly braces in any of those languages.
a = -154
when a < 0
    print "a is less than 0."        //Will be printed.
when a > 0
    print "a is greater than 0."     //Won't be printed.
The if statement is the cousin to the when statement. When an if statement begins, it opens a clause.
A clause must be closed with an end statement. If the if statement's condition is true, it executes the clause.
Otherwise, the clause is ignored.
For Java, C, and C++, the Mint if statement and its corresponding end statement is the same as an if statement that does have curly braces.
a = 200
b = 300
if a == 200 and b == 300
    show "a is two hundred and"     //This clause will be executed.
    print " b is three hundred."
end
a *= 2
b *= 2
if a == 500 and b == 600
    show "a is five hundred and"    //This clause won't be executed.
    print " b is six hundred."
end
If statements can take an optional else clause, which executes in the event that the if statement is false.
show "What is your name? "
name = input
if name == "Jack"
    print "Hello, Jack!"
else
    print "Who are you?"
end
There's also a thing called the if ... else if ... else statement which represents a chain of if statements:
import time

today = weekdayNumber()
if today == 0
    print "Sunday"
else if today == 1
    print "Monday"
else if today == 2
    print "Tuesday"
else if today == 3
    print "Wednesday"
else if today == 4
    print "Thursday"
else if today == 5
    print "Friday"
else
    print "Saturday"
end
The above if ... else if ... else statement will print the current day of week.
Notice that we did 'import time', which imports a library known as time into the current scope.
This allows us to call the subprogram weekdayNumber and get the current day
of the week as a number. There will be more info about subprograms later.

Navigation
Previous Lesson: Operators
Next Lesson: More Control Flow
Table of Contents