1 / 20

Understanding Conditionals in Programming

Learn how to use relational and logical operators, construct if/else and switch statements, and compare strings in programming. Objectives include mastering decision-making and Boolean values.

ypacker
Download Presentation

Understanding Conditionals in Programming

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. Chapter 4 Conditionals

  2. List relational operators. List logical operators. Use the hierarchy of operators chart to properly construct if/else statements. Construct switch statements. Use nested if statements. Objectives

  3. Decisions True and false values are also known as Boolean values, named after the 19th century English mathematician George Boole.

  4. A Boolean test compares primitives, constants, and variables and or objects using the following relational operators: Relational Operators Note: These relational operators must be typed exactly as above. You can’t type a space between the two equal signs and you can’t put =< to mean less than or equal.

  5. Alice Example of an if statement :

  6. Math Class

  7. String s1 = "Now is the winter of our discontent"; String s2 = "Java can be fun and hard "; String Methods

  8. String s1 = "Now is the winter of our discontent"; String s2 = "Java can be fun and hard "; String Methods

  9. Comparing Objects vs Primitives

  10. String Comparisons

  11. String Comparisons

  12. The syntax contains the keyword if, followed by a condition (boolean test) in parenthesis, followed by either a single statement or a block statement to execute if the test is true.   An optional else keyword provides the alternative statement to execute if the test is false.  if(condition) {    statements to do when conditional is true   } else {   statements to do when conditional is false   } If Statements

  13. if/else examples:

  14. Logical Operators

  15. Short-Circuiting Logical Operators

  16. The following only prints if x is less than y and x is less than z.  The && means if x is NOT less than y it won't even bother to test to see if x is less than z. if ( x < y  &&  x < z)               { System.out.println ("x is less than both y and z" ); } The following calculates grossPay if either condition is true.   The || means that if weeklyHours is less 40, it won't bother to test to see if employeeType is 'P'. if (weeklyHours < 40  ||  employeeType == 'P'  )       { grossPay =  weeklyHours * hourlyRate; } Using Logical Operators

  17. Switch Statements

  18. Complex If Statements vs. Switch

  19. Comparing Strings using if/else

  20. Comparing Strings using the Switch

More Related