Conditional/Selective Constructs
Learning Outcome(s):
1. Identify the different conditional/selective constructs.
2. Use the different conditional/selective constructs in a program.
CONDITIONAL STATEMENTS
Conditional statements are statements that check an expression then may or may
not execute a statement or group of statements depending on the result of the
condition.
1. if statement
Syntax:
if (condition) {
statements;
}
In an if statement, if the condition evaluates to TRUE, the statement or block
of statements that forms the target of the if statement will be executed. Otherwise,
the program will ignore the statement or block of statements.
Note: Never place a semicolon after the expression in an IF Statement.
Example:
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv) {
int grade;
cout << "Enter grade: ";
cin >> grade;
if (grade >= 75){
cout << "You Passed!\n";
}
return 0;
}
Sample Output:
CC 102 – Computer Programming 1 1
2. if – else statement
Syntax:
if (condition) {
statements;
}
else {
statements;
}
In an if – else statement, if the condition is TRUE, the statement or block of
statements after the IF statement will be executed; otherwise, the statement or
block of statements in the ELSE statement will be executed.
Note: Only the code associated with the IF or the code that is associated
with the ELSE executes, NEVER both.
Example:
#include <iostream>
int main(int argc, char** argv) {
int grade;
cout << "\n\nEnter grade: ";
cin >> grade;
if (grade >= 75){
cout << "You Passed!\n";
}
else {
cout << "You Failed!\n";
}
return 0;
}
Sample Output:
CC 102 – Computer Programming 1 2
3. if – else – if ladder statement
Syntax:
if (condition1) {
statements;
}
else if (condition2) {
statements;
}
else if (condition3) {
statements;
}
.
.
.
else if (conditionN) {
statements;
}
else {
statements;
}
In an if – else – if ladder statement, the conditions are evaluated from the
top downward. As soon as a true condition is found, the statement associated
with it is executed, and the rest of the ladder will not be executed. If none of the
condition is true, the final ELSE is executed.
The final ELSE acts as a default condition. If all other conditions are false,
the last else statement is performed. If the final else is not present, then no action
takes place.
Example:
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv) {
int day;
cout << "\n\nEnter day: ";
cin >> day;
CC 102 – Computer Programming 1 3
if (day == 1){
cout << "It's Sunday!\n";
}
else if (day == 2){
cout << "It's Monday!\n";
}
else if (day == 3){
cout << "It's Tuesday!\n";
}
else if (day == 4){
cout << "It's Wednesday!\n";
}
else if (day == 5){
cout << "It's Thursday!\n";
}
else if (day == 6){
cout << "It's Friday!\n";
}
else if (day == 7){
cout << "It's Saturday!\n";
}
else {
cout << "Not a day of the week!\n";
}
return 0;
}
Sample Output:
CC 102 – Computer Programming 1 4
4. nested – if statement
Syntax:
if (condition1) {
if (condition2) {
statements;
}
Else {
Statements;
}
}
else {
if (condition3) {
statements;
}
else {
statements;
}
One of the most confusing aspects of the if statement in any programming
language is the nested ifs. A nested if is an if statement that is the object of either
an if or else. This is sometimes referred to as “an if within an if.”
Example:
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv) {
int a, b, c;
cout << "\n\nEnter a: ";
cin >> a;
cout << "Enter b: ";
cin >> b;
cout << "Enter c: ";
cin >> c;
if (a > b){
if (a > c){
cout << "\n\na is the largest!\n";
}
else {
cout << "\n\nc is the largest!\n";
}
}
else {
if (b > c){
cout << "\n\nb is the largest!\n";
}
CC 102 – Computer Programming 1 5
else {
cout << "\n\nc is the largest!\n";
}
}
return 0;
}
Sample Output:
CC 102 – Computer Programming 1 6
5. switch statement
Syntax:
switch (variable) {
case constant1:
statements;
break;
case constant2:
statements;
break;
.
.
.
default:
statements;
}
In a switch statement, a variable is successively tested against a list of
integer or character constants. If a match is found, a statement or block of
statements is executed. The default part of the switch is executed if no matches
are found.
Note: The break statement is used to terminate the statement sequence
associated with each case constant.
Example:
#include <iostream>
#include <ctdlib>
using namespace std;
int main(int argc, char** argv) {
int day;
cout << "Enter day: ";
cin >> day;
switch (day){
case 1:
cout << "SUNDAY!\n";
break;
case 2:
cout << "MONDAY!\n";
break;
case 3:
cout << "TUESDAY!\n";
break;
case 4:
cout << "WEDNESDAY!\n";
break;
CC 102 – Computer Programming 1 7
case 5:
cout << "THURSDAY!\n";
break;
case 6:
cout << "FRIDAY!\n";
break;
case 7:
cout << "SATURDAY!\n";
break;
default:
cout << "NOT A DAY OF THE WEEK!\n";
}
return 0;
}
Sample Output:
CC 102 – Computer Programming 1 8