Docsity

C++ Programming: Understanding Selection Control Statements - if, if-else, and switch, Schemes and Mind Maps of Computer science

Toggle sidebar
bg1
MODULE No. 5: SELECTION CONTROL STATEMENT
EE122 – Computer Programming
ISABELA STATE UNIVERSITY – ILAGAN CAMPUS
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:
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download C++ Programming: Understanding Selection Control Statements - if, if-else, and switch and more Schemes and Mind Maps Computer science in PDF only on Docsity!

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

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:

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;

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:

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

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

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.

EE122 – Computer Programming

Page | 11

References

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