Volume purchase discount – multiple levels
Calculating volume discounts on purchase quantity volumes based on multiple levels of quantity slabs.
Learning Objectives
- Learning a true if-else-if ladder case where multiple test conditions as a sequence of mutually exclusive ranges are present.
Source Code
|
|
Run Output
Code Understanding
if (vol >=1000) discount=30;
first slab of volume of higher end is usually given as the first test condition.
else if (vol <1000 && vol >=500) discount=20;
Here is the second slab setting where further check of if with a new test condition is done.
else if (vol <500 && vol >=100) discount=10;
This one is the third slab setting with another range check with in another if ladder step.
else discount=0;
This one is where all other condition checks are exhausted.
Notes
- A programmer is usually advised to test the most frequently occurring range condition as the first test condition. Here we have checked for higher condition first. We can also check for lower range first as an alternative solution.
if (vol < 100) discount=0;
else if (vol >=100 && vol <500) discount=10;
else if (vol >=500 && vol <1000) discount=20;
else discount = 30; - The mutually exclusive range conditions are advised to either the increasing or in decreasing order.
Common Errors
- Please watch for use of && operator (and not || operator) for inside range check. Inside range check is most common for slab wise checking.
Suggested Filename(s): vol-discount.cpp, qty-disc.cpp
Created: 27-Dec-2017 00:28 | Updated: 15-Sep-2018 11:48|
