How to learn to program in a simple and funny way. Learn2program

The alternative instruction. IF MINE and IF BLOCK

To use conditional structures we have invented a new element: mystery squares. In them there may be an obstacle or a mine. The robot will only know it if it executes a conditional IF statement.

Please note: Mystery squares will appear like a question mark “?”  in the minefields on the website.

Besides, if we want to count how many mines there are in the minefield now, we will have to use the alternative IF statement for sure. The syntax of this instruction in the DEMETER II language is the following:

IF MINE NEXT THEN

[some instructions]

END IF

Action: The rover will execute the instructions only if it has a mine in front of it.

 

The rover will also have the following instruction:

IF BLOCK THEN

[some instructions]

END IF

Action: The rover will execute the instructions only if something is in front of it to block it.

 

With these instructions, the rover will be able to perform the following exercise:

The solution to this exercise is the following one:


MINES = 0
STEPS = 0
A
STEPS++
TR
A
STEPS++
TL
A
STEPS++
TL
FOR 9
IF MINE THEN
DEACT
MINES++
END
TR
A
STEPS++
TL
END
FOR 12
A
STEPS++
END
TR
A
STEPS++PRINT MINES
PRINT STEPS

 

Important: if you want to use our class documentation (exercises) you can download it from the following link:

LEARN 2 PROGRAM SLIDES

 

The Demeter Project