Selection
The type of programs we have written so far involve only sequences. That is we can follow the program starting at a statement and follow a sequence of statements one after the other. In other words there is a single path through the program. In this section we will look at problems where a sequence is inadequate.
Problem 1:
In Ontario, Canada, if you buy a single muffin, the muffin is considered to be a snack and subject to HST (13% tax). In fact, if you buy less than 6 muffins, they are subject to HST. However, if you buy 6 or more muffins, then they become groceries and therefore are not taxed.
Write a program that figures out the amount of money to charge on muffins. Muffins cost $0.90 each. The program will ask the user for the number of muffins they wish to buy. It will then calculate the following:
- cost of muffins (quantity * 0.90)
- cost of tax if applicable
- total cost
The program will display the above information.
Here is a sample run:
Please enter the number of muffins purchased: 5
Cost of muffins: 4.50
Taxes: 0.59
Total: 5.09
What we need to learn
- Learn about selection to allow multiple paths through program
- The nature of truth in C
Flowcharts
Flowcharts are diagrams that describe the flow (the sequences of statements) in your program. When you are first learning to program they can be a very useful tool. As you gain more experience, you may still find them useful in describing flow of broader steps.
How to use the flow chart
If the sequence of steps are one following the other without any sort of split, then the code for are simply placed in order.
If there is a decision (the diamond shape) we know we will need to use a control structure.
There are in general 2 types of control structures, selection and iteration. If there is a way to follow a path out of a decision box back into the same decision box, you will need an iteration type of control structure. The general look of such a flow chart is shown here:
In our situation, you can see that once you leave the decision box (either through yes or no) there is no way to go back into the decision. Thus, the type of control structure you will want to use is a selection.
Selection: The if and if/else statement
The basic method to perform selection in C is to use the if statement. The if statement allows you to do something if an expression is true, and do nothing if it is not.
Syntax:
if (expression){
//do the code in here if expression is true
}
You can also have an if/else statement. The if/else statement lets you choose to do one thing if expression is true, and a different thing if expression is false.
if (expression){
//do this code if expression is true
}
else{
//do this code if expression is false
}
To decide which to use, look at your flowchart. If the decision leads to two different processes, use an if/else statement. Below is the shape of a flow chart that shows you need an if/else:
If the flowchart leads to do something or doing nothing, then you will want to use an if statement. Below is the shape of the corresposnding flow chart for an if statment:
Given our current flow chart, our selection control should be an if/else.
Comparisons and the nature of true and false
In C, there are several operators that allow you to compare two values. These are listed below:
operator | what it does | examples |
---|---|---|
> | greater than | 6 > 3 --> true 3 > 6 --> false |
< | less than | 2 < 5 --> true 5 < 2 --> false |
>= | greater than or equal to | 4 >= 4 --> true 5 >= 4 3 >= 4 --> false |
<= | less than or equal to | 2 <= 2 --> true 1 <=2 3 <= 2 --> false |
== | equivalence, same as. Note the use of two equal signs, not 1. 1 equal sign is assignment, 2 equal sign is equivalance |
1 == 1 --> true 2 == 1 --> false |
!= | not equivalent, different | 4 != 3 --> true 3 != 3 --> false |
Unlike other languages, C does not have a strict true / false value. In other words you can throw in any expression for the if statement. If the expression evaluates to zero, the expression is false. If it evaluates to non-zero, then it is true.
For example:
if (3+2){
//code here always happens because 3+2 is 5, which is not 0 so its true
}
This is why it is so essential that you do not mix up assignment (=) and equivalence (==) operators.
int x = 0;
if (x = 5){
//this code will happen, because x = 5 assigns 5 to x
//and evaluates to 5 which is not 0, so true.
// x == 5 would be false but single = sign does not mean
//equivalence
}
Now, with all that, lets go back to our program. Using the flow chart, I can map out the program with a few quick comments. This technique can help you translate your diagram to your code:
#include <stdio.h>
int main(void){
int numberOfMuffins;
double taxes;
double muffinCost;
double totalCost;
//ask and read in the number of muffins
//calculate cost of muffin
//select between charging taxes
//and setting it to 0
//calculate total cost
//print out result
return 0;
}
Most of the above program involve concepts you already know:
#include <stdio.h>
int main(void){
int numberOfMuffins;
double taxes;
double muffinCost;
double totalCost;
printf("Please enter the number of muffins purchased: ");
scanf("%d",&numberOfMuffins);
muffinCost = numberOfMuffins * 0.90;
//select between charging taxes
//and setting it to 0
totalCost = muffinCost + taxes;
printf("Cost of muffins: %.2lf\n",muffinCost);
printf("Taxes: %.2lf\n",taxes);
printf("Total: %.2lf\n",totalCost);
return 0;
}
Now all we need to do is write the selection statement. As stated earlier, the type of selection we will do is an if/else because we will set the tax to 13% of muffinCost if we buy less than 6 and 0 otherwise. Thus the selection statement is:
if(numberOfMuffins < 6){
taxes = 0.13 * muffinCost;
}
else{
taxes = 0;
}
Now, putting it all together:
#include <stdio.h>
int main(void){
int numberOfMuffins;
double taxes;
double muffinCost;
double totalCost;
printf("Please enter the number of muffins purchased: ");
scanf("%d",&numberOfMuffins);
muffinCost = numberOfMuffins * 0.90;
if(numberOfMuffins < 6){
taxes = 0.13 * muffinCost;
}
else{
taxes = 0;
}
totalCost = muffinCost + taxes;
printf("Cost of muffins: %.2lf\n",muffinCost);
printf("Taxes: %.2lf\n",taxes);
printf("Total: %.2lf\n",totalCost);
return 0;
}
An alternative flow chart
As always there can be more than one way to write a program. If we were to initialize the taxes to 0 (by default assume someone will buy more than 6 muffins) and only calculate should they buy less, our program's flow chart would look a bit different.
The code from this diagram will also look a bit different:
#include <stdio.h>
int main(void){
int numberOfMuffins;
double taxes=0;
double muffinCost;
double totalCost;
printf("Please enter the number of muffins purchased: ");
scanf("%d",&numberOfMuffins);
muffinCost = numberOfMuffins * 0.90;
if(numberOfMuffins < 6){
taxes = 0.13 * muffinCost;
}
totalCost = muffinCost + taxes;
printf("Cost of muffins: %.2lf\n",muffinCost);
printf("Taxes: %.2lf\n",taxes);
printf("Total: %.2lf\n",totalCost);
return 0;
}
Both solutions are correct. As their plans are slightly different, their code is slightly different.