Skip to main content
Engineering LibreTexts

4.17: Practice- Conditions

  • Page ID
    10653
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Review Questions

    True / False

    1. There are only two categories of control structures.
    2. Branching control structures are rarely used in good structured programming.
    3. If then else is a multiway selection control structure.
    4. The while control structure is part of the branching category.
    5. Pseudocode is better than flowcharting.

    Answers:

    1. false
    2. true
    3. false
    4. false
    5. false

    Expressions

    Evaluate the following Boolean expressions:

    1. 25 < 7
    2. 3 < 7
    3. 14 > 7
    4. 17 <= 7
    5. 25 >= 7
    6. 13 == 7
    7. 9 != 7
    8. 5 !> 7
    9. 25 > 39 || 15 > 36
    10. 19 > 26 || 13 < 17
    11. 14 < 7 && 6 <= 6
    12. 4 > 3 && 17 >= 7
    13. ! true
    14. ! (13 == 7)
    15. 9 != 7 && ! 1
    16. 6 < && 8

    Answers:

    1. 0
    2. 1
    3. 1
    4. 0
    5. 1
    6. 0
    7. 1
    8. Error, the “not greater than” is not a valid operator.
    9. 0
    10. 1
    11. 0
    12. 1
    13. 0
    14. 1
    15. 0
    16. Error, there needs to be an operand between the operators < and &&.

    Short Answer

    1. List the four categories of control structures and provide a brief description of each category.
    2. Create a table with the six relational operators and their meanings.

    Activities

    Complete the following activities using pseudocode, a flowcharting tool, or your selected programming language. Use separate functions for input, each type of processing, and output. Avoid global variables by passing parameters and returning results. Create test data to validate the accuracy of each program. Add comments at the top of the program and include references to any resources used.

    1. Create a program to prompt the user for hours and rate per hour and then compute gross pay (hours * rate). Include a calculation to give 1.5 times the hourly rate for any overtime (hours worked above 40 hours).[1]
    2. Create a program that asks the user how old they are in years. Then ask the user if they would like to know how old they are in (M)onths, (D)ays, (H)ours, or (S)econds. Use if/else conditional statements to calculate and display their approximate age in the selected timeframe. Do not perform any unnecessary calculations.
    3. Review MathsIsFun: US Standard Lengths. Create a program that asks the user for a distance in miles, and then ask the user if they want the distance in US measurements (yards, feet, and inches) or in metric measurements (kilometers, meters, and centimeters). Use if/else conditional statements to determine their selection and then calculate and display the results.
    4. Review MathsIsFun: Area of Plane Shapes. Create a program that asks the user what shape they would like to calculate the area for. Use if/else conditional statements to determine their selection and then gather the appropriate input and calculate and display the area of the shape.
    5. Create a program that helps the user determine what sock size to order based on their shoe size:
      < 4 = extra small
      4 to 6 = small
      7 to 9 = medium
      10 to 12 = large
      13+ = extra large

      Use if/else conditional statements to determine their selection and then display the results. Round half-sizes up to the next whole size. One option for rounding is to add 0.5 and then convert to an integer.
    6. If your programming language supports it, update one or more of the programs above to replace the if/else conditional statements with case/select conditional statements.
    7. Review Wikipedia: Is functions. If your programming language supports it, update one or more of the programs above to include input validation for all numeric input.
    8. If your programming language supports it, extend one or more of the programs above by adding structured exception handling statements (try-catch, try-except, etc.) to handle any runtime errors caused by the user entering invalid values for the input.

    References


    1. PythonLearn: Variables, expressions, and statements

    4.17: Practice- Conditions is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?