Repetition (Iteration) Constructs

Consider the following example:

Find the sum of 10 numbers provided by the user.

This can be accomplished as follows:

The above solution is very long as well as difficult to understand , manipulate and communicate .

Observe that there were many repeated steps in obtaining input from the user.

The solution can be simplified by looping.

A general looping construct is shown below:

The solution can be simply implemented using looping:

Class Discussion: walk-through of solution is discussed.

This files used in this post can be downloaded here:

https://drive.google.com/drive/folders/1KjNcPQATyqh8quxnRjUDbJOgdzGvUY1P?usp=sharing

Homework:

Create an algorithm which uses repetition to output  the 12 times table from 1 to 12.

Updates

2022-10-6: Added files used in this post.

© 2020  Vedesh Kungebeharry. All rights reserved. 

The Selection Construct

Sometimes we wish to change the order of processing based on certain conditions.

This can be accomplished by using a selection construct pictured below:

We will examine this in the following example:

Jim is a salesman for an auto car company. If his total sales for the week exceeds $10000 he is rewarded a sales commission of 8 percent of the sale, if not he is awarded no commission.

Create a flowchart algorithm which accepts the total sales and outputs the commission.

Exercise:

The problem is modified such that  if the total sales were less than $10000 he would receive a commission of 4 percent. See changes below:

Jim is a salesman for an auto car company. If his total sales for the week exceeds $10000 he is rewarded a sales commission of 8 percent of the total sales, if not he is awarded a commission of 4 percent.

Create a flowchart algorithm which accepts the total sales and outputs the commission.

Draw a flowchart for the modified scenario.

© 2020  Vedesh Kungebeharry. All rights reserved.