Code Fragment can be executed only if a certain condition is met.
condition
has a boolean type (true
or false
).
Output:
if condition is true
- execute the code inside if
fragment.
Operator | Description | Examples | Result |
---|---|---|---|
== | Equal to | 5 == 5 | true |
4 == 5 | false | ||
3 + 2 == 5 | true | ||
!= | Not equal to | 7 != 5 | true |
4 != 4 | false | ||
true != false | true | ||
> | Greater than | 5 > 0 | true |
5 > 5 | false | ||
10 > 0 | true | ||
>= | Greater than or equal to | 3 >= 3 | true |
5 >= 4 | true | ||
3 >= 5 | false | ||
< | Less than | 3 < 5 | true |
5 < 6 | true | ||
5 < 5 | false | ||
<= | Less than or equal to | 2 <= 0 | false |
5 <= 5 | true | ||
5 < 1 | false |
Let's create a program that uses comparisons operators.
Determine whether person is adult (age >= 21) or child (age < 13) depending on his/her age.
Output:
Since age
is greater than 21, line 13
executes.
Because age is not less than 13, line 18
is not executed.