Skip to main content

Section 4.7 WriteSelection-WE3-P1

Subsection 4.7.1 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 4.7.2 WrSelect-WE3-P1

Table for Q8

Table 4.7.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.

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.

2.

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.

3.

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}.

You have attempted of activities on this page.