Conditional statement (if
)
In the problem of finding the maximum number of two, we met a new operator that began with the word
if
.
This operator is called conditional.
The word
if
is translated from English as "
if", and the word
else
as "
else ". After the word
if
, a logical condition is written, and if it is true (true), then all commands (operators) that appear after the condition in curly brackets
{}
will be executed. If condition is false (false), then the commands in curly braces after the word
else
.
are executed
General view of the conditional operator
if (boolean_condition ) // header with condition
{
... // "if" block — statements that are executed
// if condition in header is true
}
else
{
... // "otherwise" block — statements that are executed
// if condition in brackets is false
}
Need to remember!
1. if
- else - is a single statement. Therefore, between the parenthesis that ends the if
(}) and the word else
cannot contain other operators.
2. Never put a condition after the word else
. The "else" is executed when the main condition specified after the word if
- is false, that is, it is not fulfilled.
3. If, in the block "if" or in the "else" there is only one operator, then the curly braces can be omitted.
4. A Boolean condition is an expression that can be used to say whether it is true (meaning it is true) or false (meaning it is not true).
A logical condition is written using the signs of logical relations
>, < |
greater than less |
>=, <= |
greater than or equal, less than or equal to |
== |
equals |
!= |
not equal |