Conditional execution
Conditional statements first evaluate a Boolean condition and then,
based on whether it's true or false, execute other statements.
The most common form is:
|
Sometimes, the else clause is omitted:
|
Often, the else clause provides further
if statements:
|
if(condition1) {
// more statements;
.....
}
else {
// more statements;
.....
}
|
|
if(condition1) {
// more statements;
.....
}
|
|
if(condition1) {
// more statements;
.....
}
else if(condition2) {
// more statements;
.....
}
else {
// more statements;
.....
}
|
|
Note that in the examples, above,
each block of statements to be executed
has been written within curly-brackets.
The curly-brackets are not required
(we could just write a single statement for
either if or else clause).
However, adding curly-brackets is considered a good practice.
They provide a safeguard for when additional statements are added later.
CITS2002 Systems Programming, Lecture 2, p9, 24th July 2024.
|