1 / 16

Compound Conditionals

Understand the concept of boolean variables and conditional statements in solving problems using programming algorithms.

naomip
Download Presentation

Compound Conditionals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Compound Conditionals BSU CS4MS - 4/9/19 Algorithmic Problem-Solving, Problem Solving with Programming http://www.cs.bsu.edu/cs4ms/docs/CompoundConditionalsSlides.pptx

  2. 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

  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 Condition 2

  6. Truth Table for OR The symbols || stands for the OR operator. Condition 1 Condition 2

  7. 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.

  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 (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, 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))

  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 countwas 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 countwas initialized to 7, 7 + 3 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.

More Related