With the Top-Down Approach, We're Really Going “Down” Now
In the lecture, it was shown how flowcharts can be used to describe the program flow within a function. The following conventions were used:
-
Conditional jumps are represented by a diamond, inside which the condition is depicted.
-
Unconditional jumps are represented by an empty diamond.
-
All other instructions are represented by a rectangle containing the instruction.
-
All routes and rectangles are arranged vertically.: This expresses that in the corresponding machine code, the instructions are stored sequentially in memory, representing the sequential execution of instructions. Arrows visualize the order in which the instructions are executed. Execution always starts from the top.
To better understand how control structures can be mapped to primitive machine instructions, it was also described how a while loop can be realized with goto statements:

Comparing the flowchart with the code containing goto statements, the following rules can be observed:
-
An empty route is realized with a goto statement.
-
A route with a condition is realized with a construct of the form if (condition) { goto some_label; }, informally referred to as “Conditional Goto.”
Tasks
-
Derive rules for expressing control structures with goto statements by examining various examples. As the first example, use the following program:
1 2 3 4 5 6 7 8 9 10 11 12
@ <stdio.hdr> fn main() { local ch: int = getchar(); if (ch == 'A') { printf("I got an A!\n"); } else { printf("Not an A :(\n"); } }
First, test the program with various inputs. Then describe the program with a flowchart. Rewrite the flowchart to use goto statements and then, of course, implement this flowchart in a program if_goto.abc.
-
A do-while loop can be described with the following flowchart:
Use the following program do_while.abc, which reads characters until it receives a period .:
1 2 3 4 5 6 7 8 9 10 11
@ <stdio.hdr> fn main() { local ch: int; do { ch = getchar(); printf("got '%c' (ASCII %d)\n", ch); } while (ch != '.'); }
Test the program and confirm that the last character (the period .) is also output. Describe the program again with a flowchart and then rewrite it (File: do_while_goto.abc) to use goto statements.
-
Repeat the entire process for the following program for.abc, i.e., testing, creating a flowchart, and rewriting it into a program (File: for_goto.abc) that uses goto statements:
1 2 3 4 5 6 7 8 9 10 11
@ <stdio.hdr> fn main() { local ch: int; do { ch = getchar(); printf("got '%c' (ASCII %d)\n", ch); } while (ch != '.'); }