Jump to content

Programming Fundamentals/Flowcharts

From Wikibooks, open books for an open world

Overview

[edit | edit source]
Flowchart displaying "Hello world!"

A flowchart is a type of diagram that represents an algorithm, workflow or process. The flowchart shows the steps as boxes of various kinds, and their order by connecting the boxes with arrows. This diagrammatic representation illustrates a solution model to a given problem. Flowcharts are used in analyzing, designing, documenting or managing a process or program in various fields.[1]

Discussion

[edit | edit source]

Flowcharts display the steps in code as shapes connected together with arrows. The main goal is to create a rough draft of a solution to a coding problem. The type of shapes seen in the flowchart depends on what statements the programmer wants to create. For example, an “if” statement (a part of the code that only works if a certain condition is true) is represented by a diamond, while the looping statements (ones that allow a section of code to repeat itself as needed) are represented by hexagons. Flowcharts may also color code the different types of statements as well, making the code easier to read.

Common flowcharting symbols and examples follow. When first reading this section, focus on the simple symbols and examples. Return to this section in later chapters to review the advanced symbols and examples.

Simple Flowcharting Symbols

[edit | edit source]

Terminal

[edit | edit source]

The rounded rectangles, or terminal points, indicate the flowchart's starting and ending points.

Terminal

Flow Lines

[edit | edit source]

Note: The default flow is left to right and top to bottom (the same way you read English). To save time arrowheads are often only drawn when the flow lines go contrary the normal.

Line

Input/Output

[edit | edit source]

The parallelograms designate input or output operations.

Input/Output

Process

[edit | edit source]

The rectangle depicts a process such as a mathematical computation, or a variable assignment.

Process

Decision

[edit | edit source]

The diamond is used to represent the true/false statement being tested in a decision symbol.

Decision

Advanced Flowcharting Symbols

[edit | edit source]

Module Call

[edit | edit source]

A program module is represented in a flowchart by a rectangle with some lines to distinguish it from process symbol. Often programmers will make a distinction between program control and specific task modules or between local functions and library functions.

Predefined Process

Connectors

[edit | edit source]

Sometimes a flowchart is broken into two or more smaller flowcharts. This is usually done when a flowchart does not fit on a single page, or must be divided into sections. A connector symbol, which is a small circle with a letter or number inside it, allows you to connect two flowcharts on the same page. A connector symbol that looks like a pocket on a shirt, allows you to connect to a flowchart on a different page.

On-page Connector Off-page connector

Simple Examples

[edit | edit source]


Simple Examples

[edit | edit source]

We will demonstrate various flowcharting items by showing the flowchart for some pseudocode.

Functions

[edit | edit source]

pseudocode: Function with no parameter passing

Function clear monitor
    Pass In: nothing
    Direct the operating system to clear the monitor
    Pass Out: nothing
End function

pseudocode: Function main calling the clear monitor function

Function main
    Pass In: nothing
    Doing some lines of code
    Call: clear monitor
    Doing some lines of code
    Pass Out: value zero to the operating system
End function

Sequence Control Structures

[edit | edit source]

The next item is pseudocode for a simple temperature conversion program. This demonstrates the use of both the on-page and off-page connectors. It also illustrates the sequence control structure where nothing unusual happens. Just do one instruction after another in the sequence listed.

pseudocode: Sequence control structure

Filename: Solution_Lab_04_Pseudocode.txt
Purpose:  Convert Temperature from Fahrenheit to Celsius
Author:   Ken Busbee; © 2008 Kenneth Leroy Busbee
Date:     Dec 24, 2008

Pseudocode = IPO Outline

input
    display a message asking the user for the temperature in Fahrenheit
    get the temperature from the keyboard

processing
    calculate the Celsius by subtracting 32 from the Fahrenheit temperature then multiply the result by 5 then divide the result by 9. Round up or down to the whole number.
    HINT: Use 32.0 when subtracting to ensure floating-point accuracy.

output
    display the Celsius with an appropriate message
    pause so the user can see the answer 

Advanced Examples

[edit | edit source]

Selection Control Structures

[edit | edit source]

pseudocode: If then Else

If age > 17
    Display a message indicating you can vote.
Else
    Display a message indicating you can't vote.
Endif

pseudocode: Case

Case of age
    0 to 17   Display "You can't vote."
    18 to 64  Display "You are in your working years."
    65 +      Display "You should be retired."
End case

Iteration (Repetition) Control Structures

[edit | edit source]

pseudocode: While

count assigned zero
While count < 5
    Display "I love computers!"
    Increment count
End while

pseudocode: For

For x starts at 0, x < 5, increment x
    Display "Are we having fun?"
End for

The for loop does not have a standard flowcharting method and you will find it done in different ways. The for loop as a counting loop can be flowcharted similar to the while loop as a counting loop.

pseudocode: Do While

count assigned five
Do
    Display "Blast off is soon!"
    Decrement count
While count > zero

pseudocode: Repeat Until

count assigned five
Repeat
    Display "Blast off is soon!"
    Decrement count
Until count < one

Key Terms

[edit | edit source]
decision symbol
A diamond used in flowcharting for asking a question and making a decision.
flow lines
Lines (sometimes with arrows) that connect the various flowcharting symbols.
flowcharting
A programming design tool that uses graphical elements to visually depict the flow of logic within a function.
input/output symbol
A parallelogram used in flowcharting for input/output interactions.
process symbol
A rectangle used in flowcharting for normal processes such as assignment.

References

[edit | edit source]