Topic 9
Control Structures and Flowcharts
1
Control Structures
 Control structures determine the flow of
execution or control flow in a program.
 3 control structures:
 Sequence
 Selection
 Repetition
2
Control Structures
 Sequence control structure executes statements
one after another in the sequence they appear.
 Example:
3
int n;
cout << "Enter n: ";
cin >> n;
int n_squared = n * n;
cout << "n x n = " << n_squared << endl;
Control Structures
4
int n;
cout << "Enter n: ";
cin >> n;
int n_squared = n * n;
cout << "n x n = " << n_squared << endl;
 Sequence control structure executes statements
one after another in the sequence they appear.
 Example:
Control Structures
5
int n;
cout << "Enter n: ";
cin >> n;
int n_squared = n * n;
cout << "n x n = " << n_squared << endl;
 Sequence control structure executes statements
one after another in the sequence they appear.
 Example:
Control Structures
int n;
cout << "Enter n: ";
cin >> n;
int n_squared = n * n;
cout << "n x n = " << n_squared << endl;
6
 Sequence control structure executes statements
one after another in the sequence they appear.
 Example:
Control Structures
int n;
cout << "Enter n: ";
cin >> n;
int n_squared = n * n;
cout << "n x n = " << n_squared << endl;
7
 Sequence control structure executes statements
one after another in the sequence they appear.
 Example:
Control Structures
 Selection control structure chooses between two or
more alternatives.
 It allows you to make decisions.
 Repetition control structure repeats an operation or
sequence of operations many times.
8
Flowcharts
 Flowchart is a tool to design algorithms for
solutions to problems, just like pseudocode.
 It represents the logical flow of a program or the
program logic in graphical form (with picture
symbols).
9
Flowcharts – Common Symbols
Symbol Meaning
Terminal – for start and stop
Flow lines
Assignment statement
Input/output statement
Module call (void functions only)
Connector
n
10
Flowchart Example 1
Equivalent algorithm in pseudocode:
START
Read n1, n2
sum ← n1 + n2
Display sum
STOP
Start
Stop
Read n1, n2
sum ← n1 + n2
Display sum
11
Flowchart Example 2 - 1
Equivalent algorithm in pseudocode:
START
draw_intersect()
draw_rectangle()
STOP
Start
Stop
draw_intersect()
draw_rectangle()
12
Flowchart Example 2 - 2
Equivalent algorithm in pseudocode
for function Draw-rectangle:
draw_rectangle ()
draw_horizontal ()
draw_parallel ()
draw_horizontal()
RETURN
Note: Use function name (with
optional parameter names) and
Return instead of Start and Stop
draw_rectangle()
Return
draw_horizontal()
draw_parallel()
draw_horizontal()
13
Flowchart Example 3 - 1
Equivalent algorithm in pseudocode:
START
Read n1, n2
sum ← get_sum(n1, n2)
Display sum
STOP
Start
Stop
Read n1, n2
sum ← get_sum(n1, n2)
Display sum get_sum(n1, n2) is function call.
The function returns the sum of
n1 and n2
14
Flowchart Example 3 - 2
Equivalent algorithm in pseudocode
for function get_sum:
get_sum(a, b)
result ← a + b
RETURN
get_sum(a, b)
Return
result ← a + b
15

09_Control_Structures_and_Flowcharts.pdf

  • 1.
  • 2.
    Control Structures  Controlstructures determine the flow of execution or control flow in a program.  3 control structures:  Sequence  Selection  Repetition 2
  • 3.
    Control Structures  Sequencecontrol structure executes statements one after another in the sequence they appear.  Example: 3 int n; cout << "Enter n: "; cin >> n; int n_squared = n * n; cout << "n x n = " << n_squared << endl;
  • 4.
    Control Structures 4 int n; cout<< "Enter n: "; cin >> n; int n_squared = n * n; cout << "n x n = " << n_squared << endl;  Sequence control structure executes statements one after another in the sequence they appear.  Example:
  • 5.
    Control Structures 5 int n; cout<< "Enter n: "; cin >> n; int n_squared = n * n; cout << "n x n = " << n_squared << endl;  Sequence control structure executes statements one after another in the sequence they appear.  Example:
  • 6.
    Control Structures int n; cout<< "Enter n: "; cin >> n; int n_squared = n * n; cout << "n x n = " << n_squared << endl; 6  Sequence control structure executes statements one after another in the sequence they appear.  Example:
  • 7.
    Control Structures int n; cout<< "Enter n: "; cin >> n; int n_squared = n * n; cout << "n x n = " << n_squared << endl; 7  Sequence control structure executes statements one after another in the sequence they appear.  Example:
  • 8.
    Control Structures  Selectioncontrol structure chooses between two or more alternatives.  It allows you to make decisions.  Repetition control structure repeats an operation or sequence of operations many times. 8
  • 9.
    Flowcharts  Flowchart isa tool to design algorithms for solutions to problems, just like pseudocode.  It represents the logical flow of a program or the program logic in graphical form (with picture symbols). 9
  • 10.
    Flowcharts – CommonSymbols Symbol Meaning Terminal – for start and stop Flow lines Assignment statement Input/output statement Module call (void functions only) Connector n 10
  • 11.
    Flowchart Example 1 Equivalentalgorithm in pseudocode: START Read n1, n2 sum ← n1 + n2 Display sum STOP Start Stop Read n1, n2 sum ← n1 + n2 Display sum 11
  • 12.
    Flowchart Example 2- 1 Equivalent algorithm in pseudocode: START draw_intersect() draw_rectangle() STOP Start Stop draw_intersect() draw_rectangle() 12
  • 13.
    Flowchart Example 2- 2 Equivalent algorithm in pseudocode for function Draw-rectangle: draw_rectangle () draw_horizontal () draw_parallel () draw_horizontal() RETURN Note: Use function name (with optional parameter names) and Return instead of Start and Stop draw_rectangle() Return draw_horizontal() draw_parallel() draw_horizontal() 13
  • 14.
    Flowchart Example 3- 1 Equivalent algorithm in pseudocode: START Read n1, n2 sum ← get_sum(n1, n2) Display sum STOP Start Stop Read n1, n2 sum ← get_sum(n1, n2) Display sum get_sum(n1, n2) is function call. The function returns the sum of n1 and n2 14
  • 15.
    Flowchart Example 3- 2 Equivalent algorithm in pseudocode for function get_sum: get_sum(a, b) result ← a + b RETURN get_sum(a, b) Return result ← a + b 15