Module-5-Selection-Control-Statement
Module-5-Selection-Control-Statement
Topics
1. if Statement
2. if-else Statement
3. Nested-if Statement
4. Switch Statement
Objectives
Understand, demonstrate, and integrate conditionals statement such as the: if, else, if-else, and switch
in writing and solving C++ program.
Content
The selection control statements allows to choose the set-of-instructions for execution depending upon an
expression's truth value. C++ provides following two types of selection statements:
if statement
switch statement
The selection control statements are also called conditional statements or decision statements and are divided
into these parts:
1. if Statement
An if statement tests a particular condition; if the condition evaluates to true, a course-of-action is followed i.e., a
statement or set-of-statements is executed. Otherwise if the condition evaluates to false, the course-of-action is
ignored.
if (expression)
{
statement(s);
}
Here, statement may be a single statement or a block of statements, or nothing (in case of empty statement).
The expression must be enclosed in parentheses. If the expression evaluates to true i.e., a nonzero value, the
statement is executed, otherwise ignored. For example, the following code fragment
Example No 1:
The example checks whether the character variable ch stores a space or not; if it does, the number of spaces are
incremented by 1.
Example No 2:
char ch;
cin >> ch;
The above example reads a character. If the character input is a space, if flashes a message specifying it. If the
character input is a digit, it flashes a message specifying it. Let's take an example demonstrating the if statement
of C++ practically.
Example No 3:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num;
cout<<"Enter a number: ";
cin>>num;
if(num%2==0)
{
cout<<"You entered an even number";
}
getch();
}
Example No. 4:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x, y, z, max;
cout<<"Enter any three numbers: ";
cin>>x>>y>>z;
max = x;
if(y>max)
{
max = y;
}
if(z>max)
{
max = z;
}
cout<<"\n"<<"The largest of "<<x<<", "<<y<<" and "<<z<<" is "<<max;
getch();
}
When the above C++ program is compiled and executed, it will produce the following output. This is the output, if
3rd number is biggest.
2. if-else Statement
You have seen so far allow you to execute a set of statements if a condition or expression evaluates to true.
What if there is another course of action to be followed if the expression evaluates to false ? There is another
form of if that allows for this kind of either-or condition by providing an else clause. The syntax of the if-else
statement is the following:
if(expression)
{
statement 1;
}
else
{
statement 2;
}
If the expression evaluates to true i.e., a nonzero value, the statement 1 is executed, otherwise, statement 2 is
executed. The statement 1 and statement 2 can be a single statement, or a compound statement, or a null
statement.
Note - Remember, in an if-else statement, only the code associated with if (i.e., statement 1) or the code
associated with else (i.e., statement 2) executes, never both.
Example No. 1:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num;
cout<<"Enter a number: ";
cin>>num;
if(num%2==0)
{
cout<<"You entered an even number";
}
else
{
cout<<"You entered an odd number";
}
getch();
}
Below are the two sample run of the above C++ program:
Example No. 2:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x, y ;
cout<<"Enter two numbers: ";
cin>>x>>y;
if(x>y)
{
cout<<x<<" is largest";
}
else
{
cout<<y<<" is largest";
}
getch();
}
When the above C++ program is compiled and executed, it will produce the following output:
Important - Placement of semicolon is also important. In an if statement, DO NOT put semicolon in the line
having test condition, such as
Caution - If you put a semicolon after the test condition, the if statement ends there. The block or statements
following are no more part of if in such cases. Now, let's discuss nested ifs statement in C++.
3. Nested-if Statement
A nested if is an if that has another if in its if's body or in its else's body. The nested if can have one of the
following three forms. Here is the first form:
if(expression 1)
{
if(expression 2)
{
statement 1;
}
else
{
statement 2;
}
}
else
{
body-of-else;
}
if(expression 1)
{
body-of-if;
else
{
if(expression 2)
{
statement 1;
}
else
{
statement 2;
}
}
}
if(expression 1)
{
if(expression 2)
{
statement 1;
}
else
{
statement 2;
}
}
else
{
if(expression 3)
{
statement 3;
}
else
{
statement 4;
}
}
Example No. 1:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num;
cout<<"Enter a number: ";
cin>>num;
if(num>100)
{
if(num>500)
{
if(num>10000)
{
cout<<"Its too high..!!";
}
if(num<1000)
{
cout<<"Its medium..!!";
}
}
if(num<=500)
{
cout<<"Its low..!!";
}
}
if(num<=100)
{
cout<<"Its too low..!!";
}
getch();
}
4. Switch Statement
This selection statement successively tests the value of an expression against a list of integer or character
constants. When a match is found, the statements associated with that constant are executed. The syntax of
switch statement is as follows:
switch(expression)
{
case constant1 : statement sequence 1;
break;
case constant2 : statement sequence 2;
break;
case constant3 : statement sequence 3;
break;
.
.
.
case constant n-1 : statement sequence n-1;
break;
default : statement sequence n;
}
The expression is evaluated and its values are matched against the values of the constants specified in the case
statements. When a match is found, the statement sequence associated with that case is executed until the
break statement or the end of switch statement is reached. If a case statement does not include a break
statement, then the control continues right on the next case statement(s) until either a break is encountered or
end of switch is reached. This situation (i.e., missing break in case statement) is called fall through. The default
statement gets executed when no match is found. The default statement is optional and, if it is missing , no
action takes place if all matches fail.
Example No. 1:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int dow;
cout<<"Enter number of week's day (1-7): ";
cin>>dow;
switch(dow)
{
case 1 : cout<<"\nSunday";
break;
case 2 : cout<<"\nMonday";
break;
case 3 : cout<<"\nTuesday";
break;
case 4 : cout<<"\nWednesday";
break;
case 5 : cout<<"\nThursday";
break;
case 6 : cout<<"\nFriday";
break;
case 7 : cout<<"\nSaturday";
break;
default : cout<<"\nWrong number of day";
break;
}
getch();
}
When the above program is compiled and executed, it will produce the following output:
Important Note:
The switch and if-else both are selection statements and they both let you select an alternative out of given many
alternatives by testing an expression. However, there are some differences in their operations. These are given
below:
o The switch statement differs from the if statement in that switch can only test for equality whereas if can
evaluate a relational or logical expression i.e., multiple conditions.
o The switch statement selects its branches by testing the value of same variable (against a set of
constants) whereas the if-else construction lets you use a series of expressions that may involve
unrelated variables and complex expressions.
o The if-else is more versatile of the two statements. For example, if-else can handle ranges whereas
switch cannot. Each switch case label must be a single value.
o The if-else statement can handle floating-point tests also apart from handling integer and character tests
whereas a switch cannot handle floating-point tests. The case labels of switch must be an integer
(which includes char also).
o The switch case label value must be a constant. So, if two or more variables are to be compared, use if-
else.
o The switch statement is more efficient choice in terms of code used in a situation that supports the
nature of switch operation (testing a value against a set of constants).
Self-Assessment Question
Problem No. 1: Write a program that determines a student’s grade. The program will read three types
of scores (quiz, mid-term, and final scores) and determine the grade based on the following rules:
Problem No. 2: Write a C++ program that prompts the user to input five integer values and find the
greatest value of the five values.
References
https://www.cplusplus.com/doc/tutorial/
https://www.w3schools.com/cpp/