Flowcharts and Conditional Expressions
Learn to visualize conditional expressions and decisions in a program with the help of flowcharts.
We'll cover the following...
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.