Conditional instructions

Programs made of simple suite of instructions similar to previous exercise are quite boring. They always do the same thing, and cannot react to external conditions. A conditional let the program adapt by doing something like if it's raining, take an umbrella.

The [!thelang] syntax is the following:

[!java|scala|c]if (condition) {
    whatToDoIfTrue();
    whatToDoNextIfTrue();
}[/!][!python]if condition:
    whatToDoIfTrue()
    whatToDoNextIfTrue()[/!]
whatToDoAnyway()[!java|c];[/!]

If the condition is true, the code of the next block will be executed and then it will continue with the rest of the code. If the condition is false, the next block is ignored and the execution continues after it. The conditional block can contain several instructions, it can even contain other tests, along with their sub-blocks.

In this example, the instructions whatToDoIfTrue() and whatToDoNextIfTrue() will be executed if and only if the condition is true, while the instruction whatToDoAnyway() will be executed whether or not the condition is true.

In [!thelang], the blocks of code are [!java|scala|c]enclosed between curly brackets: a { sign opens the block, while a } sign closes it. White spaces are not important[/!][!java|c].[/!][!scala], provided that your instructions are still separated with a semi-column or an end of line.[/!] [!java|scala|c]It is still very important to correctly indent your code to keep it readable.[/!] [!python]marked by the indentation: every lines that are a bit shifted to the right with white spaces belong to the block. Quite often, people use 4 spaces for indentation, but it works if you use more or less spaces. Simply, any lines of the block must use the same amount of spaces. The end of Python code blocks are not marked by any specific char. Indenting lines starts a block and unindenting ends it. Do not forget the colon (:) at the end of the if line, python needs it to know that a new block begins. The fact that python relies on indentation to delimit blocks is a very good property for beginners: it will force you to adhere to strict code presentation standards.[/!] It is very easy to get lost in your own code if it's not properly indented, so you want to clean it up so that working on your code remains pleasant and productive.

All indentations of a given block must be consistent, and it is not possible to cut a block. The two following codes are incorrect and will raise errors.

if condition:
    whatToDo()
     whatToDoNext() # one space too much 
whatToDoAnyway()
if condition:
    whatToDo()
whatToDoAnyway()
    whatToDoNext() # this block is not hanging to a condition line

The condition must be a [!c]int[/!][!java]boolean[/!][!scala|python]Boolean[/!] expression. The inner block of code will get executed if the expression is evaluated to [!c]1[/!][!java|scala]true[/!][!python]True[/!] and it will be ignored if it is [!c]0[/!][!java|scala]false[/!][!python]False[/!]. [!python|scala|java][!java|scala]true[/!][!python]True[/!] and [!java|scala]false[/!][!python]False[/!] are constant values defined by [!thelang] directly, just as 0 or 1 in mathematics.[/!]

The condition can be a [!c]int[/!][!java]boolean[/!][!scala|python]Boolean[/!] variable (we will come back on variables in a later exercise, don't worry) or an arithmetic test, such as x == 5, which checks whether the current value of x is 5, or such as != (checking inequality, that is, returning [!c]1[/!][!java|scala]true[/!][!python]True[/!] only if the left-hand side is different from the right-hand side), < (smaller than), > (larger than), <= (smaller or equal to), >= (larger or equal to).

Beware of the classical trap, which consists in testing the equality of a variable using = instead of ==. Hopefully, the [!java|scala|c]compiler[/!][!python]interpreter[/!] detects this problem most of the time, but it could get trapped is some cases (such as when you are affecting a [!c]int[/!][!java|python|scala]boolean[/!] variable). So you'd better to be careful...

The condition can also be a call to some particular methods returning a boolean. For example, the isFacingWall() method of the buggle returns [!c]1[/!][!java|python|scala]true[/!] if the buggle is facing a wall, and [!c]0[/!][!java|python|scala]false[/!] in the other case.

Finally, a condition can be composed of several sub-conditions connected by boolean operations:

Last, it is possible to specify what to do when the condition is false using the following syntax. In this case, the instruction whatToDoIfItsFalse will be executed only if the condition is false.

[!java|scala|c]if (condition) {
    whatToDoIfTheConditionIsTrue();
} else {
    whatToDoIfItsFalse();
}[/!][!python]if (condition):
    whatToDoIfTheConditionIsTrue()
else:
    whatToDoIfItsFalse()[/!]

Don't forget the colon (:) after the else, it is indicating that a new block is beginning.

Exercise goal

If the buggle is facing a wall, you must move one step back. If not, you must move one step forward. To detect whether you are facing a wall, simply use the isFacingWall() built-in, that every buggle understands.

This exercise is a bit different: your code has to work for several buggles, each of them being in a specific initial condition. The same code will be executed for each of them.

When your program works, move forward to the next exercise, that is hidden in a sub-tree of the selection window.