0% found this document useful (0 votes)
45 views28 pages

Lecture 6

The document discusses different types of statements in C++ including sequential, selection, and loop statements. It covers conditional statements like if, if-else, and if-else-if statements. It also covers the switch statement which provides an alternative way for multiple selection. Examples are provided for each type of statement.

Uploaded by

Mahad Qasim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views28 pages

Lecture 6

The document discusses different types of statements in C++ including sequential, selection, and loop statements. It covers conditional statements like if, if-else, and if-else-if statements. It also covers the switch statement which provides an alternative way for multiple selection. Examples are provided for each type of statement.

Uploaded by

Mahad Qasim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Lecture 6

STATEMENTS
TRANSFER OF CONTROL

 The process of transferring control from one


location of program to another;
 Two types of transfer of control:

 Unconditional Transfer of Control


 Conditional Transfer of Control.
TYPES OF STATEMENT

 Three types of statement:


 Sequential statement: in which commands are
executed sequentially.
 Selection Statement: in which one choice is
selected on the bases of some known condition.
 Loop Statement: Which repeat some statement to
satisfy certain condition.
 Types of Statements
 Conditional Statement
 The IF….Statement
 The IF….Else Statement
 The Nested IF…..Statement

 The Switch Statement.


CONDITIONAL STATEMENTS
 Provide
 Ability to control whether a statement list is
executed

 Two constructs
 If statement
 if

 if-else

 Switch statement
THE BASIC IF STATEMENT

 Syntax
if (Expression)
Action; Expression
false
 If the Expression is true true
then execute Action

 Action is either a single Action


statement or a group of
statements within braces
THE IF…… STATEMENT
 Enables to test for a condition to
perform some action:
 For Single Statement:
 if (condition)
 Statement;

 For multiple Statement:


 if (condition)

 {
 Statement1;
 Statement2;
.
.
.
 }
 Program to check whether the number is divisible
by 3.
main()
{
int n;
cout<< “Enter a number”;
cin>> n;
If (n%3 ==0)
{
Cout<<“Number is divisible”;
}
}
EXAMPLE

 Program to take two values from keyboard and


then to print the largest value, using if
statement.
WHAT IS THE OUTPUT?

int m = 5;
int n = 10;

if (m < n)
{
++m;
++n;
}
cout << " m = " << m << " n = " n << endl;
}
THE IF-ELSE STATEMENT
 Syntax
if (Expression)
Action1
else
Action2 Expression
 If Expression is true then execute
Action1 otherwise execute Action2 false
true
if (v == 0)
{
cout << "v is 0"; Action1 Action2
}
else
{
cout << "v is not 0";
}
IF…….ELSE STATEMENT
 Use for making two way
decision.
 For Single Statement:
 if (condition)
 Statement-1;
Else
 Statement-2;
 For multiple Statement:
 if (condition)
 {
 Statement1;
 Statement2;
}
Else
 {
 Statement1;
 Statement2;
}
FINDING THE MAX
int Value1;
int Value2;
int Max;

cout << "Enter first integers: ";


cin >> Value1;
cout << "Enter second integers: ";
cin >> Value2;

if (Value1 < Value2)


{
Max = Value2;
}
else
{
Max = Value1;
}
cout << "Maximum of inputs is: " << Max << endl;
MULTIPLE SELECTION

 It is often the case that depending upon the


value of an expression we want to perform a
particular action
 Two major ways of accomplishing this choice
 if-else-if statement
 if-else statements “glued” together

 Switch statement
 An advanced construct
MULTIPLE SELECTION

 Sometimes it is necessary to branch in more


than two directions.
 We do this via multiple selection.

 The multiple selection mechanism in C++ is the


if-else-if and switch statement.
MULTIPLE SELECTION WITH IF
if (day == 0 ) (continued)
{
cout<< “Sunday”; if (day == 4)
} {
cout<< “Thursday” ;
if (day == 1 ) }
{ if (day == 5)
cout<< “Monday” ; {
} cout<< “Friday” ;
}
if (day == 2)
if (day == 6)
{ {
cout<< “Tuesday” ; cout<< “Saturday” ;
} }
if (day == 3) if ((day < 0) || (day > 6))
{
{ cout<<“Error - invalid day.\n” ;
cout<< “Wednesday”; }
}
MULTIPLE SELECTION WITH IF-ELSE
if (day == 0 )
{
cout<< “Sunday” ;
}
else if (day == 1 )
{
cout<< “Monday” ;
}
else if (day == 2)
{ This if-else structure is more
cout<< “Tuesday” ;
}
else if (day == 3)
efficient than the corresponding
{

}
cout<< “Wednesday”; if structure. Why?
else if (day == 4)
{
cout<< “Thursday”;
}
else if (day == 5)
{
cout<< “Friday”;
}
else if (day = 6)
{
cout<< “Saturday”;
}
else {
cout<< “Error - invalid
day.\n”;
}
AN IF-ELSE-IF STATEMENT
if ( number < 0 )
{
cout << number << " is negative" << endl;
}
else if (number > 0 )
{
cout << number << " is positive" << endl;
}
else
{
cout << number << " is zero" << endl;
}
 Write a program that inputs test score of a
student and display his grade on the following
criteria:
Test Score Grade
>= 90 A
80 – 89 B
70 – 79 C
60 – 69 D
Below 60 F
 Write a program that input a salary. If the salary
is 20000 or more, it deducts 7% of salary. If
the salary is 10000 or more but less than
20000, it deducts 1000 from the salary. If
salary is less than 10000, It deduct nothing.
THE SWITCH MULTIPLE-SELECTION STRUCTURE
switch ( integer expression )
{
case constant1 :
statement(s)
break ;
case constant2 :
statement(s)
break ;

...
default: :
statement(s)
break ;
}
A SWITCH STATEMENT
Char ch;
Cout<<“Enter any character”;
Cin>>ch;
switch (ch)
{
case 'a': case 'A':
cout << ch << " is a vowel" << endl;
break;
case 'e': case 'E':
cout << ch << " is a vowel" << endl;
break;
.
.
.
case 'u': case 'U':
cout << ch << " is a vowel" << endl;
break;
default:
cout << ch << " is not a vowel" << endl;
}
 Write a program that input number of day of
week and displays the name of the day. For
example if user enters 1, it displays “Monday”
and so on.
Int n;
Cout<<“Enter any number”;
Cin>>n;
switch (n)
{
case 1:
cout << “Monday" << endl;
break;
case 2:
cout << “Tuesday” << endl;
break;
.
.
.
case 7:
cout << Sunday" << endl;
break;
default:
cout << “ Invalid Number”;
}
 Write a program that inputs grade of a student
and display his test score on the following
criteria:
Test Score Grade Test Score
>= 90 A >= 90
80 – 89 B 80 – 89
70 – 79 C 70 – 79
60 – 69 D 60 – 69
Below 60 F Below 60
THE SWITCH STATEMENT
switch (grade)
{
case ‘A’:
cout << “Grade is between 90 & 100”;
break;
case ‘B’:
cout << “Grade is between 80 & 89”;
break;
case ‘C’:
cout << “Grade is between 70 & 79”;
break;
case ‘D’:
cout << “Grade is between 60 & 69”;
break;
case ‘F’:
cout << “Grade is between 0 & 59”;
break;
default:
cout << “You entered an invalid grade.”;
}

You might also like