Ifs
We saw that a program executes block by block, from top to bottom. But we usually need some branching/decisions to make in a program.
Let's make a program that prints whether a number is positive or negative:
- declare
x: Integer - input
x - add
Ifblock with valuex > 0 - output
"Positive"in thetruebranch (line withtruelabel) - output
"Negative"in thefalsebranch (line withfalselabel)
For example, if the user enters number 9, output should be "Positive". If the user enters number -55, output should be "Negative".
The expression x > 0 has type Boolean.
Boolean can be true or false.
We use the comparison operators that return a Boolean value:
<,<=,==,!=,>,>=for comparing numbers==and!=for comparing other types