Skip to main content

Section 4.2 WriteConditional-WE2-P1

Subgoals for writing conditionals:.

  1. Define how many mutually exclusive paths are needed
  2. Order paths from most restrictive to least restrictive
  3. Write conditional statement with Boolean expression
  4. Follow with true path including action in indentation
  5. Repeat 3 and 4 until all paths are included
  6. (Optional) Follow with else path including action in indentation

Subsection 4.2.1 WrCond-WE2-P1

Exercises Exercises

1.
Put the code in the right order to create a program that will ask the user for an integer between 0 and 25, print the value, and if the value is greater than 18, print a message saying “This is an overload”; otherwise print a message saying “Not an overload”.
2.
Put the code in the right order to create a program that will ask the user for an integer value between 3 and 7, print the value, and if the value is equal to 5 then set the string variable class_type equal to ‘G’, and print a message saying “graduate student”; otherwise set class_type equal to ‘U’ and print a message saying “undergraduate”. Finally, print the class_type.
3.
Put the code in the right order to create a program that will ask the user for an integer to represent a year (1500 to 3000 inclusive) and print the value. Then it will print true if the year is a leap year, otherwise the program prints false. A leap year is a multiple of 4, with one exception: if the year is a multiple of 100 but not 400, then it is not a leap year. For example, the year 1900 was not a leap year, but the year 2000 was indeed a leap year.
4.
Put the code in the right order to create a program that will ask the user for an integer grade (0 to 100 inclusive) and print the value. Then determine the letter grade where 90 to 100 is an ‘A’, 80 to 89 is a ‘B’, 70 to 79 is a ‘C’, 60 to 69 is a ‘D’, and 0 to 59 is an ‘F’. Then print the letter grade.
5.
Put the code in the right order to create a program that will ask the user for 3 numbers (1 to 10 inclusive) for variables a, b, and c, and print the value of each variable. Then compare a to b, a to c, and b to c, and print statements for each comparison, reporting for each pair of variables if they are equal, greater than, or less than.
You have attempted of activities on this page.