Chapter 4 - Control Statements
Chapter 4 - Control Statements
C++
Control Statements
Statement 1
Statement 2
Statement 3
05/27/2024 C++ Control Statements 8
SELECTION
Selection
• The Selection construct means the execution
of statement(s) depending upon a condition-
test.
• If a condition evaluates to true, a course-of-
action (a set of statements) is followed
otherwise another course -of-action (a
different set of statements).
• This construct(selection construct) is also
called decision construct because it helps in
making decision about which set-of-
statements is to be executed.
05/27/2024 C++ Control Statements 9
THE SELECTION CONSTRUCT
Statement 1
Statement 2
Condition ? False
True
Statement 1
The Loop Body
Statement 2
Example of if-else
if (basic>8000)
{
total_da=(40*basic)/100
gross=total_da + basic
}
else
{
total_da=(40*basic)/100
05/27/2024 gross=total_da + basic
C++ Control Statements 15
}
NESTED IFs
A nested if is an if that has another if in its body
or in its else body. The nested if can have one of
the following three forms.
Form 1 :
if (expression 1)
{
if (expression 2)
statement 1
else
statement 2
}
else
body of else
05/27/2024 C++ Control Statements 16
Form 2:
if (expression 1)
{
if (expression 2)
statement 1
else
statement 2
……….
}
else
{
if (expression 2)
statement 1
else
statement 2
……….
05/27/2024 } C++ Control Statements 17
Form 3:
if (expression 1)
{
body of if
}
else
{
if (expression 2)
statement 1
else
statement 2
……….
}
#include<iostream.h>
int main()
{
char ch;
float a,b, result;
cout<<"Enter the two values" ;
cin>>a>>b;
cout<<"Enter the Operator [ + - * / ] : ";
cin>>ch;
#include<iostream.h>
void main()
{ case 6 : cout<<“\n Friday”;
int dow; break;
cout<<“Enter the number of day”; case 7 : cout<<“\nSaturday”;
cin>>dow; break;
switch(dow) default :cout<<“Wrong
number of day”
{ break;
case 1 : cout<<“\n Sunday”; }
break; }
case 2 : cout<<“\n Monday”;
break; Out put
case 3 : cout<<“\n Tuesday”; Enter the number of day 5
break; Thursday
case 4 : cout<<“\n Wednesday”;
break;
case 5 : cout<<“\n Thursday”;
05/27/2024 C++ Control Statements 24
break;
THE switch Vs if-else-if
• Both let you select an alternative out of many
alternatives by testing an expression.
• However there are some differences in their
operation and they are,
1. Switch can only test for equality where as if can
evaluate a relational or logical expressions
i.e multiple conditions.
2. The switch statement selects its branches by
testing the value of same variable (against the
set of constants) where as the if else
construction lets you to use a series of
expressions that may involve unrelated
variables and complex expressions.
05/27/2024 C++ Control Statements 25
…continued
3. The if-else-if is more versatile/multipurpose of
two statements where as switch cannot. Each
switch case label must be a single value.
int a=0;
start :
cout<<++a<<“, ”;
if(a<5)
{
goto start;
}
else
{
cout<<“STOP”;
}
05/27/2024 C++ Control Statements 47
break STATEMENT
The break statement enables the program to
skip over the part of the code.
A break statement terminates the smallest
enclosing while, do-while, for or switch
statement.
do {
statement
if(val>2000)
break;
…..
statement;
} while (test expression);
statement 3;