Flowcharts and related terms

Sometimes, it becomes difficult to understand the flow of a complex decision structure in text format. Flowcharting is a technique that visually demonstrates the flow of instructions in a program.

A program that involves decisions also has branches of flow. The expression used to write the decision is called a conditional expression or condition. The flow of the program depends on the result of a condition, which is either true or false. If the condition is true, the part of the program to be executed is called the true branch. Similarly, if the condition is false, the part of the program to be executed is called the false branch. Flowcharts illustrate the branching of flow in such programs.

Note: The terms, statement and instruction, are used interchangeably.

There are several shapes and symbols used in flowcharting. Some of the shapes that we use to make our flowcharts include:

  • Start/Stop: It is used to indicate the start and the end of the program.
  • Input/Output: It is used for input and output instructions in the program.
  • Decision: It is used to write conditional expressions in the program.
  • Process: It is used to write instructions other than input/output and conditions in the program.
  • Flow: It describes the flow direction from one shape to another.

There are two types of decisions in programming, which we’ll explore in the following sections.

One-way decision

A decision that has only one branch is known as a one-way decision. This branch is executed when the conditional expression is true. Therefore, it’s termed as a true branch.

Note: The decision box has two outgoing arrows (outflows) — one arrow is labeled “Yes”, which indicates the start of the true branch, and the other arrow is labeled “No”.

Let’s learn about one-way decisions with the help of the following sample flowchart.

Sample flowchart: One-way decision

In this sample flowchart, we want to find the absolute difference between two values input by the user. The difference is calculated by subtracting one value from the other. After finding the difference, we evaluate whether it’s negative with the help of a condition in the decision box. The instruction diff < 0 uses a new operator <, called less-than, which returns true if diff is negative. So, we change its sign via diff = - diff. If the value of diff is not negative, we don’t take any additional steps. In the end, we display the value of diff.

In the flowchart above:

  • The first box is the input/output box. It contains two input instructions.
  • The second box is the process box. It calculates the difference between two variables and stores it in the variable diff.
  • The third box is the decision box. It contains the conditional expression.
  • The right arrow is used for the true branch, which will be executed only if diff is negative.
  • The last box is the input/output box. It contains the print statement, which displays the absolute difference along with the label.

One-way decision practice

Let’s use a few simple examples to practice flowcharting one-way decisions.

Weekdays

Say we want to develop a converter program that converts a day number into a day name. The following flowchart takes an integer value representing the day number of the week (1,2,,7)(1, 2, …, 7) as input and gives the name of the day as output (“Sunday” against 11, “Monday” against 22, and so on).

We use a new symbol == to test equality to show that the left-hand side (LHS) and right-hand side (RHS) of these expressions are equal.

In the flowchart above:

  • The dayName variable is assigned a value based on the value of the day number.

  • The last box displays the value stored in the dayName.

Month names

Now let’s create a converter that changes a month number into a month name. The following flowchart takes an integer value representing the month of the year (1,2,3,4...12)(1, 2, 3, 4 ... 12) as input and displays the month name (“January”, “February” … “December”).

In the flowchart above:

  • The monthName variable is assigned a value based on the value of the month number.

  • The last box displays the value stored in the monthName.

Letter grades

Next, let’s create a converter that changes percentage marks into letter grades. The following flowchart takes a value representing percentage marks as input and displays the corresponding letter grade using a one-way decision.

If the marks are 8585 or above, then A will be displayed. If the marks are 7070 or above but below 8585, then B will be displayed. If the marks are 5555 or above, but below 7070, then C will be displayed. Otherwise, an F will be displayed.

The new symbol >=, called greater-than-or-equal, which returns true for marks >= 85 when the value of marks is 8585 or above. Remember that and returns true when both conditions are true. In the flowchart above:

  • The grade is assigned a value based on the value of the marks.
  • The last box displays the value stored in the grade.

Two-way decision

A decision with two possible branches is known as a two-way decision. These two distinct branches, the true branch and the false branch, are based on the result of a conditional expression. The right branch from the decision box starts the true branch, and the left branch starts the false branch. For further clarification, we label them “Yes*” and “No”, respectively.

Note: The decision box of a two-way decision has two branches — “Yes” indicates the true branch and “No” indicates the false branch.

All the examples in the above section illustrate the one-way decision. However, all these problems may be solved using two-way decisions in a more elegant way.

Let’s learn about two-way decisions with the help of the following sample flowchart.

Sample flowchart: Two-way decision

In this sample flowchart of a two-way decision, we want to find the difference between the two values input by the user. The difference is calculated by subtracting the smaller value from the larger one. First, we compare both inputs from the user with the help of an if statement. The statement a > b uses a new operator >, called greater-than, which returns true if a is greater than b, so we compute a - b and store the difference in the diff. When a is not greater than b we compute b - a and store the difference in the diff. In the end, we display the value of the diff.

Now, consider the solution of the same problem using a one-way decision given in the previous section. There is a possibility of an additional computation when the condition is true in that solution. Here, we use only one in either case.

In the flowchart above:

  • The first box is the input/output box. It contains two input instructions.

  • The second box is the decision box. It contains the conditional expression.

  • The right arrow is used for the true branch, which will be executed when a is greater than b.

  • The left arrow is used for the false branch, which will be executed when a is less than or equal to b.

  • The last box is the input/output box. It contains the print statement, which displays the difference along with a label.

Two-way decision practice

Let’s use a few simple examples to practice flowcharting two-way decisions.

Positive or negative

Let’s say we want to find the sign of a number. The following flowchart takes an integer value as input and displays whether the value is positive or negative.

In the flowchart above:

  • The msg is assigned a value based on the sign of a.

  • The last box displays the value stored in the msg.

Greater of two numbers

Let’s say we want to find the greater between two numbers. The following flowchart takes two integer values as input and shows which of these two values is greater.

In the flowchart above:

  • The max variable is assigned the greater (maximum) value between a and b.
  • The last box displays the value stored in the max.

Even or odd

Let’s say we want to determine whether a number is even or odd. The following flowchart takes an integer value as input and shows whether the value is even or odd. We use the modulus operator % that returns the remainder after division. It’s also possible to perform this operation with integer division, as shown below:

In the flowchart above:

  • The msg is assigned the value based on the result of a % 2 == 0. If a is an even number, a % 2 returns 0; otherwise, it returns 1.
  • The last box displays the value stored in the msg.

Greatest of three numbers

The nesting of decisions allows a program to sequentially determine the path of execution through a hierarchy of decisions.

The following flowchart takes three distinct integers as input and shows the greatest value among the three.

Let’s break down the nested decisions in the flowchart above:

  • The true branch of the first decision box contains another decision box as its first step, which is called a nested decision.
  • The false branch of the first decision box is also an example of a nested decision.
  • If the value of a is greater than b, we compare a and c. If a isn’t greater than b, we compare b and c.
  • The max variable is assigned the greatest (maximum) value among a, b, and c since all values ought to be distinct, so no two values are equal.
  • The last box displays the value stored in the max.

Letter grades (two-way if)

Let’s say we want to write a program that converts percentage marks into letter grades. The following flowchart uses a two-way if to take an input of percentage marks and display the corresponding letter grade.

If the marks are 8585 or above, an A will be displayed. If the marks are 7070 or above but below 8585, a B will be displayed. If the marks are 5555 or above but below 7070, a C will be displayed. Otherwise, an F will be displayed.

Previously, we solved the same problem using a one-way decision. However, that solution redundantly tests the condition again and again. For example, if the marks are below 5555, we got the correct grade after the first condition. However, we kept testing for other values due to the structure of a one-way decision.

Another point to notice is that we may start testing from grade F to A (as we did in the above section) or vice versa. In this solution, the order is from A to F.

In the flowchart above:

  • The false branches of the first two decision boxes are examples of nested decisions.

  • The grade is assigned a value based on the value of the marks.

  • The last box displays the value stored in the grade.