Paper written by Bohm and Jacopini in 1966 proposed that all programs can be written using 3 types of control structures:
sequential
conditional
repetition
Source: Bohm, C., Jacopini, G. (1966). Flow diagrams, turing machines and languages with only two formation rules. Communications of the ACM, 9, 366-371.
Control structures in the C language
Decision with if statement
Pseudo code:
display have a nice day
if raining
display reminder to bring umbrella
Be careful to avoid division by zero in test conditions.
Flowchart:
Decision with if statement
Pseudo code:
obtain input number
if multiple of 5
display message that multiple found
display remainder of division by 5
Be careful to avoid division by zero in test conditions.
Flowchart:
Decision with if / else statement
Pseudo code:
obtain 2 cards
if sum of cards equals to 21
display blackjack message
else
display sum of cards
Be careful to avoid division by zero in test conditions.
Flowchart:
Decision with if / else statement and logical negation
Pseudo code:
obtain 2 cards
if sum of cards not equal to 21
display sum of cards
else
display blackjack message
Be careful to avoid division by zero in test conditions.
Flowchart:
Nested decisions
Pseudo code:
obtain 2 cards
calculate sum of cards
if sum of cards less than 12
if first card is an ace
add 10 to sum
display the sum
The decision logic affects the order of the test conditions.
Be careful to avoid division by zero in test conditions.
Flowchart:
It may be possible to optimize the logic of nested decisions.
Chained decisions
Pseudo code:
obtain 2 cards
calculate sum of cards
if sum equal to 21
display blackjack message
else if 2 cards are actually a pair
suggest that the player split the pair
else if sum is more than 17
suggest that player stand the hand
display the sum
The decision logic affects the order of the test conditions.
Chained decision structures have only one path of execution.
Be careful to avoid division by zero in test conditions.
Flowchart:
Selection decisions with switch statement
Pseudo code:
obtain 2 cards
calculate sum of cards
evaluate value of sum
21
display blackjack message
20
19
18
suggest that player stand the hand
default
display the sum
switch statements must use integer variables.
The default selection is always executed. It must be the last selection of the switch statement.
Selection decision structures may have more than one path of execution.
The decision logic affects the order of the comparison conditions. Comparisons are made in sequence from top to bottom.
Flowchart:
Selection decisions with switch statement and break keyword
Pseudo code:
obtain 2 cards
calculate sum of cards
evaluate value of sum
21
display blackjack message
break
20
19
18
suggest that player stand the hand
break
default
display the sum
break
The break keyword immediately exits the current switch statement.
The default selection is only executed if theswitchstatement has not ended.
Flowchart:
Decisions may have one or more paths of execution
Chained decision structures have only one path of execution.
Selection decision structures may have more than one path of execution.
Software development cycle
Design and test the program logic with pseudo code and flow charts before implementing it with source code.
Use extra printf() functions to help follow the program logic.
Remember to check for matching braces when replacing a simple statement with a compound statement.
Iteration using for loops
Pseudo code:
setup variables
start loop by initializing counter to zero
repeat loop if counter is less than 3
obtain input card value
add input to sum
increase counter by 1 and go to test condition
display sum of all cards
The break keyword immediately exits the current for loop.
It is possible that the statements inside a for loop is never executed because the test condition was never successful.
Be careful to avoid division by zero in test conditions.
Flowchart:
Iteration using while loops
Pseudo code:
setup variables
start loop by initializing counter to zero
repeat loop if counter is less than 3
obtain input card value
add input to sum
increase counter by 1 (remember this step to avoid infinite loops)
display sum of all cards
The break keyword immediately exits the current while loop.
It is possible that the statements inside a while loop is never executed because the test condition was never successful.
Be careful to avoid division by zero in test conditions.
Flowchart:
Iteration using do / while loops
Pseudo code:
setup variables
initialize counter to zero
start loop
obtain input card value
add input to sum
increase counter by 1 (remember this step to avoid infinite loops)
repeat loop if counter is less than 3
display sum of all cards
The statements inside a do / while loop always execute at least once because the body of the loop executes before it reaches the test condition.
The break keyword immediately exits the current do / while loop.
Be careful to avoid division by zero in test conditions.
Flowchart:
Dry run table during the software development cycle
A dry run is a manual step by step trace of the program logic and algorithm. It uses a table where the first column is the step number and one column is added for each data variable used in the program. Each statement in the program increases the dry run table by one step. Using a dry run table is useful for testing and finding errors.