If-s
We already saw that a program executes one command after another, from top to bottom.
But usually we need some branching/decisions to make while executing a program.
Let's write a program that will print if a number is positive or negative:
- declare an integer
x
- input
x
- add
If
block with valuex > 0
- in the
true
branch output"Positive"
- in the
false
branch output"Negative"
For example, if the user enters number 9, output should be "Positive".
If user enters number -55, output should be "Negative".
Maybe you haven't thought about it, but expression x > 0
has type Boolean
.
Boolean
can be true
or false
.
Congrats, you've come to the end of the Basic tutorial series.
Hope you have learned something useful! :)
Continue learning with Intermediate level tutorials.