Conditional Statements
Video Summary: https://youtu.be/HkSoyLfwtOI
Conditional statements allow the programmer to make some parts of the program optional; that is, they allow the programmer to say that some code can only run if the right conditions are met.
if
Statements
An if
statement uses a Boolean expression to determine whether
to execute a statement or to skip it. Here is the syntax template:
if (Boolean_Expression)
{
//code to run if Boolean_Expression is true
}
The expression in parentheses can be of any simple data type. Almost without exception, this will be a logical (Boolean) expression; if not, its value is implicitly coerced to type bool(nonzero value means true, zero value means false).
Now let's look at the following example:
int number, sum; sum = 10; cout << "Please enter an integer value: " << endl; cin >> number; if (number < 0) { number = 0; } sum = sum + number; cout << "The sum is " << sum << endl;
The expression (number < 0)
is evaluated.
If the result is true, the statement number = 0;
is executed.
If the result is false, that statement is skipped.
In either case, the next statement to be executed is sum = sum + number;
.
if-else
Statements
An if-else
statement uses a Boolean expression to determine which one of
the two statements to execute. Here is the syntax template:
if (Boolean_Expression)
{
//code to run if Boolean_Expression is true
}
else
{
//code to run if Boolean_Expression is false
}
The boolean expression in parentheses will be evaluated with the result of true or false. True results will always cause the statement or block after the if to run. False results will cause the statement or block marked with else to run. Here is an example:
cout << "You are "; if (age >= 65) cout << "a senior "; else cout << "not a senior "; cout << "citizen." << endl;
The characters "You are " are sent to the output stream.
The expression age >= 65 is evaluated.
If the result is true, the characters "a senior " are sent to the output stream.
If the result is false, the characters "not a senior " are sent to the output stream.
In either case, the next statement to be executed sends the the characters "citizen." to the output stream.
There is one thing to point out here: any statement in an IF or ELSE statement could be a block or a compound statement. If so, they must be enclosed in a pair of braces. Here is an example which also shows a compound expression:
if ( (age > 65) && (gender == 'f') ) { cout << "Quilting group meets:"; cout << "Thursday afternoons at 4:00 p.m."; }
if ( (age > 65) && (gender == 'f') ) cout << "Quilting group meets:"; // only old ladies see this cout << "Thursday afternoons at 4:00 p.m.";// everyone sees this
if
, and you do not use braces, then a later else
will not match
if ( (age > 65) && (gender == 'f') ) cout << "Quilting group meets:"; cout << "Thursday afternoons at 4:00 p.m."; // you will always see this else // Syntax error cout << "No quilting for you!";
Nested if
Statements
An if
statemenet uses a Boolean expression to determine whether to execute
or skip a statement. An if-else
statement uses a Boolean expression
to determine which one of two statements to execute. The statements to be
executed or skipped could be simple statements or compound statements (blocks).
These optional statements can also include an if
or if-else
statement. The result is called a nested if statement.
A look-ahead: You will soon be learning about the C++ SWITCH statement. For some very complex if/else constructs, it is preferable to use the switch instead.
The following example is a nested If statement.
cout << "You are "; if (age >= 65) cout << "a senior." << endl; else if (age >= 19) cout << "an adult." << endl; else if (age >= 13) cout << "a teenager." << endl; else cout << "a child." << endl; cout << "You are a great person." << endl;
The characters "You are " are sent to the output stream.
The expression age >= 65 is evaluated.
If the result is true, the characters "a senior." are sent to the output stream.
If the result is false, the expression age >= 19 is evaluated.
If the result is true,the characters "an adult." are sent to the
output stream.
If the result is false, the expression age >= 13 is evaluated.
If the result is true,the characters "a teeneager." are sent
to the output stream.
If the result is false, the expression "a child.
is sent to the output stream.
In any case above, the next statement to
be executed sends the the characters "You are a great person." to
the output stream.
Notice: once age has a value, only one statement is selected to be executed. If we add braces to the program segment, it would be easy to see the levels of nesting. Let's look at it now:
cout << "You are "; if (age >= 65) { cout << "a senior." << endl; cout << "*****" << endl; } else { if (age >= 19) { cout << "an adult." << endl; cout << "*****" << endl; } else { if (age >= 13) { cout << "a teenager." << endl; cout << "*****" << endl; } else { cout << "a child." << endl; cout << "*****" << endl; } } } cout << "You are a great person." << endl;
else-if
Statements
If you have a list of options you wish to test one after another, you might
want to try using some else if
statements
instead of nesting an if
as the only code
inside an else. You can study the following example, then try making a similar
change to the age example above.