In many problems the value of a variable or the result of an expression can define which statement or block of statements should be executed. In the exercises that follow, you will learn how to test if a value or the result of an expression belongs within a specific range of values (from a series of consecutive ranges of values).

Suppose that you want to display a message indicating the types of clothes a woman might wear at different temperatures.

Outdoor Temperature

(in degrees Fahrenheit)

Types of Clothes a Woman Might Wear
Temperature < 45 Sweater, coat, jeans, shirt, shoes
45 ≤ Temperature < 65 Sweater, jeans, jacket, shoes
65 ≤ Temperature < 75 Capris, shorts, t-shirt, tank top, flip flops, athletic shoes
75 ≤ Temperature Shorts, t-shirt, tank top, skort, skirt, flip flops

At first glance you might be tempted to use single-alternative decision structures. It is not wrong actually but if you take a closer look, it becomes clear that each condition is interdependent, which means that when one of these evaluates to true, none of the others should be evaluated. You need to select just one alternative from a set of possibilities.

There are actually two decision control structures that can be used for this purpose, and these are the multiple-alternative decision structure and nested decision control structures. However, the multiple-alternative decision structure is the best choice. It is more convenient and increases readability.

Exercise – Calculating the Discount

Write a program that calculates the discount that customers receive based on the dollar amount of their order. If the total amount ordered is less than $30, no discount is given. If the total amount is equal to or greater than $30 and less than $70, a discount of 5% is given. If the total amount is equal to or greater than $70 and less than $150, a discount of 10% is given. If the total amount is $150 or more, the customer receives a discount of 20%.

Solution

The following table summarizes the various discounts that are offered.

Range Discount
amount < $30 0%
$30 ≤ amount < $70 5%
$70 ≤ amount < $150 10%
$150 ≤ amount 20%

The program is as follows.

PHP

Java

C++

C#

Visual Basic

Python

A closer examination, however, reveals that the Boolean expressions (written in the marked lines) can be further improved. For example, when the first Boolean expression evaluates to false, the flow of execution continues to evaluate the second Boolean expression, in which, variable amount is definitely greater than or equal to 30. Therefore, the Boolean expression amount >= 30, when evaluated, is certainly true and thus can be omitted. The same logic applies to all cases; you can improve all Boolean expressions written in the marked lines. The final program is shown here, with all unnecessary evaluations removed.

PHP

Java

C++

C#

Visual Basic

Python

Exercise – Validating Data Input and Calculating the Discount

Rewrite the program of the previous exercise to validate the data input. Individual error messages should be displayed when the user enters any non-numeric or negative values.

Solution

Let’s try once again the “from inner to outer” method. The inner code fragment has already been discussed in the previous exercise, whereas the outer program that validates data input is shown here.

PHP

Java

C++

C#

Visual Basic

Python

Combining this with the program of the previous exercise, the final program becomes

PHP

Java

C++

C#

Visual Basic

Python

Notice: The marked lines are from the previous exercise

Leave a Reply

Your email address will not be published. Required fields are marked *

This article is taken from a book of mine. If you enjoyed it, please visit my author's page inand check out all of my books!