0% found this document useful (0 votes)
2 views

Module-5-Selection-Control-Statement

This document covers selection control statements in C++, including if, if-else, nested if, and switch statements. It explains their syntax, usage, and provides examples to demonstrate how these statements can be used to control the flow of a program based on conditions. Additionally, it highlights the differences between if statements and switch statements, along with self-assessment questions for practice.

Uploaded by

vq201161
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Module-5-Selection-Control-Statement

This document covers selection control statements in C++, including if, if-else, nested if, and switch statements. It explains their syntax, usage, and provides examples to demonstrate how these statements can be used to control the flow of a program based on conditions. Additionally, it highlights the differences between if statements and switch statements, along with self-assessment questions for practice.

Uploaded by

vq201161
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

MODULE No.

5: SELECTION CONTROL STATEMENT


EE122 – Computer Programming Page | 1

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.

Here is the syntax or general form of the if statement:

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:

if (ch == ' ')


{
spaces++;
}

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:

ISABELA STATE UNIVERSITY – ILAGAN CAMPUS


MODULE No. 5: SELECTION CONTROL STATEMENT
EE122 – Computer Programming Page | 2

char ch;
cin >> ch;

if (ch == ' ')


{
cout << "You entered a space" << "\n" ;
}
If (ch >= '0' && ch <= '9')
{
cout << "You entered a digit" << "\n";
}

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.

Here is the sample run of the above C++ program:

Example No 3:

/* C++ Selection Statements - C++ if Statement */

#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();
}

Here is the sample run of this C++ program:

ISABELA STATE UNIVERSITY – ILAGAN CAMPUS


MODULE No. 5: SELECTION CONTROL STATEMENT
EE122 – Computer Programming Page | 3

Example No. 4:

/* C++ Selection Statements - C++ if Statement */

#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.

Here is another output, if 2nd number is biggest.

Now, here is the last output, if 1st number is biggest:

ISABELA STATE UNIVERSITY – ILAGAN CAMPUS


MODULE No. 5: SELECTION CONTROL STATEMENT
EE122 – Computer Programming Page | 4

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:

/* C++ Selection Statements - C++ if-else Statement */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num;
cout<<"Enter a number: ";
cin>>num;

ISABELA STATE UNIVERSITY – ILAGAN CAMPUS


MODULE No. 5: SELECTION CONTROL STATEMENT
EE122 – Computer Programming Page | 5

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:

/* C++ Selection Statements - C++ if-else Statement */

#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:

ISABELA STATE UNIVERSITY – ILAGAN CAMPUS


MODULE No. 5: SELECTION CONTROL STATEMENT
EE122 – Computer Programming Page | 6

Important - Placement of semicolon is also important. In an if statement, DO NOT put semicolon in the line
having test condition, such as

if (y > max) ; // DO NOT do this

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;
}

This is the second form of nested ifs statement

if(expression 1)
{
body-of-if;
else
{
if(expression 2)
{
statement 1;
}
else
{
statement 2;

ISABELA STATE UNIVERSITY – ILAGAN CAMPUS


MODULE No. 5: SELECTION CONTROL STATEMENT
EE122 – Computer Programming Page | 7

}
}
}

Now, this is the third form of nested ifs statement

if(expression 1)
{
if(expression 2)
{
statement 1;
}
else
{
statement 2;
}
}
else
{
if(expression 3)
{
statement 3;
}
else
{
statement 4;
}
}

Example No. 1:

/* C++ Selection Statements - C++ nested ifs Statement */

#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..!!";

ISABELA STATE UNIVERSITY – ILAGAN CAMPUS


MODULE No. 5: SELECTION CONTROL STATEMENT
EE122 – Computer Programming Page | 8

}
}
if(num<=100)
{
cout<<"Its too low..!!";
}
getch();
}

Here are the sample runs of the above C++ program:

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;
}

ISABELA STATE UNIVERSITY – ILAGAN CAMPUS


MODULE No. 5: SELECTION CONTROL STATEMENT
EE122 – Computer Programming Page | 9

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:

/* C++ Selection Statement - C++ switch Statement */

#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:

ISABELA STATE UNIVERSITY – ILAGAN CAMPUS


MODULE No. 5: SELECTION CONTROL STATEMENT
EE122 – Computer Programming Page | 10

Here is another sample output of the above C++ program :

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

1. Discuss the difference between if statement and switch statement


2. Solve the following problems in C++ using your preferred control statement

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:

o if the average score =90% =>grade=A


o if the average score >= 70% and <90% => grade=B
o if the average score>=50% and <70% =>grade=C
o if the average score<50% =>grade=F

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.

ISABELA STATE UNIVERSITY – ILAGAN CAMPUS


MODULE No. 5: SELECTION CONTROL STATEMENT
EE122 – Computer Programming Page | 11

References

 https://www.cplusplus.com/doc/tutorial/
 https://www.w3schools.com/cpp/

ISABELA STATE UNIVERSITY – ILAGAN CAMPUS

You might also like