Programming Fundamentals/Practice: Conditions
- Structured Programming
- Selection Control Structures
- If Then Else
- Code Blocks
- Relational Operators
- Assignment vs Equality
- Logical Operators
- Nested If Then Else
- Case Control Structure
True / False
- There are only two categories of control structures.
- Branching control structures are rarely used in good structured programming.
- If then else is a multiway selection control structure.
- The while control structure is part of the branching category.
- Pseudocode is better than flowcharting.
- In a sequence control structure, the program executed path is chosen by options prompted by questions.
- An iteration control structure allows some lines of code to be executed many times.
- A relational operator is an operator used to create complex Boolean expressions.
- The best way to structure an if-then-else statement is to check if a statement is True.
- The value of the statement "True and False" is 'True'.
- false - There are three.
- false - It is part of the selection structure.
- false - It is part of the repetition category.
- false - While pseudocode may be very useful, it does not have the same clarity as flowcharting.
- false - Sequence structure programs execute items in the order listed.
- false - They are used to test relationships between values.
- false - In general, it is better to test for False or not False rather than True or not True.
- false - The statement's value is 'False'.
Evaluate the following Boolean expressions:
- 25 < 7
- 3 < 7
- 14 > 7
- 17 <= 7
- 25 >= 7
- 13 == 7
- 9 != 7
- 5 !> 7
- 25 > 39 || 15 > 36
- 19 > 26 || 13 < 17
- 14 < 7 && 6 <= 6
- 4 > 3 && 17 >= 7
- ! true
- ! (13 == 7)
- 9 != 7 && ! 1
- 6 < && 8
- Error, the “not greater than” is not a valid operator.
- Error, there needs to be an operand between the operators < and &&.
- List the four categories of control structures and provide a brief description of each category.
- Create a table with the six relational operators and their meanings.
- Provide examples of when it is appropriate to use "if" vs. "if-else" vs. "if-else if-else".
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.
- 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).
- 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.
- 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.
- 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.
- 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 largeUse 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.
- 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.
- 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.
- 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.