Introduction to Select Case Logic Structure

We have been tasked with writing a program for a veterinary clinic which specializes in only cats and dogs.  They see approximately 100 animals dogs everyday.  Their problem is that they are constantly asked to determine the age in human years of dogs, cats, horses, rabbits, and gerbils.  Cats have a human year multiplier of seven, dogs six years, rabbits ten years, horse four year and gerbils 20 year multiplier.  They want you to write a program that will tell them the human year equivalent for their pet.  Be sure you use the Select Case Logic structure in your code. 

First Let's do the problem statement for this:
Assumptions
Input
Calculations
Output
Answer

 

 

 

 

 

 

 

 

Problem Statement
Assumptions:
Will only have five types of animal
Will only have two types of living arrangements
Only type and years since birth will determine human equivalent
Only whole years will be used.
cat years = 7
dog years = 6
rabbit years = 10
horse years = 4
gerbil years = 20

Input:
Type of animal
How many years it has been living (years alive)

Calculations
If a type of animal = cat then
     human years = years alive *  cat years
Else If a type of animal = dog then
     human years = years alive * dog years
Else If a rabbit then
     human years = years alive* rabbit years
Else if a horse then
     human years = years alive* horse years
Else if a gerbil then
     human years = years alive* gerbil years

Output:
Your pet's age in human years is "human years"

Now write the human Algorithm  Answer

 

 

 

 

 

 

 

 

 

 

Human Algorithm:
Remember, we create the human algorithm from the problem statement (although we can go the other way if you find that creating the human algorithm is easier.  We start out by declaring our constants (1-5), inputting our data (6-7), doing the math (8-17) then outputting the answer (148

Human Algorithm

1.  cat years = 7
2.  dog years = 6
3.  rabbit years = 10
4.  horse years = 4
5.  gerbil years = 20
 

6.  Ask What Type of animal
7.  Ask How many years it has been living (years alive)
 

8. If a type of animal = cat then
9.     human years = years alive *  cat years
10.Else If a type of animal = dog then
11.     human years = years alive * dog years
12.Else If a rabbit then
13.    human years = years alive* rabbit years
14.Else if a horse then
15    human years = years alive* horse years
16.Else if a gerbil then
17.    human years = years alive* gerbil years
 

18. Output: Your pet's age in human years is "human years"

Note, in the past we didn't number "else" because if was the opposite side of the "if" decision.  We do number "else if" because it is a whole other decision that has it's own shape in the flowchart.

Problem statement from above

Assumptions:
cat years = 7
dog years = 6
rabbit years = 10
horse years = 4
gerbil years = 20

Input:
Type of animal
How many years it has been living (years alive)

Calculations
If a type of animal = cat then
     human years = years alive *  cat years
Else If a type of animal = dog then
     human years = years alive * dog years
Else If a rabbit then
     human years = years alive* rabbit years
Else if a horse then
     human years = years alive* horse years
Else if a gerbil then
     human years = years alive* gerbil years

Output:
Your pet's age in human years is "human years"

 


Now Create the flowchart   Answer

 

 

 

 

 

 

 

 

Flow Chart:  Visual Logic does not display Select Case.  You must use If...elseif..else logic then code it using select case

The flowchart has gotten to large to show side by side with the human algorithm but each line of the human algorithm is another symbol in the flowchart.

 

 

 

 

 

 

 

 

 

Now Create the VB Script   Answer

 

 

 

 

 

 

 

VB Script Code:

The flowchart has gotten to large to display side by side but each step in the flowchart is another line of code


<script language = vbscript>
Option Explicit

Dim catyears, dogyears, rabbityears, horseyears, gerbilyears
Dim animaltype, animalage, humanage
'Assignments
catyears = 7
dogyears = 6
rabbityears = 10
horseyears = 4
gerbilyears = 20
'input
animaltype= inputbox("dog, cat, horse, rabbit, gerbil")
Animalage = Inputbox("What is the animal's age")
'calculations
Select Case animaltype '<--places what is in animal type into a register

Case "dog" '<--sends dog to a register to compare with what is in animaltype
humanage = animalage * dogyears

case "cat"
Humanage = animalage * catyears

case "rabbit"
Humanage = animalage * rabbityears

case "horse"
Humanage = animalage * horseyears

case "gerbil"
Humanage = animalage * gerbilyears

case else                                    '<-- Case Else is what happens when there is no match
msgbox("Your entry didn't match dog, cat, rabbit, horse or gerbil")
Humanage = 0
End Select '<--Ends the select statement

'Output
If humanage > 0 then
document.write "The age of the " & animaltype & " is " & humanage
else
document.write "You didn't type in dog, cat, rabbit, horse, or gerbil"
end if
</script>