SlideSet 14
SlideSet 14
SlideSet 14
3
Conditional Control Flow
4
Conditional Control Flow
In conditional logic:
5
Types of Conditional Control Flow
6
One-Way Selection
• In one-way selection there is one condition and only one possible choice
available either we choose it or not.
• If the condition is satisfied we choose it, and do not choose it, if the
condition is not satisfied.
• Like, the boss checks the experience of an employee and adds the bonus, if
the experience is more than 2 years.
• Similarly, shopkeeper gives you a discount of 10% if you make the purchase
of more than Rs. 5000.
• Both of these examples involve one-way selection.
7
One-Way Selection
In one-way selection:
8
One-Way Selection – Flow Chart
Following is the flow of execution of one-way selection:
9
One-Way Selection - Example
Problem Statement 1: If the number is positive then display it.
10
One-Way Selection - Example
Problem Statement 2: If the number is multiple of 5 then add 1 to it and
display the resultant number.
11
Two-Way Selection
12
Two-Way Selection
In two-way selection:
13
Two-Way Selection – Flow Chart
Following is the flow of execution of two-way selection:
14
Two-Way Selection - Example
Problem Statement 1: If the number is even, display “Number is even” else
display “Number is odd”.
15
Two-Way Selection - Example
Problem Statement 2: If the number is even, make it odd else double it and
finally display the number.
16
Multi-Way Selection
• Multi-way selection is series of two-way selections.
• In multi-way selection there are multiple conditions and multiple possible choices
available, we choose any one of them.
• Either all the conditions will not be satisfied or at maximum any one of the
condition can be satisfied. But at the end we can only choose one of the choice.
• Like, a person checks the age of the three persons to determine who is elder
amongst them? If the age of first person, is greater than second and third, then
person 1 is elder; otherwise if the age of second person, is greater than first and
third, then person 2 is elder; otherwise person 3 is elder.
• It is an example involving multi-way selection.
17
Multi-Way Selection
In multi-way selection:
• If the 1st condition is satisfied (true), the 1st set of statements is executed.
• If the 1st condition is not satisfied (false), then we check for 2nd condition.
• If the 2nd condition is satisfied (true), the 2nd set of statements is executed.
• If the 2nd condition is not satisfied (false), then we check for 3rd condition.
• And the process continues up to nth condition.
• If the nth condition is satisfied (true), the nth set of statements is executed.
• If the nth condition is not satisfied (false), then (n+1)th set of statements is executed.
18
Multi-Way Selection – Flow Chart
Following is the flow of execution of multi-way (4-way) selection:
19
Multi-Way Selection – Example
Problem Statement: Compare three numbers and display which number is
largest among them.
20
Choice-Way Selection
21
Choice-Way Selection
• Like, a teacher gives remarks to a student according to the marks he/she
gets out of 5. He sets the remarks criteria as:
Marks Remarks
0 Very Bad
1 Not Satisfactory
2 Slightly Satisfactory
3 Satisfactory
4 Good
5 Excellent
22
Choice-Way Selection
In choice-way selection:
• We make a choice.
• If choice matches with 1st option, the 1st statement set is executed.
• If choice matches with 2nd option, the 2nd statement set is executed.
• If choice matches with 3rd option, the 3rd statement set is executed.
• And the process continues up to nth option.
• If choice matches with nth option, the nth statement set is executed.
23
Choice-Way Selection – Flow Chart
Following is the flow of execution of choice-way (4-way) selection:
24
Choice-Way Selection – Example
Problem Statement: Give remarks to the student according to the marks
he/she gets out of 5 in the sessional test.
25
Conditional Control Structures in C++
• The statements that are needed to be executed conditionally are placed with
in the conditional structures and one or more conditions are specified.
26
if Statement in C++
27
if Statement – Syntax
if ( condition ) if ( condition )
{ statement ;
statement set ;
}
28
if Statement – Syntax
29
if Statement – Syntax
30
if Statement – Flow Chart
31
if Statement - Example
Problem Statement 1: If the number is positive then display it.
32
if Statement - Example
Problem Statement 2: If the number is multiple of 5 then add 1 to it and
display the resultant number.
33
If-else Statement in C++
34
If-else Statement – Syntax
if ( condition ) if ( condition )
{ statement set 1 ;
statement set 1 ; else
} statement set 2 ;
else
{
statement set 2 ;
}
35
If-else Statement – Syntax
36
If-else Statement – Syntax
37
If-else Statement – Syntax
38
If-else Statement – Flow Chart
39
if-else Statement - Example
Problem Statement 1: If the number is even, display “Number is even” else
display “Number is odd”.
40
if-else Statement - Example
Problem Statement 2: If the number is even, make it odd else double it and
finally display the number.
41
if-else-if Statement in C++
• If-else-if statement implements multi-way selection logic.
• It executes the statement(s) on the basis of multiple conditions (atleast two).
• If the first condition is true, it executes if block.
• If the first condition is false, it checks second condition.
• If the second condition is true, it executes else-if block 1.
• If the second condition is false, it checks third condition.
• And the process continues up to nth condition.
• If the nth condition is true, it executes else-if block n.
• If the nth condition is false, it executes else block.
42
if-else-if Statement – Syntax
if ( condition1 ) if ( condition1 )
{
statement set 1 ; statement set 1 ;
} else if ( condition2 )
else if ( condition2 )
{ statement set 2 ;
statement set 2 ; else if ( condition3 )
}
else if ( condition3 ) statement set 3 ;
{ else
statement set 3 ;
} statement set 4 ;
else
{
statement set 4 ;
}
43
if-else-if Statement – Syntax
44
if-else-if Statement – Flow Chart
45
if-else-if Statement - Example
Problem Statement: Compare three numbers and display which number is
largest among them.
46
switch Statement in C++
47
switch Statement – Syntax
switch ( expression )
{
case 1 :
statement set 1 ;
break ;
case 2 :
statement set 2 ;
break ;
case 3 :
statement set 3 ;
break ;
default :
statement set 4 ;
}
48
switch Statement – Syntax
49
switch Statement – Syntax
50
switch Statement – Flow Chart
51
switch Statement - Example
Problem Statement: Give remarks to the student according to the marks
he/she gets out of 5 in the sessional test.
52
Decision to Choose Between Conditional Control Structure
53
Program Examples
if, if-else, if-else-if and switch statements
54
Program Example 01
Problem Statement:
Write a program in C++ that asks you the marks obtained in six different
subjects. The program displays the total obtained marks, percentage and the
grade as shown in following distribution.
Percentage Grade
>=85 A+
>=80 and <85 A
>=75 and <80 B+
>=70 and <75 B
>=65 and <70 C+
>=60 and <65 C
>=55 and <60 D+
>=50 and <55 D
<50 F
55
Program Example 01
56
Program Example 01
57
Program Example 01
58
Program Example 02
Problem Statement:
There are two circular grounds Ground-A and Ground-B. Ground-A is having
diameter of 15 meters and Ground-B is having diameter of 20 meters. Mohsin
is running in Ground-A and Neetesh is running in Ground-B. Write a computer
program that asks the user to input the time taken, in seconds, to complete one
compete round of the ground by both the friends and displays who is running
faster.
59
Program Example 02
60
Program Example 02
61
Program Example 02
62
Program Example 03
Problem Statement:
Write a computer program that asks the user to enter three angles of a
triangle. The program displays whether the triangle is right-angle, acute-angle
or obtuse-angle.
63
Program Example 03
64
Program Example 03
65
Program Example 03
66
Program Example 04
Problem Statement:
Write a computer program that asks the user to enter date of birth and month
of birth. The program should display the zodiac star.
67
Program Example 04
68
Program Example 04
69
Program Example 04
70
Program Example 05
Problem Statement:
Write a computer program that asks the user to enter any character. The
program should whether the entered character is a vowel or a consonant.
71
Program Example 05
72
Program Example 05
73
Program Example 05
74