Compound Conditionals Algorithmic ProblemSolving Problem Solving with Programming
Compound Conditionals Algorithmic Problem-Solving, Problem Solving with Programming BSU CS 4 MS - 4/9/19 http: //www. cs. bsu. edu/cs 4 ms/docs/Compound. Conditionals. Slides. pptx
What is a boolean? - A variable that has 2 possible values (True or False) Boolean is the name for something that can only be True or False True Yes No False
What is a conditional statement? - Tells a program to do different actions depending on whether a boolean condition is true or false. These are usually “if” statements. Manipulate the control flow of a program. The control flow is the order that instructions run in a program.
Symbols used in conditionals == means “equal to” ! means “not” || means “or” != means “not equal to” && means “and” > means “greater than” Anything inside ( ) is grouped < means “less than”
Truth Table for AND The symbols && stands for the AND operator. Condition 2 Condition 1 AND True False True TRUE FALSE False FALSE
Truth Table for OR The symbols || stands for the OR operator. Condition 2 Condition 1 OR True False True TRUE False TRUE FALSE
Truth Table for NOT The symbol ! stands for the NOT operator. NOT Condition 1 True False FALSE TRUE The NOT operator switches a condition or boolean statement to its OPPOSITE.
Steps To Solve Conditional Statements 1) Pay attention to the order of operations. a) Simplify expressions inside parentheses first. 2) Then apply operations outside parentheses. NOTE: String variables are formatted inside quotation marks and need to match exactly for an == expression to evaluate to true. NOTE: Var stands for variable (which acts as a “placeholder” for a known or unknown entity).
Example 1 Two variables, month and day, have been initialized with the values shown below: var month = “January”; var day = 26; Does the following conditional statement evaluate to True or False? (month == “January”) && (day == 25)
Example 1 (Solution - Step 1) Two variables, month and day, have been initialized with the values shown below: var month = “January”; var day = 26; Does the following conditional statement evaluate to True or False? (month == “January”) && (day == 25) Begin by looking at the expression within the first parentheses: (month == “January”) The == symbol means “equal to”, and since the variable month was initialized to “January”, this expression is TRUE.
Example 1 (Solution - Step 2) Two variables, month and day, have been initialized with the values shown below: var month = “January”; var day = 26; Does the following conditional statement evaluate to True or False? (month == “January”) && (day == 25) Now, we are left with the following: TRUE && (day == 25) Next, simplify the expression in the remaining parentheses. Since the variable day was initialized to 26 (NOT 25), this statement is FALSE.
Example 1 (Solution - Step 3) Two variables, month and day, have been initialized with the values shown below: var month = “January”; var day = 26; Does the following conditional statement evaluate to True or False? (month == “January”) && (day == 25) Now, we are left with the following statement: TRUE && FALSE The && symbol means “and”. For an “and” expression to be true, BOTH statements need to be true. Therefore, the above statement is FALSE! Our final answer is FALSE.
Example 2 Two variables, count and name, have been initialized with the values shown below: var name = “Jacob”; var count = 7; Does the following conditional statement evaluate to True or False? ((count > 7) || ((count + 3) < 15))
Example 2 (Solution - Step 1) Two variables, count and name, have been initialized with the values shown below: var name = “Jacob”; var count = 7; Does the following conditional statement evaluate to True or False? ((count > 7) || ((count + 3) < 15)) Begin by looking at the expression within the first parentheses inside the outer parentheses: (count > 7) The > symbol means “greater than”, and since the variable count was initialized to 7, this expression is FALSE because 7 is not greater than 7. They are equal.
Example 2 (Solution - Step 2) Two variables, count and name, have been initialized with the values shown below: var name = “Jacob”; var count = 7; Does the following conditional statement evaluate to True or False? ((count > 7) || ((count + 3) < 15)) Now, we are left with the following: ((FALSE) || ((count + 3) < 15)) Next, simplify the expression in the second inner set of parentheses: ((count + 3) < 15)) Since the variable count was initialized to 7, 7 + 3 equals 10 which is indeed less than 15, so this statement is TRUE.
Example 2 (Solution - Step 3) Two variables, count and name, have been initialized with the values shown below: var name = “Jacob”; var count = 7; Does the following conditional statement evaluate to True or False? ((count > 7) || ((count + 3) < 15)) Now, we are left with the following: FALSE || TRUE Using the truth table for OR, we can simplify this expression to TRUE. Our final answer is TRUE.
- Slides: 16