Jan 2023
FUNDAMENTALS OF COMPUTER
PROGRAMMING
Chere L. (M. Tech)
Lecturer, SWEG, AASTU
Using C++
Jan 2023
PROGRAM FLOW CONTROLS
(Selection Statements)
Outline
▪ Introduction to flow control
▪ Branching flow controls
✓ One-way selection
✓ Two-way selection
✓ Multiple selections
✓ switch statement
CHAPTER FOUR
Jan 2023
PROGRAM FLOW CONTROLS
(Loop Statements)
Outline
▪ Introduction to iterative flow control
▪ Iterative/looping flow controls Stmts
✓ for loop
✓ while loop
✓ do . . . while loop
▪ Jumping statements
✓ break, continue, goto
▪ Program termination statements
✓ return, exit, abort
CHAPTER FOUR
Jan 2023
Objectives
At the end of this section, you able to
▪ Identify and use selection flow controls and iterative flow controls.
▪ Form and evaluate Boolean expressions
▪ Examine the relational and logical operators
▪ Determine the use case of the various selection, looping, jumping,
and program termination statements
▪ Design and develop a program using selection and loop statements
and the other flow control statements.
Jan 2023
4.1 Introduction to Flow Control
Jan 2023
What is flow control?
▪ It refers to the order in which a program statements (instructions) are
executed (performs actions).
▪ The term reflects the fact that the currently executing statement has
control of the CPU and is handed over (flow) to another statement when
its execution is completed.
Jan 2023
Cont’d
The natural flow control of program
▪ Typically, the flow control in a program is
sequential, which is the most common and
straightforward.
▪ However, usually a program execution is not
limited to a sequential
Jan 2023
Cont’d
Can the programmer
alter the normal
order of program
execution?
▪ Yes, 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.
Jan 2023
The Basic Program Flow Controls
▪ Generally there are three basic program flow controls
Execution of instructions
sequentially one after
another
allow alternative actions
based upon conditions that
are evaluated at run time
allows to execute a
statement or group of
statements multiple times
Jan 2023
Types of Selection Statements
✓ One-way selection --- if Stmts
✓ Two-way selection --- if . . . else Stmts
✓ Multiple selection
➢ if . . . else if Stmts
➢ switch Stmts
➢ nested selection Stmts
Types of Looping Statements
✓ while loop
✓ for loop
✓ do...while loop
✓ Nested looping
Also group as
➢ Pre-test loops Vs. Post-test loops
➢ Count-controlled Vs. Event-controlled
Jan 2023
Selection Statements
Jan 2023
Identify the type of selection statements and write their syntax
1
2
if (expression) {
statement (s);
}
next statement(s);
▪ if the condition (Boolean expression)
is TRUE (evaluated to 1), then
statement(s) or the block that follows
the selection is executed and then the
next statement(s) gets executed.
▪ Otherwise nothing will be executed and
execution continues with the next
statement in the program.
Expression/condition
✓ It is a Boolean expression
✓ A condition that must be evaluated to
be true/false
✓ two or more relational expressions can
be combined with logical operators
if (expression){
statement1 / block1
}else{
statement2 / block2;
}
next statement(s);
Jan 2023
Cont’d
3
if (expression1){
statement1 / block1
}
else if (expression2){
statement2 / block2;
}
. . . . .
else {
statement-N / block-N;
}
next statement(s);
Jan 2023
Cont. . .
4
Jan 2023
Nested Selection Stmts
5
if (expression1){
if (expression1.1){
statement1.1 / block1.1;
}
statement1.2 / block1.2 //optional
}
else {
if (expression2){
statement2 / block2;
}
else if (expression3){
statement3 / block3;
}
else
statement4 / block4
}
next statement(s);
Jan 2023
Looping Statements
Jan 2023
Part of looping statements
▪ Initialization Expression(s)
✓ Initialize(s) the loop
✓ Variables at the beginning of the loop.
▪ Test Expression
✓ Decides whether the loop will be executed (if the test expression is true) or
not (if the test expression is false).
▪ Update Expression(s)
✓ Update(s) the values of loop variables after every iteration of the loop.
▪ The Body-of-the-Loop
✓ Contains statements to be executed repeatedly.
Jan 2023
Identify the type of looping statements and write their syntax
6
while (repetition condition) {
statement (s);
}
next statement(s);
Jan 2023
Cont’d
7
statement
true
condition
evaluated
false
increment
initialization
for ( initialization ; condition ; increment )
{
statement;
}
Theinitialization is
executed once before
the loopbegins
✓ It is similar to the while loop
condition part
✓ The statement is executed until
the conditionbecomes false
The incrementportionis
executed at
the end of eachiteration
Jan 2023
Cont’d
8
do {
statement (s);
} while (repetition condition)
next statement(s);
Jan 2023
Nested Loops
9
Jan 2023
Reading Resources/Materials
eBooks – selection statements
▪ 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, Global Edition] (2017)
Jan 2023
Reading Resources/Materials
eBooks – loop statements
▪ Chapter 7 & 8: Diane Zak; An Introduction to Programming with C++ [8th Edition],
2016 Cengage Learning
▪ Chapter 5: 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 & 5: P. Deitel , H. Deitel; C++ how to program, [10th, Global Ed.] (2017)
Jan 2023
Reading Resources/Materials
Online Materials
▪ https://www.w3schools.com/cpp/default.asp
▪ https://www.javatpoint.com/cpp-tutorial
▪ https://www.programiz.com/cpp-programming
▪ https://www.hackerrank.com/domains/cpp
▪ https://cplusplus.com/doc/tutorial/
Jan 2023
Thank You
For Your Attention!!

Fundamentals of Computer Programming Summary of Flow Controls

  • 1.
    Jan 2023 FUNDAMENTALS OFCOMPUTER PROGRAMMING Chere L. (M. Tech) Lecturer, SWEG, AASTU Using C++
  • 2.
    Jan 2023 PROGRAM FLOWCONTROLS (Selection Statements) Outline ▪ Introduction to flow control ▪ Branching flow controls ✓ One-way selection ✓ Two-way selection ✓ Multiple selections ✓ switch statement CHAPTER FOUR
  • 3.
    Jan 2023 PROGRAM FLOWCONTROLS (Loop Statements) Outline ▪ Introduction to iterative flow control ▪ Iterative/looping flow controls Stmts ✓ for loop ✓ while loop ✓ do . . . while loop ▪ Jumping statements ✓ break, continue, goto ▪ Program termination statements ✓ return, exit, abort CHAPTER FOUR
  • 4.
    Jan 2023 Objectives At theend of this section, you able to ▪ Identify and use selection flow controls and iterative flow controls. ▪ Form and evaluate Boolean expressions ▪ Examine the relational and logical operators ▪ Determine the use case of the various selection, looping, jumping, and program termination statements ▪ Design and develop a program using selection and loop statements and the other flow control statements.
  • 5.
  • 6.
    Jan 2023 What isflow control? ▪ It refers to the order in which a program statements (instructions) are executed (performs actions). ▪ The term reflects the fact that the currently executing statement has control of the CPU and is handed over (flow) to another statement when its execution is completed.
  • 7.
    Jan 2023 Cont’d The naturalflow control of program ▪ Typically, the flow control in a program is sequential, which is the most common and straightforward. ▪ However, usually a program execution is not limited to a sequential
  • 8.
    Jan 2023 Cont’d Can theprogrammer alter the normal order of program execution? ▪ Yes, 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.
  • 9.
    Jan 2023 The BasicProgram Flow Controls ▪ Generally there are three basic program flow controls Execution of instructions sequentially one after another allow alternative actions based upon conditions that are evaluated at run time allows to execute a statement or group of statements multiple times
  • 10.
    Jan 2023 Types ofSelection Statements ✓ One-way selection --- if Stmts ✓ Two-way selection --- if . . . else Stmts ✓ Multiple selection ➢ if . . . else if Stmts ➢ switch Stmts ➢ nested selection Stmts Types of Looping Statements ✓ while loop ✓ for loop ✓ do...while loop ✓ Nested looping Also group as ➢ Pre-test loops Vs. Post-test loops ➢ Count-controlled Vs. Event-controlled
  • 11.
  • 12.
    Jan 2023 Identify thetype of selection statements and write their syntax 1 2 if (expression) { statement (s); } next statement(s); ▪ if the condition (Boolean expression) is TRUE (evaluated to 1), then statement(s) or the block that follows the selection is executed and then the next statement(s) gets executed. ▪ Otherwise nothing will be executed and execution continues with the next statement in the program. Expression/condition ✓ It is a Boolean expression ✓ A condition that must be evaluated to be true/false ✓ two or more relational expressions can be combined with logical operators if (expression){ statement1 / block1 }else{ statement2 / block2; } next statement(s);
  • 13.
    Jan 2023 Cont’d 3 if (expression1){ statement1/ block1 } else if (expression2){ statement2 / block2; } . . . . . else { statement-N / block-N; } next statement(s);
  • 14.
  • 15.
    Jan 2023 Nested SelectionStmts 5 if (expression1){ if (expression1.1){ statement1.1 / block1.1; } statement1.2 / block1.2 //optional } else { if (expression2){ statement2 / block2; } else if (expression3){ statement3 / block3; } else statement4 / block4 } next statement(s);
  • 16.
  • 17.
    Jan 2023 Part oflooping statements ▪ Initialization Expression(s) ✓ Initialize(s) the loop ✓ Variables at the beginning of the loop. ▪ Test Expression ✓ Decides whether the loop will be executed (if the test expression is true) or not (if the test expression is false). ▪ Update Expression(s) ✓ Update(s) the values of loop variables after every iteration of the loop. ▪ The Body-of-the-Loop ✓ Contains statements to be executed repeatedly.
  • 18.
    Jan 2023 Identify thetype of looping statements and write their syntax 6 while (repetition condition) { statement (s); } next statement(s);
  • 19.
    Jan 2023 Cont’d 7 statement true condition evaluated false increment initialization for (initialization ; condition ; increment ) { statement; } Theinitialization is executed once before the loopbegins ✓ It is similar to the while loop condition part ✓ The statement is executed until the conditionbecomes false The incrementportionis executed at the end of eachiteration
  • 20.
    Jan 2023 Cont’d 8 do { statement(s); } while (repetition condition) next statement(s);
  • 21.
  • 22.
    Jan 2023 Reading Resources/Materials eBooks– selection statements ▪ 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, Global Edition] (2017)
  • 23.
    Jan 2023 Reading Resources/Materials eBooks– loop statements ▪ Chapter 7 & 8: Diane Zak; An Introduction to Programming with C++ [8th Edition], 2016 Cengage Learning ▪ Chapter 5: 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 & 5: P. Deitel , H. Deitel; C++ how to program, [10th, Global Ed.] (2017)
  • 24.
    Jan 2023 Reading Resources/Materials OnlineMaterials ▪ https://www.w3schools.com/cpp/default.asp ▪ https://www.javatpoint.com/cpp-tutorial ▪ https://www.programiz.com/cpp-programming ▪ https://www.hackerrank.com/domains/cpp ▪ https://cplusplus.com/doc/tutorial/
  • 25.
    Jan 2023 Thank You ForYour Attention!!