Programming Fundamentals/Practice: Conditions

Chapter Summary

 * Structured Programming - Structured programming is a programming paradigm aimed at improving the clarity, quality, and development time of a computer program by making extensive use of the structured control flow constructs of selection, repetition, block structures, and subroutines.
 * Selection Control Structures - Conditional statements perform different computations or actions depending on whether a programmer-specified Boolean condition evaluates to true or false.
 * If Then Else - A two-way selection structure common across many programming languages.
 * Code Blocks - A lexical structure of source code which is grouped together.
 * Relational Operators - Tests or defines some kind of relation between two entities.
 * Assignment vs Equality - Assignment sets a value to the memory address location of a variable. The left operand is set to the value of the right operand and is denoted with a single equals sign (=). Equality is a relational operator used to compare or test two operands and is often denoted with two or three equals signs (==, ===). The result of such a comparisons returns true or false.
 * Logical Operators - Symbol or word used to connect two or more expressions such that the value of the compound expression produced depends only on that of the original expressions and on the meaning of the operator.
 * Nested If Then Else - Two-way selection structures may be nested inside other two-way selection structures, resulting in multi-way selection.
 * Case Control Structure - A case or switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via a multiway branch.

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.
 * 6) In a sequence control structure, the program executed path is chosen by options prompted by questions.
 * 7) An iteration control structure allows some lines of code to be executed many times.
 * 8) A relational operator is an operator used to create complex Boolean expressions.
 * 9) The best way to structure an if-then-else statement is to check if a statement is True.
 * 10) The value of the statement "True and False" is 'True'.
 * 11) To initialize or set the value of a variable, you should use the equality operator (==)

Answers:


 * 1) false - There are three.
 * 2) true
 * 3) false - It is part of the selection structure.
 * 4) false - It is part of the repetition category.
 * 5) false - While pseudocode may be very useful, it does not have the same clarity as flowcharting.
 * 6) false - Sequence structure programs execute items in the order listed.
 * 7) true
 * 8) false - They are used to test relationships between values.
 * 9) false - In general, it is better to test for False or not False rather than True or not True.
 * 10) false - The statement's value is 'False'.
 * 11) false - Use the assignment operator (=)

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:


 * 0
 * 1
 * 1
 * 0
 * 1
 * 0
 * 1
 * 1) Error, the “not greater than” is not a valid operator.
 * 0
 * 1
 * 0
 * 1
 * 0
 * 1
 * 0
 * 1) 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.
 * 3) Provide examples of when it is appropriate to use "if" vs. "if-else" vs. "if-else if-else".
 * 4) Describe the differences between Assignment and Equality and provide examples of each being used properly.

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). For example, 50 hours worked at $10 per hour with overtime is $550.
 * 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) Review  Aging in dogs. Create a program to prompt the user for the name of their dog and its age in human years. Calculate and display the age of their dog in dog years, assuming the first two years equal 10.5 years each, with subsequent years equaling four human years. Be sure to include the dog's name in the output, such as:
 * 6) 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.
 * 7) 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.
 * 8) Review  Is functions. If your programming language supports it, update one or more of the programs above to include input validation for all numeric input.
 * 9) 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.