1 / 18

Objectives:

Objectives:. Learn about the boolean data type Learn the syntax for if-else statements Learn about relational and logical operators, De Morgan’s laws, short-circuit evaluation Learn when to use nested if-else statements, if-else-if sequences. if Statement. if ( < condition > ) {

troy-hays
Download Presentation

Objectives:

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. Objectives: • Learn about the boolean data type • Learn the syntax for if-else statements • Learn about relational and logical operators, De Morgan’s laws, short-circuit evaluation • Learn when to use nested if-else statements, if-else-if sequences

  2. if Statement if ( <condition> ) { < statements > }

  3. boolean Data Type • George Boole (1815 - 1864) • boolean variables may have only two values, true or false. • You define boolean fields or boolean local variables the same way as other variables. boolean true false Reserved words

  4. Boolean Expressions • In if (<condition> )<condition> is a Boolean expression. • A Boolean expression evaluates to either true or false. • Boolean expressions are written using boolean variables and relational and logical operators.

  5. Relational Operators ==, !=, <, >, <=, >= is equal to is NOT equal to

  6. Relational Operators (cont’d) • Apply to numbers or chars: if ( count1 <= count2 ) ... if ( sum != 0 ) ... if ( letter == 'Y' ) ... • Do not use == or != with doubles because they may have rounding errors double x = 7.0; double y = 3.5; if (x / y == 2.0) ... May be false!

  7. if-else Statement if ( <condition> ) { < statements > } if ( <condition> ) { < statements > } else { < other statements > } else clause is optional

  8. Nested if-else if (condition) {statements}; else if (condition) {statements}; else if (condition) {statements}; else {statements}; }

  9. if-else-if int x = some number; if( x > 30 ) { System.out.print("Value of X is greater than 30"); } else if( x > 20 ) { System.out.print("Value of X is greater than 20"); } else if( x >10 30 ) { System.out.print("Value of X is greater than 10"); }else { System.out.print("This is less than 10"); } } }

  10. if (...) if (...) statement1; else statement2; ... Common if-else Errors Extra semicolon: Missing braces: • if (...) ; • { • statements; • ... • } if (...) statement1; statement2; ... It is safer to always use braces in if-else

  11. Relational Operators (cont’d) • Be careful using == and != with objects (for example, Strings): they compare references (addresses) rather than values (the contents) String cmd = console.readLine(); if ( cmd == "Help" ) ... Wrong! (always false)

  12. Relational Operators (cont’d) • Use the equals or equalsIgnoreCase methods to compare Strings: or • String cmd = console.readLine(); • if ( cmd.equals ("Help") ) ... • if ( "Help".equals (cmd) ) ...

  13. Relational Operators (cont’d) • Use the == or != operator with strings and other objects when you want to know whether or not this is exactly the same object. • Also use == or != to compare to null. • String text = file.readLine( ); • if ( text != null ) ...

  14. Logical Operators &&, ||, ! and or not

  15. Logical Operators (cont’d) • ( condition1 && condition2 ) is true if both condition1 and condition2 are true • ( condition1 || condition2 ) is true if condition1 or condition2 (or both) are true • !condition1 is true if and only if condition1 is false

  16. Logical Operators (cont’d) • &&, ||, and ! obey the laws of formal logic called De Morgan’s Laws: • Example: if ( ! ( x => -10 && x <= 10 ) ) ... if ( x < -10 || x > 10 ) ... ! (p && q) == ( !p || !q ) ! (p || q) == ( !p && !q ) Easier to read

  17. Ranks of Operators Highest !-(unary) ++ -- (cast) * /% + - < <= > >= == != && || Easier to read Lowest if ( ( ( year % 4 ) == 0 ) && ( month == 2 ) ) ... if ( year % 4 == 0 && month == 2 ) ...

  18. if (condition1&&condition2) ... If condition1 is false, then condition2 is not evaluated (the result is false anyway) if (condition1||condition2) ... If condition1 is true, then condition2 is not evaluated (the result is true anyway) Short-Circuit Evaluation if ( x >= 0 && Math.sqrt (x) < 15.0) ... Always OK: won’t get to sqrt if x < 0

More Related