6 / 16

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 If block with value x > 0
  • output "Positive" in the true branch (line with true label)
  • output "Negative" in the false branch (line with false label)

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