Conditional Statement
Programming
Fundamentals
Decision Making
• If
• If-else
If-else • If-else if Switch
• Nested it
If Statement
C++ supports • Less than: a < b
the usual • Less than or equal to a <= b
logical • Greater than: a > b
• Greater than or equal to a >= b
conditions • Equal to a == b
from • Not Equal to a != b
mathematics:
C++ has the following conditional
statements:
Use if to specify a block of code to be executed, if a specified
condition is true
Use else to specify a block of code to be executed, if the same
condition is false
Use else if to specify a new condition to test, if the first
condition is false
Use switch to specify many alternative blocks of code to be
executed
The if Statement
• Use the if statement to specify a block of C++
code to be executed if a condition is true.
• Syntax
if (condition) {
// block of code
}
Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an error.
Example of if statement
Example 1 Example 2
#include <iostream>
using namespace std;
int x = 20;
int main() { int y = 18;
if (20 > 18) { if (x > y) {
cout << "20 is greater than 18"; cout << "x is greater than y";
} }
return 0;
}
Else Statement
• Use the else statement to specify a block of
code to be executed if the condition is false.
• Syntax
if (condition) {
// block of code
else {
// block of code
#include <iostream>
using namespace std;
Example
int main() {
In the example above, time int time = 20;
(20) is greater than 18, so the
condition is false. Because of if (time < 18) {
this, we move on to the else cout << "Good day.";
condition and print to the
screen "Good evening". If the }
time was less than 18, the else {
program would print "Good cout << "Good evening.";
day".
}
return 0;
}
Else if Statement
Use the else if statement to specify a new condition if the first condition is false.
if (condition1) {
// block of code to be executed if condition1 is true
}
else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
}
else {
// block of code to be executed if the condition1 is false and condition2 is false
}
Flowchart
int time = 22;
Example if (time < 10) {
cout << "Good morning.";
In this example above, time (22)
is greater than 10, so the first
}
condition is false. else if (time < 20) {
The next condition, in the else if
statement, is also false, so we cout << "Good day.";
move on to the else condition
since condition1 and condition2
}
is both false - and print to the
screen "Good evening".
else {
However, if the time was 14, our cout << "Good evening.";
program would print "Good
day."
}
Nested if Statement
A nested if in C is an if statement that is the target of another
if statement.
Nested if statements mean an if statement inside another if
statement.
Yes, both C and C++ allow us to nested if statements within if
statements, i.e., we can place an if statement inside another if
statement.
Flowchart
Syntax if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
if (i == 10)
{
Example // First if statement
if (i < 15)
cout<<"i is smaller than 15\n";
// Nested - if statement
// Will only be executed if statement above
// is true
if (i < 12)
cout<<"i is smaller than 12 too\n";
else
cout<<"i is greater than 15";
}
Shorthand if..else
There is also a short-hand if else, which is known as the ternary operator
because it consists of three operands. It can be used to replace multiple
lines of code with a single line. It is often used to replace simple if else
statements:
variable = (condition) ? expressionTrue : expressionFalse;
string result = (time < 18) ? "Good day." : "Good evening.";
Describe the output produced by
this poorly indented program
segment:
int number = 4;
double alpha = -1.0;
if (number > 0)
if (alpha > 0)
cout << "Here I am!" << endl;
else
cout << "No, I’m here!" << endl;
cout << "Not, actually" << endl;
Consider the following if statement, where
doesSignificantWork,
makesBreakthrough,and
nobelPrizeCandidate are all boolean
if (doesSignificantWork) { variables:
if (makesBreakthrough)
nobelPrizeCandidate = true;
else
nobelPrizeCandidate = false;
}
else if (!doesSignificantWork) First, write a simpler if statement that is
nobelPrizeCandidate = false; equivalent to this one. Then write a
single
assignment statement that does the
same thing.
A 4-digit number is entered through
keyboard. Write a program to print a new
number with digits reversed as of
original one.
INPUT : 1234 OUTPUT : 4321
INPUT : 5982 OUTPUT : 2895
End of Conditional Statement
Else if
If Statement Else Statement
Statement
Shorthand
Nested if
Exercises if..else
Statement
Statement