160 likes | 171 Views
Understand the concept of boolean variables and conditional statements in solving problems using programming algorithms.
E N D
Compound Conditionals BSU CS4MS - 4/9/19 Algorithmic Problem-Solving, Problem Solving with Programming http://www.cs.bsu.edu/cs4ms/docs/CompoundConditionalsSlides.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 Yes No True False On Off
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 “or” && means “and” Anything inside ( ) is grouped ! means “not” != means “not equal to” > means “greater than” < means “less than”
Truth Table for AND The symbols && stands for the AND operator. Condition 1 Condition 2
Truth Table for OR The symbols || stands for the OR operator. Condition 1 Condition 2
Truth Table for NOT The symbol ! stands for the NOT operator. NOT Condition 1 The NOT operator switches a condition or boolean statement to its OPPOSITE.
Steps To Solve Conditional Statements • Pay attention to the order of operations. • Simplify expressions inside parentheses first. • 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, countand 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 countwas 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 countwas 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.