Skip to main content

Section 6.5 WriteSelection-WE2-P1

Subgoals for Writing Selection Statements.

If Statement
  1. Define how many mutually exclusive paths are needed
  2. Order from most restrictive/selective group to least restrictive
  3. Write if statement with Boolean expression
  4. Follow with true bracket including action
  5. Follow with else bracket
  6. Repeat until all groups and actions are accounted for
OR Switch Statement
  1. Determine variable / expression for mutually exclusive ranges
  2. Write switch statement based on variable / expression
  3. Each range is a ‘case’
  4. Include break statements and default case if needed

Subsection 6.5.1

Table for Q8
Table 6.5.1.
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

Exercises Exercises

1.
Q8: Put the code in the correct 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.
2.
Q9: Put the code in the correct 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.
3.
Q10: Put the code in the correct 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}.
You have attempted of activities on this page.