SlideShare a Scribd company logo
Fundamentals of
Computer Programming
Chapter 2
Flow of Control Part I
(Selection Statement)
Chere L. (M.Tech)
Lecturer, SWEG, AASTU
1
Outline
 Introduction to flow control
 Branching flow controls (selection statements)
 One-way selection
 Two-way selection
 Multiple selection
 switch statement
Chapter 2
2
Objectives
 Learn how to use selection flow control
 Learn how to form Boolean expressions and examine
relational and logical operators
 Design and develop program using selection
statements
Chapter 2
3
1. Introduction to flow controls
 Flow of control is the order in which a program statements are
executed (performs actions).
 The term flow control reflects the fact that the currently executing
statement has the control of the CPU and handed over (flow) to
another statement when it’s execution completed.
 Typically, the flow control in a program is sequential, which is the
most common and straight-forward.
 However, usually a program execution is not limited to a sequential
 Programmers can control the order of instruction execution
 Accordingly, most programming language including C++ provides
control structures that serve to specify what has to be done by our
program, when and under which circumstances.
Chapter 2
4
1. Introduction to flow controls (cont’d)
 Generally there are three basic program flow control
1. Sequential – execution of instruction sequentially one after an other
2. Selection/branching – allow alternative actions based up on conditions
that are evaluated at run time.
3. Iteration/loop – allows to execute a statement or group of statements
multiple times
Chapter 2
5
2. Recalling relational and logical operators
 Logical/Boolean expressions are a fundamental part of control
statements and are formed with combinations of two kinds of
operators:
 The expression of both types of operators are evaluated to
true/false
(a) Relational operators (b) Logical operators
Chapter 2
Boolean
expression
example
6
2. Selection/Branching statements
 The selection statements include the following
 if statement ----> One-way selection
 if...else statement ---> Two-way selection
 Conditional operator
 nested if --- else statement
 else if…else statement ---> Multiple selection
 switch statement
Chapter 2
7
2. Selection statements (cont’d)
 One-way selection --- > if statement
Chapter 2
8
if (expression) {
statement (s);
}
next statement(s);
 Expression
 A condition that must evaluated to true/false (i.e. Boolean expression)
 One or more relational expression can be combined with logical operators
 if condition is TRUE the statement(s) or the block that follow the selection
is executed and then next statement(s) get executed.
 Otherwise nothing will be executed and execution continues with the next
statement in the program.
2. Selection statements (cont’d)
 Two-way selection -- > if ….. else statement
Chapter 2
9
 Expression -- similar to that of one-selection statement
 if condition is TRUE the statement1 or block1 is that follow the if selection
is executed and then next statement(s) get executed.
 Otherwise the statement2 or block2 is executed and the execution
continues with the next statement in the program.
if (expression){
statement1 / block1
}
else{
statement2 / block2;
}
next statement(s);
2. Selection statements (cont’d)
 Another Syntax ----- > without the block { }
Chapter 2
 Can be used when there is only one statement
 Not suggested (it causes dangling Else Problem)
Example
if (condition)
<statement_true>;
else
<statement_false>;
if (condition)
<statement_true>;
if (mark >= 50) {
cout << "Congratulation!" << endl;
cout << "Keep it up!" << endl;
}
else {
cout << “Failed, try harder!" << endl;
}
if (work_hrs > 40) {
OT = work_hrs – 40 * 120;
cout << “Over Time " <<OT<<endl;
}
10
2. Selection statements (cont’d)
 Conditional operator instead of if …. else statement
Chapter 2
11
2. Selection statements (cont’d)
 Nested if/else statement
 Refers to using within another selection statement
 Match each else with the last unmatched if
Chapter 2
12
2. Selection statements (cont’d)
 Multiple selection -- > if ….. else if statement
Allows for conditional execution based up on more than two alternatives
Chapter 2
13
 Expression -- similar to that of one-selection statement
 if expression1 returns TRUE the statement1 or block1 is that follow the if
selection is executed and then next statement(s) get executed.
 Otherwise expression2 of each else if part is evaluated and the statement2 or
block2 of the selection that returns TRUE is executed and the execution continues
with the next statement in the program.
if (expression1){
statement1 / block1
}
else if (expression2){
statement2 / block2;
}
. . . . .
else {
statement-N / block-N; }
next statement(s);
2. Selection statements (cont’d)
 if ….. else if Vs. nested if … else statement
 What is the difference between nested if – else and else if selection
structure?
 Evaluate the below two examples
(a) (b)
Chapter 2
14
2. Selection statements (cont’d)
 switch statement
It is similar to if … else if combination, it enables you to test several cases
generated by a given expression
The value of a variable/expression determine where the program will branch
Chapter 2
15
switch (expression)
{
case constant-1:
group of statements 1;
break;
case constant-2:
group of statements 2;
break;
case constant-3:
group of statements 3;
break;
-
-
default:
default group of statements
}
2. Selection statements (cont’d)
 How it works
 The switch expression is evaluated once
 The value of the expression is compared with values of each case
 If there is a match, the associated block of statements is executed
 The statements following the matched case will be executed until
a break statement is reached
 When a break statement is reached, the switch terminates, and the flow
of control jumps to the next line following the switch statement.
 A switch statement can have an optional default case, which usually
appears at the end of the switch.
 The default case can be used for performing a task when none of cases is
true.
 Note
 The expression must evaluated to literal value (integral/character/enum)
 Each case is followed by the value to be compared to and a colon.
 The expression of each case statement in the block must be unique.
 If no break appears, the flow of control will fall through to subsequent
cases until a break is reached.
Chapter 2
16
2. Selection statements (cont’d)
 Example 1:
Chapter 2
17
Additional notes
 The case statement expression
cannot be variable and also range
 Switch can only be used to
compare an expression against
constants
 It is not necessary to include
braces {} surrounding the
statements for each of the cases
 Even if it is usually necessary to
include a break statement at the
end of each case, there are
situations in which it makes sense
to have a case without a break
2. Selection statements (cont’d)
Example 2: switch statement Vs. if…else if statement
Chapter 2
18
Which is selection statement is best fit
for a problem which has range selection?
2. Selection statements (cont’d)
Chapter 2
19
Dangling else problem
 What does it display for x=4?
 The problem is that it displays “4 is an odd number” message for positive even
numbers and zero
 Reason is that, although indentation says the reverse, else belongs to second
(inner) if
 else belongs to the most recent if
 Solution: using brace {} as follow
2. Selection statements (cont’d)
Chapter 2
20
Short-circuit Evaluation
 Evaluate the first (leftmost) Boolean sub-expression.
 If its value is enough to judge about the value of the entire expression, then stop
there. Otherwise continue evaluation towards right.
 Example: if (count != 0 && scores/count < 60)
{
cout<<"low average";
}
 In this example, if the value of count is zero, then first sub-expression becomes
false and the second one is not evaluated.
 In this way, we avoid “division by zero” error (that would cause to stop the
execution of the program)
 Alternative method to avoid division by zero without using short-circuit
evaluation: if (count != 0){
if (scores/count < 60){
cout<<"low average";
}
}
Reading Resources/Materials
Chapter 5 & 6:
 Diane Zak; An Introduction to Programming with C++ [8th
Edition], 2016 Cengage Learning
Chapter 4:
 Gary J. Bronson; C++ For Engineers and Scientists [3rd
edition], Course Technology, Cengage Learning, 2010
Chapter 2 (section 2.4):
 Walter Savitch; Problem Solving With C++ [10th edition],
University of California, San Diego, 2018
Chapter 4:
 P. Deitel , H. Deitel; C++ how to program, [10th edition],
Global Edition (2017)
21
Thank You
For Your Attention!!
22
Ad

More Related Content

Similar to Chapter 2 - Flow of Control Part I.pdf (20)

2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt
ManojKhadilkar1
 
control-statements, control-statements, control statement
control-statements, control-statements, control statementcontrol-statements, control-statements, control statement
control-statements, control-statements, control statement
crrpavankumar
 
CS305PC_C++_UNIT 2.pdf jntuh third semester
CS305PC_C++_UNIT 2.pdf jntuh third semesterCS305PC_C++_UNIT 2.pdf jntuh third semester
CS305PC_C++_UNIT 2.pdf jntuh third semester
VeeraswamyDasari2
 
C++ STATEMENTS
C++ STATEMENTS C++ STATEMENTS
C++ STATEMENTS
Prof Ansari
 
Chapter 4(1)
Chapter 4(1)Chapter 4(1)
Chapter 4(1)
TejaswiB4
 
CSE-1203-Lecture-05-Branching. for c programmepptx
CSE-1203-Lecture-05-Branching. for c programmepptxCSE-1203-Lecture-05-Branching. for c programmepptx
CSE-1203-Lecture-05-Branching. for c programmepptx
MARaihanEmon
 
Control structures
Control structuresControl structures
Control structures
Gehad Enayat
 
Selection statements
Selection statementsSelection statements
Selection statements
Harsh Dabas
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
Thesis Scientist Private Limited
 
Flow of control by deepak lakhlan
Flow of control by deepak lakhlanFlow of control by deepak lakhlan
Flow of control by deepak lakhlan
Deepak Lakhlan
 
[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2
yasir_cesc
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
Soran University
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
Madishetty Prathibha
 
chapter-4 slide.pdf
chapter-4 slide.pdfchapter-4 slide.pdf
chapter-4 slide.pdf
cricketreview
 
C sharp chap4
C sharp chap4C sharp chap4
C sharp chap4
Mukesh Tekwani
 
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlIntroduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
ENGWAU TONNY
 
Ch04
Ch04Ch04
Ch04
Arriz San Juan
 
Unit IV Array in VB.Net.pptx
Unit IV Array in VB.Net.pptxUnit IV Array in VB.Net.pptx
Unit IV Array in VB.Net.pptx
Ujwala Junghare
 
slides03.ppt
slides03.pptslides03.ppt
slides03.ppt
Anjali127411
 
MODULE_2_Operators.pptx
MODULE_2_Operators.pptxMODULE_2_Operators.pptx
MODULE_2_Operators.pptx
VeerannaKotagi1
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt
ManojKhadilkar1
 
control-statements, control-statements, control statement
control-statements, control-statements, control statementcontrol-statements, control-statements, control statement
control-statements, control-statements, control statement
crrpavankumar
 
CS305PC_C++_UNIT 2.pdf jntuh third semester
CS305PC_C++_UNIT 2.pdf jntuh third semesterCS305PC_C++_UNIT 2.pdf jntuh third semester
CS305PC_C++_UNIT 2.pdf jntuh third semester
VeeraswamyDasari2
 
Chapter 4(1)
Chapter 4(1)Chapter 4(1)
Chapter 4(1)
TejaswiB4
 
CSE-1203-Lecture-05-Branching. for c programmepptx
CSE-1203-Lecture-05-Branching. for c programmepptxCSE-1203-Lecture-05-Branching. for c programmepptx
CSE-1203-Lecture-05-Branching. for c programmepptx
MARaihanEmon
 
Control structures
Control structuresControl structures
Control structures
Gehad Enayat
 
Selection statements
Selection statementsSelection statements
Selection statements
Harsh Dabas
 
Flow of control by deepak lakhlan
Flow of control by deepak lakhlanFlow of control by deepak lakhlan
Flow of control by deepak lakhlan
Deepak Lakhlan
 
[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2
yasir_cesc
 
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlIntroduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
ENGWAU TONNY
 
Unit IV Array in VB.Net.pptx
Unit IV Array in VB.Net.pptxUnit IV Array in VB.Net.pptx
Unit IV Array in VB.Net.pptx
Ujwala Junghare
 

More from KirubelWondwoson1 (6)

00 C++ For Engineers and Scientists.pdf
00 C++ For Engineers and Scientists.pdf00 C++ For Engineers and Scientists.pdf
00 C++ For Engineers and Scientists.pdf
KirubelWondwoson1
 
Chapter 4 (Part I) - Array and Strings.pdf
Chapter 4 (Part I) - Array and Strings.pdfChapter 4 (Part I) - Array and Strings.pdf
Chapter 4 (Part I) - Array and Strings.pdf
KirubelWondwoson1
 
Chapter 1 - Basic concepts of programming.pdf
Chapter 1 - Basic concepts of programming.pdfChapter 1 - Basic concepts of programming.pdf
Chapter 1 - Basic concepts of programming.pdf
KirubelWondwoson1
 
Chapter 5 - Modular Programming.pdf
Chapter 5 - Modular Programming.pdfChapter 5 - Modular Programming.pdf
Chapter 5 - Modular Programming.pdf
KirubelWondwoson1
 
Chapter 4 (Part II) - Array and Strings.pdf
Chapter 4 (Part II) - Array and Strings.pdfChapter 4 (Part II) - Array and Strings.pdf
Chapter 4 (Part II) - Array and Strings.pdf
KirubelWondwoson1
 
Chapter 3 - Flow of Control Part II.pdf
Chapter 3  - Flow of Control Part II.pdfChapter 3  - Flow of Control Part II.pdf
Chapter 3 - Flow of Control Part II.pdf
KirubelWondwoson1
 
00 C++ For Engineers and Scientists.pdf
00 C++ For Engineers and Scientists.pdf00 C++ For Engineers and Scientists.pdf
00 C++ For Engineers and Scientists.pdf
KirubelWondwoson1
 
Chapter 4 (Part I) - Array and Strings.pdf
Chapter 4 (Part I) - Array and Strings.pdfChapter 4 (Part I) - Array and Strings.pdf
Chapter 4 (Part I) - Array and Strings.pdf
KirubelWondwoson1
 
Chapter 1 - Basic concepts of programming.pdf
Chapter 1 - Basic concepts of programming.pdfChapter 1 - Basic concepts of programming.pdf
Chapter 1 - Basic concepts of programming.pdf
KirubelWondwoson1
 
Chapter 5 - Modular Programming.pdf
Chapter 5 - Modular Programming.pdfChapter 5 - Modular Programming.pdf
Chapter 5 - Modular Programming.pdf
KirubelWondwoson1
 
Chapter 4 (Part II) - Array and Strings.pdf
Chapter 4 (Part II) - Array and Strings.pdfChapter 4 (Part II) - Array and Strings.pdf
Chapter 4 (Part II) - Array and Strings.pdf
KirubelWondwoson1
 
Chapter 3 - Flow of Control Part II.pdf
Chapter 3  - Flow of Control Part II.pdfChapter 3  - Flow of Control Part II.pdf
Chapter 3 - Flow of Control Part II.pdf
KirubelWondwoson1
 
Ad

Recently uploaded (20)

Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
Ad

Chapter 2 - Flow of Control Part I.pdf

  • 1. Fundamentals of Computer Programming Chapter 2 Flow of Control Part I (Selection Statement) Chere L. (M.Tech) Lecturer, SWEG, AASTU 1
  • 2. Outline  Introduction to flow control  Branching flow controls (selection statements)  One-way selection  Two-way selection  Multiple selection  switch statement Chapter 2 2
  • 3. Objectives  Learn how to use selection flow control  Learn how to form Boolean expressions and examine relational and logical operators  Design and develop program using selection statements Chapter 2 3
  • 4. 1. Introduction to flow controls  Flow of control is the order in which a program statements are executed (performs actions).  The term flow control reflects the fact that the currently executing statement has the control of the CPU and handed over (flow) to another statement when it’s execution completed.  Typically, the flow control in a program is sequential, which is the most common and straight-forward.  However, usually a program execution is not limited to a sequential  Programmers can control the order of instruction execution  Accordingly, most programming language including C++ provides control structures that serve to specify what has to be done by our program, when and under which circumstances. Chapter 2 4
  • 5. 1. Introduction to flow controls (cont’d)  Generally there are three basic program flow control 1. Sequential – execution of instruction sequentially one after an other 2. Selection/branching – allow alternative actions based up on conditions that are evaluated at run time. 3. Iteration/loop – allows to execute a statement or group of statements multiple times Chapter 2 5
  • 6. 2. Recalling relational and logical operators  Logical/Boolean expressions are a fundamental part of control statements and are formed with combinations of two kinds of operators:  The expression of both types of operators are evaluated to true/false (a) Relational operators (b) Logical operators Chapter 2 Boolean expression example 6
  • 7. 2. Selection/Branching statements  The selection statements include the following  if statement ----> One-way selection  if...else statement ---> Two-way selection  Conditional operator  nested if --- else statement  else if…else statement ---> Multiple selection  switch statement Chapter 2 7
  • 8. 2. Selection statements (cont’d)  One-way selection --- > if statement Chapter 2 8 if (expression) { statement (s); } next statement(s);  Expression  A condition that must evaluated to true/false (i.e. Boolean expression)  One or more relational expression can be combined with logical operators  if condition is TRUE the statement(s) or the block that follow the selection is executed and then next statement(s) get executed.  Otherwise nothing will be executed and execution continues with the next statement in the program.
  • 9. 2. Selection statements (cont’d)  Two-way selection -- > if ….. else statement Chapter 2 9  Expression -- similar to that of one-selection statement  if condition is TRUE the statement1 or block1 is that follow the if selection is executed and then next statement(s) get executed.  Otherwise the statement2 or block2 is executed and the execution continues with the next statement in the program. if (expression){ statement1 / block1 } else{ statement2 / block2; } next statement(s);
  • 10. 2. Selection statements (cont’d)  Another Syntax ----- > without the block { } Chapter 2  Can be used when there is only one statement  Not suggested (it causes dangling Else Problem) Example if (condition) <statement_true>; else <statement_false>; if (condition) <statement_true>; if (mark >= 50) { cout << "Congratulation!" << endl; cout << "Keep it up!" << endl; } else { cout << “Failed, try harder!" << endl; } if (work_hrs > 40) { OT = work_hrs – 40 * 120; cout << “Over Time " <<OT<<endl; } 10
  • 11. 2. Selection statements (cont’d)  Conditional operator instead of if …. else statement Chapter 2 11
  • 12. 2. Selection statements (cont’d)  Nested if/else statement  Refers to using within another selection statement  Match each else with the last unmatched if Chapter 2 12
  • 13. 2. Selection statements (cont’d)  Multiple selection -- > if ….. else if statement Allows for conditional execution based up on more than two alternatives Chapter 2 13  Expression -- similar to that of one-selection statement  if expression1 returns TRUE the statement1 or block1 is that follow the if selection is executed and then next statement(s) get executed.  Otherwise expression2 of each else if part is evaluated and the statement2 or block2 of the selection that returns TRUE is executed and the execution continues with the next statement in the program. if (expression1){ statement1 / block1 } else if (expression2){ statement2 / block2; } . . . . . else { statement-N / block-N; } next statement(s);
  • 14. 2. Selection statements (cont’d)  if ….. else if Vs. nested if … else statement  What is the difference between nested if – else and else if selection structure?  Evaluate the below two examples (a) (b) Chapter 2 14
  • 15. 2. Selection statements (cont’d)  switch statement It is similar to if … else if combination, it enables you to test several cases generated by a given expression The value of a variable/expression determine where the program will branch Chapter 2 15 switch (expression) { case constant-1: group of statements 1; break; case constant-2: group of statements 2; break; case constant-3: group of statements 3; break; - - default: default group of statements }
  • 16. 2. Selection statements (cont’d)  How it works  The switch expression is evaluated once  The value of the expression is compared with values of each case  If there is a match, the associated block of statements is executed  The statements following the matched case will be executed until a break statement is reached  When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.  A switch statement can have an optional default case, which usually appears at the end of the switch.  The default case can be used for performing a task when none of cases is true.  Note  The expression must evaluated to literal value (integral/character/enum)  Each case is followed by the value to be compared to and a colon.  The expression of each case statement in the block must be unique.  If no break appears, the flow of control will fall through to subsequent cases until a break is reached. Chapter 2 16
  • 17. 2. Selection statements (cont’d)  Example 1: Chapter 2 17 Additional notes  The case statement expression cannot be variable and also range  Switch can only be used to compare an expression against constants  It is not necessary to include braces {} surrounding the statements for each of the cases  Even if it is usually necessary to include a break statement at the end of each case, there are situations in which it makes sense to have a case without a break
  • 18. 2. Selection statements (cont’d) Example 2: switch statement Vs. if…else if statement Chapter 2 18 Which is selection statement is best fit for a problem which has range selection?
  • 19. 2. Selection statements (cont’d) Chapter 2 19 Dangling else problem  What does it display for x=4?  The problem is that it displays “4 is an odd number” message for positive even numbers and zero  Reason is that, although indentation says the reverse, else belongs to second (inner) if  else belongs to the most recent if  Solution: using brace {} as follow
  • 20. 2. Selection statements (cont’d) Chapter 2 20 Short-circuit Evaluation  Evaluate the first (leftmost) Boolean sub-expression.  If its value is enough to judge about the value of the entire expression, then stop there. Otherwise continue evaluation towards right.  Example: if (count != 0 && scores/count < 60) { cout<<"low average"; }  In this example, if the value of count is zero, then first sub-expression becomes false and the second one is not evaluated.  In this way, we avoid “division by zero” error (that would cause to stop the execution of the program)  Alternative method to avoid division by zero without using short-circuit evaluation: if (count != 0){ if (scores/count < 60){ cout<<"low average"; } }
  • 21. Reading Resources/Materials Chapter 5 & 6:  Diane Zak; An Introduction to Programming with C++ [8th Edition], 2016 Cengage Learning Chapter 4:  Gary J. Bronson; C++ For Engineers and Scientists [3rd edition], Course Technology, Cengage Learning, 2010 Chapter 2 (section 2.4):  Walter Savitch; Problem Solving With C++ [10th edition], University of California, San Diego, 2018 Chapter 4:  P. Deitel , H. Deitel; C++ how to program, [10th edition], Global Edition (2017) 21
  • 22. Thank You For Your Attention!! 22
  翻译: