Download presentation
Presentation is loading. Please wait.
1
Compound Conditionals
Ball State University - CS4MS - Fall 2018
2
What is a boolean? A variable that has 2 possible values (True or False) A boolean is a name for something that can only be True or False To fully understand what conditional statements are, you first need to know what a boolean is.
3
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.
4
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”
5
Truth Table for AND The symbols && stands for the AND operator.
Condition 1 AND True False TRUE FALSE Condition 2
6
Truth Table for OR The symbols || stands for the OR operator.
Condition 1 OR True False TRUE FALSE Condition 2
7
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.
8
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).
9
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)
10
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.
11
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 (and NOT 25), this statement is FALSE.
12
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.
13
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))
14
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.
15
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, equals 10 which is indeed less than 15, so this statement is TRUE.
16
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.