Lesson 01: Flowcharts
Program development can be described as a serval step process:
- Understand the problem.
- Plan the logic of the program.
- Code the program using a structured computer language.
- Using a compiler, translate the source code into a machine-executable program.
- Test and debug the program.
- Put the program into production, write the manual, and prepare some training plans for customers.
- Maintain and enhance the program.
Planning the logic of the program requires the development of algorithms. An algorithm is a finite, ordered set of unambiguous steps that terminate with a solution to the problem. Human readable representations such as flowcharts and pseudocode are typically used to describe an algorithm's steps and the relationships among the steps.
A flowchart is a graphical representation of the steps and control structures used in an algorithm. A flowchart does not involve a particular programming language but rather uses geometric symbols and flowlines to describe the algorithm. From a flowchart, a programmer can produce the code required to compile an executable program.
Online Flowchart: https://app.diagrams.net/
1.1 Basic Flowchart Blocks
What is a flowchart?
- A flowchart is a picture (graphical representation) of the problem-solving process.
- A flowchart gives a step-by-step procedure for the solution of a problem.
Uses of a flowchart
- To specify the method of solving a problem.
- To plan the sequence of a computer program.
- Communicate ideas, solutions.
- Show the logic of the classification of data.
- To teach and also to learn.
Flowchart symbols and their purpose
|Geometric Shape||Symbol Name||Purpose|
|Flowlines||Arrows are used to connect the steps in a flowchart to show the flow or sequence of the problem-solving process|
|Terminal||Ellipse (or lozenge) is used to indicate the start and end of a flowchart. The Start is written in the ellipse indicates the beginning of a flowchart. End or Stop or Exit are written in the ellipse indicates the end of the flowchart.|
|Input/Output||A parallelogram is used to read data (input) or to print data (output).|
|Process||A rectangle is used to show the processing that takes place in the flowchart.|
|Decision||A diamond with two branches is used to show the decision making step in a flowchart. A question is specified in the diamond. The next step in the sequence is based on the answer to the question, which is 「Yes」 or 「No」.|
|Subroutine||Predefined Process symbol shows the named process that is defined elsewhere.|
|On-page Connector||Pairs of the on-page connector are used to replace long lines on a flowchart page.|
|Off-page Connector||An off-page connector is used when the target is on another page.|
Shows relationships between different shapes.
Flowlines show the sequence of processes and have arrows to show the process's direction. Each flowline usually connects two blocks.
The terminator symbol marks the starting or ending point of the system. It usually contains the word "Start" or "End."
The terminal symbols, sometimes called start-stop symbols, show the Start and End points of the process.
Input / Output
Represents material or information entering or leaving the system, such as customer order (input) or a product (output).
Input/output symbols, represented by parallelograms, indicating input/output operations.
A box can represent a single step ("add two cups of flour") or an entire sub-process ("make bread") within a larger process.
Processing symbols, represented by rectangles, indicating processing steps, such as arithmetic statements. A process always has exactly one input and one output.
A decision or branching point. Lines representing different decisions emerge from different points of the diamond.
Decision symbols, represented by diamonds. The label in a decision block should be a question that clearly has only two possible answers. The decision block will have exactly one input and two outputs. The two outputs will be labeled with the two answers to the question to show the logic flow direction depending upon the decision made.
Indicates a sequence of actions that perform a specific task embedded within a larger process. This sequence of actions could be described in more detail on a separate flowchart.
Subroutines are portions of code that run and return the execution point to the calling function. This allows you to write one subroutine and call it as often as you like from anywhere in the code. Subroutines make the code smaller and easier to test.
The flowchart connector symbols are used to make a logic transfer to another location in the flowchart. A transfer location can be on the same page or another page. When you reach the bottom of the page or need to jump to another page, draw a flow chart connector symbol and connect it to the last item on the chart.
The connectors are used in pairs: in the originating drawing and the connected drawing. A flowline leaving a drawing requires a To connector. The same line in the second drawing requires a From connector. Both To and From connectors are represented by the same symbol, and both have the same connector number to identify the continuity.
Connects two or more parts of a flowchart, which are on the same page.
On-page symbol: a circle with a number or letter to identify the matching transfer location. The on-page connector shows a jump from one point in the process flow to another. They are handy for avoiding flow lines that cross other shapes and flow lines. They are also handy for jumping to and from a sub-process defined in a separate area than the main flowchart.
Figure 1.7: Sample Page Connector Example
Connects two parts of a flowchart that are spread over different pages.
Off-page symbol: a square with a pointed bottom, containing a page number and a letter to identify the matching transfer location. The off-page connector shows the continuation of a process flowchart onto another page. The target page number must be added in the To symbol, and both To and From symbols have the same node letters (or number).
Figure 1.8: Off-Page Connector Example
- The flowchart can have only one start and one end symbol.
- On-page connectors are referenced using numbers.
- Off-page connectors are referenced using the alphabet.
- The general flow of processes is top to bottom or left to right.
- Arrows should not cross each other.
1.2 Flowchart Structures
A structured flowchart is one in which all processes and decisions must fit into one of a few basic structured elements. The basic elements of a structured flowchart are shown below sessions. It should be possible to take any structured flowchart and enclose all of the blocks within one of the following process structures. Note that each of the structures shown below has exactly one input and one output. Thus the structure itself can be represented by a single process block.
The sequence structure is just a series of processes carried out one after the other. Most programs are represented at the highest level by a sequence, possible with a loop from the end to the beginning.
Figure 2.1: Sequence Structure
In sequence structure, each block performs an action or task and then connects to the next action in order. So, action or event leads to the next ordered action in a predetermined order. A sequence can contain any number of tasks, but there is no chance to branch off and skip any tasks or actions. Once you start a series of actions in a sequence, you must continue step-by-step to execute each action until the sequence ends. When run, the program must perform each action in order with no possibility of skipping an action or branching off to another action.
The selection structure is also known as an if-else structure and a decision structure. A selection structure is a programming feature that performs different processes based on whether a boolean condition is true or false. No matter which path to follow, continue with the next procedure.
There are two types of selection structures that can be used to achieve different outcomes.
The case structure represents a serial of if-else structures that compare the same variable with different expression values.
- There are only three basic structures — sequence, selection, and loop structures.
- Each structure has one entry and one exit point.
- Structures attach to others only at entry or exit points.
1.3 Structured Program Design
Simple programming exercises can often be solved by just sitting down and writing code to implement the desired problem. Complex problems, however, can be difficult to write and impossible to debug if not implemented using a structured design process. Using a structured design process leads to the following benefits:
- Early detection of design flaws
- Programs that can be easily modified
- Clear and complete documentation
- Modular design to improve testing
- Modular design to break up a problem into smaller sections
The structured program mainly consists of three types of elements:
- Selection Statements
- Sequence Statements
- Iteration Statements
The structured program consists of well-structured and separated modules. But the entry and exit in a Structured program is a single-time event. It means that the program uses single-entry and single-exit elements. Therefore a structured program is a well-maintained, neat, and clean program. This is the reason why the Structured Programming Approach is well accepted in the programming world.
All logic problems can be solved using only these three structures — sequence, selection, and loop structures. A structured program includes only combinations of the three structures. All the structures can be combined in infinite ways and can be stacked or connected only at their entrance or exit points.
A structured program:
- Another structure can replace any individual action or step in a structure.
- Nesting: placing one structure within another
- Block: a group of structures that execute as a single unit
if conditionC is ture then
while conditionF is true
Figure 3.1: Structured Flowchart and Pseudocode
Flowchart Showing a Sequence Nested within a Selection
if conditionA is true then
Selection in a Sequence within a Selection
if conditionA is true then
if conditionF is true then
Flowchart for For Loop within Selection within Sequence within Selection
if conditionA is true then
if conditionF is true then
while conditionJ is true
1.4 Unstructured Program
Examples of Good and Bed Structured Program
The following figure shows an example of a properly and improperly structured flowchart. The unstructured flowchart is an example of what can happen if a program is written first, and then a flowchart is created to represent the program. This type of unstructured flow is often called 'spaghetti' programming and normally has elements of its structure impossibly intertwined around other elements. A program of this sort is complicated to understand, implement, debug, and maintain.
Flowchart of Number-Double Problem
You are designing a program to ask users to put an integer number. If the number input is 0, then stop the program; otherwise, calculate the doubling value and display it on the screen. Then repeat the procedures again.
For the loop in Figure 4.4 to be a structured loop, the logic must return to the decision when the sequence ends. The flowchart in Figure 4.5 shows the flow of logic returning to the decision immediately after the sequence. Now the flowchart is structured.
Figure 4.5: Structured, But Nonfunctional, Flowchart of Number-Doubling Problem
Let us follow the flowchart in Figure 4.5 to run the program. Assuming when the program starts, the user enters 10 for the value of userInput. Ten is not equal to zero, so the number is doubled, and 20 is printed out on the screen. Then the question userInput equal to zero is asked again. It can not be false, because the logic did not go to the block, the value of userInput never changed. Therefore, 10 is doubled again and the answer 20 prints again. This goes on forever, with the 20 being printed repeatedly. The program logic shown in the above diagram is structured, but it does not work.
How can the number-doubling problem be structured and still work? Sometimes, some extra steps may be added to the flowchart to make the program to be structured. In this case, after printing the answer on the screen and before checking the userInput value, the step must be added in between in order to update the userInput value.
while userInput is not equal to zero
calculate answer = userInput * 2
Figure 4.6: Functional, Structured Flowchart for the Number-Doubling Problem
You are designing a system to determine discounts for customers based on their age. If they are younger than 10 or older than 55, they can get a 15% discount, otherwise, they can get a 10% discount.
Figure 4.7: Unstructured Selection
Is the flowchart segment in Figure 4.7 structured?
No, it is not constructed from the three basic structures. There are two flowlines pointing to the same processing block (discount = 15%) from different selection structures, that must be untangled.
Follow the line on the left side of Question (age < 55), just untangle it by repeating the step that is tangled. The result is the flowchart shown in Figure 4.8. The entire flowchart is structured — it has a sequence followed by a selection inside a selection.
if age is greater than 10 then
if age is less than 55 then
discount = 0.1
discount = 0.15
discount = 0.15
Figure 4.8: Finished Flowchart
The flowchart in Figure 4.9 is neither a while loop (that begins with a decision and, after an action, returns to the decision), nor a do-while loop (that begins with an action and ends with a decision that might repeat the action). Instead, it begins like a while loop, with a process followed by a decision, but one branch of the decision does not repeat the initial process; instead, it performs an additional new action before repeating the initial process.
Figure 4.9: Unstructured Loop
Figure 4.10 shows the same logic as Figure 4.9, but now it is structured logic, with a sequence of two actions occurring within the loop. Does this diagram look familiar to you? It uses the same technique of repeating a needed step that in [Flowchart of Number-Double Problem]
Figure 4.10: Sequence and Structured Loop That Accomplish the Same Tasks as Figure 4.9
Q01 Draw Flowcharts
Using an online flowchart website to draw the following flowcharts, export as PDF file then submit it before the due. (Do not hand draw any flowcharts.)
* Draw the flowchart on the left, including comments and arrows.
Q02:Match the elements of a flowchart
Match the elements of a flowchart and their purpose of use in the following:
The elements include: [Loop], [Start], [Process], Input], [Stop], [Decision], and [Output]
|Calculate total of A, B, C.|
|Indicate that the problem has been solved.|
|Find if a number is greater than the other.|
|Read a number and calculate the factorial of the number.|
|Read three numbers.|
|Print the total.|
|Indicate the beginning of a problem-solving flow.|
Complete the crossword puzzle.
- I am a rectangle in a flowchart. What do I represent?
- When you want to show a decision-making step, you can use this box.
- You can use me to communicate ideas; graphically represent a problem-solving process.
- I connect two geometrical boxes in a flowchart.
- In the flowchart, I represent data or information that is available.
- All flowcharts begin with me. I am elliptical in shape.
Q04: Programming Structure
For each of the unstructured flowcharts as below, redrawn in such a way that the logic remains the same but is structured. Also, highlight the different types of structures. Do not hand draw any flowcharts.