4.4.1. WriteSelection-WE3-P1¶
Subgoals for Writing Selection Statements
If Statement
Define how many mutually exclusive paths are needed
Order from most restrictive/selective group to least restrictive
Write if statement with Boolean expression
Follow with true bracket including action
Follow with else bracket
Repeat until all groups and actions are accounted for
OR Switch Statement
Determine variable / expression for mutually exclusive ranges
Write switch statement based on variable / expression
Each range is a ‘case’
Include break statements and default case if needed
WrSelect-WE3-P1
Table for Q8
Distance |
Cost |
---|---|
0 through 100 |
5.00 |
More than 100 but not more than 500 |
8.00 |
More than 500 but less than 1,000 |
10.00 |
1,000 or more |
12.00 |
Put the code in the right order to create a program that will generate a value 0 to 1500 inclusive, assign a value to double variable cost
depending on the value of integer variable distance
as shown in the table above, and then print both values.
Put the code in the right order to create a program that will generate an integer between 1 and 7, print the value, and display the name of the weekday where 1 is Sunday and 7 is Saturday.
Put the code in the right order to create a program that will generate an integer between 65 and 122 inclusive and then convert that code to a character through a cast. The program should then print the character value and print “Vowel” or “Consonant” depending on the character value. If the character is not a letter (between a and z or A and Z), then an error message should be printed. Vowels are considered to be {a, e, i, o, u}.