Conditional command

In the previous chapter, we worked with logical expressions that have gained a value true or false. Beside the loop with a condition (the While loop, the Repeat loop), they are most often used in the conditional If command. With this command you can decide whether a command, or the sequence of commands is to be executed or not.

The conditional If command is written as follows:

if condition then command;

If it is a compound command, it is written as follows:

if condition then

begin

command1;

command2;

...

end;

This is the syntax of the conditional If command with an incomplete condition with the commands to be executed only when the logical expression becomes true.

Implementing the conditional If command will be as follows:

  • When executing a conditional command, the condition is first tested and if it is true (valid), the command or the sequence of commands is executed (in nested conditional If command). If the condition is not met, the program does nothing.

Let´s show it on a simple example (Figure 75). The program evaluates whether the entered number is positive. If the condition is met (valid), the program prints to the Label component "The given number is positive", otherwise the program does nothing (Figure 76). 

Figure 75: Conditional If command with incomplete condition
Figure 75: Conditional If command with incomplete condition
Figure 76: Evaluation of the condition
Figure 76: Evaluation of the condition

The syntax of the conditional If command with a full condition has the following form:

if condition then

command1

else

command2;

If the logical expression (condition) is true, the command 1 is executed, otherwise the command 2. Always only one of these two commands is executed. When using multiple commands (a compound command), it is necessary to close them with the begin and end keywords.

if condition then

begin

command1;

command2;

...

end

else

begin

command3;

command4;

...

end;

Implementing the conditional If command with the full condition will be as follows:

  • The beginning of the conditional If command (until the else word) works as before, that is if the condition is met (valid), the command is executed. Commands written after the else keyword are executed if the condition is not met.
  • Note! The last command before the else keyword must not be finished with a semicolon as the program would evaluate such a listing as a conditional If command with an incomplete condition, and the commands after the else keyword would be ignored.

In the previous example (Figure 75), the program did not do anything when entering a negative number. Now we want the program to write "The given number is negative" (Figure 77). 

Figure 77: Conditional If command with full condition
Figure 77: Conditional If command with full condition

For the sake of clarity, let´s look at another example, which will draw red dots on the left half of the graphical area and blue dots on the right half. Except for the conditional If command we will also use a For loop with a fixed number of repetitions (Figure 78).

Figure 78: Conditional If command and For loop
Figure 78: Conditional If command and For loop

Note! If after a command in the first branch a semicolon is written, we will indicate that the else-branch will not be, and there is another command following.

After running the program (F9) we see following (Figure 79):

Figure 79: Drawing by using the conditional If command
Figure 79: Drawing by using the conditional If command
Vytvorte si webové stránky zdarma! Táto stránka bola vytvorená pomocou služby Webnode. Vytvorte si vlastný web zdarma ešte dnes! Vytvoriť stránky