Introduction

This topic introduces you to some fundamental coding practices for decision structure where the values being compared are discreet and known. Typical data types used, which will always be discreet are int and char.

Demo Problem

Calculate the bill for internet service based on the table below:

Package Cost Calculation
A $9.95 per month for up to 10 hours;
additional hours are billed at $2.00 per hour
B $13.95 per month for up to 20 hours;
additional hours are billed at $1.00 per hour
C $19.95 per month for unlimited hours

Input Variables

  • package (char data type)
  • hours (int data type)

Output Variables

  • bill (double data type)

switch-demo-1

User Input

switch-demo-2

If the user enters A or B then prompt the user for the number of additional hours:
switch-demo-3

Switch Structure

This problem can be solved using an if-then-else structure but as the type of package is known and discreet, a switch structure can be used instead.
switch-demo-4

Inside the switch structure add the following tests:

  1. Test for package A:
    switch-demo-5
  2. Test for package B:
    switch-demo-6
  3. Test for package C and add a default case:
    switch-demo-7
    Note: the default case is used in case the user does not enter a valid character for the switch(package). Is there another option to account for this?

Output

Add the following code for the output:
switch-demo-8

Test Plan

Even though you have coded a solution to the problem does not mean the solution is always going to work. Therefore, before executing the solution you need to create a test plan to outline all possible scenarios.

Scenario Hours Bill Y/N
A 9 9.95  
A 10 9.95  
A 11 11.95  
B 19 13.95  
B 20 13.95  
B 21 14.95  
C N/A 19.95  
D N/A ??  

Execute Solution

When you execute the solution, you will test all the scenarios of your Test Plan. The results are shown below:
switch-demo-test-1
switch-demo-test-2
switch-demo-test-3
switch-demo-test-4
switch-demo-test-5
switch-demo-test-6
switch-demo-test-7
switch-demo-test-8

From these results, return to your Test Plan:

Scenario Hours Bill Y/N
A 9 9.95 Y
A 10 9.95 Y
A 11 11.95 Y
B 19 13.95 Y
B 20 13.95 Y
B 21 14.95 Y
C N/A 19.95 Y
D N/A ?? Y

Decisions Home

CPSC1012 Home