Next: Summary Up: Introduction to structured design Previous: Introduction to structured design


Conditional Control Structures

In Section 5.2 it was shown that algorithms can be described using a few concepts, namely, sequence, conditional execution (or selection) and repetition. The idea of assignment was also used.

In designing programs it is best to proceed with algorithm/program design in a top-down structured manner, that is by first recognising the major components of the solution, then expressing the solution as a sequence of these major components and then expanding the major components themselves similarly. These ideas have already been illustrated in Lesson 5. They are further expanded below.

Consider the example C++ program that computes the area and perimeter of a rectangle (see Lesson 6) when the length and width are entered by a user. A possible initial algorithm is as follows, where each step is numbered:

1. Enter the length and width of the rectangle.

2. Calculate the area and perimeter of the rectangle.

3. Output the area and the perimeter.

Notice that the idea of sequence has been used here, that is that the operations are carried out in the order they are written. This is the simplest way of structuring the solution to a problem. Each step in the above is now expanded as follows:

1. Enter
   1.1 Prompt user to enter length
   1.2 Enter length
   1.3 Prompt user to enter width
   1.4 Enter width

2. Calculate
   2.1 Calculate perimeter as twice sum
       of length and width
   2.2 Calculate area as product of length and width

3. Output
   3.1 Output perimeter
   3.2 Output area

At this stage the problem has now been completely solved independent of the language the program is to be written in. It is now simple to write the program in any suitable programming language. In fact in Lesson 6 this program was given as an example and Lesson 7 covered enough C++ to allow this program to be written in C++.

Unfortunately not all problems are so simple to solve. Frequently the simple idea of sequence used above is insufficient to describe the solution of many problems.
Consider the following problem:

Write a program which enters the number of hours worked in a week and an hourly rate of pay of an employee. The program should output the wage of the employee. The employee is paid at the normal hourly rate for the first forty hours and subsequently at one and a half times the hourly rate.

The problem solution now appears fairly straightforward and modelling it on the previous case an algorithm is written as follows:

1. Enter the hours worked and the hourly rate.

2. Calculate the wage.

3. Output the wage.

However in attempting to expand step 2, the simple idea of sequence is not sufficient. This is because if the number of hours worked is less than or equal to forty then the final wage is the number of hours multiplied by the hourly rate whereas if more than forty hours are worked then it is the hourly rate for the first forty hours and one and a half times the hourly rate for the remaining hours. Thus the truth of a condition `number of hours less than or equal to forty' determines which calculation should be carried out. Such a conditional statement has already been encountered in Lesson 5. There it was introduced as being a statement which tests a condition and depending on the result of the test carries out one operation or another. Thus using a conditional statement the algorithmic solution above could be expanded as follows:

1. Enter
   1.1 Prompt user for hours worked.
   1.2 Enter hours worked.
   1.3 Prompt user for hourly rate.
   1.4 Enter hourly rate.

2. Calculate wage
   2.1 If hours worked is less than or equal to forty
       then
          2.1.1 calculate normal wage.
       otherwise
          2.1.2 calculate over hours wage.
3. Output the wage

The details of working out the wage are not important here, what is important is that in describing the solution a conditional statement was used. Conditional statements are often characterised by the words

if condition then A else B
which carries out the process A if the condition evaluates to true and otherwise carries out the process B. All programming languages have some form of conditional statement. The conditional statements available in C++ are considered in the following few lessons.



Next: Summary Up: Introduction to structured design Previous: Introduction to structured design