Chapter 7 - Boolean expressions and if-else statements. This chapter also includes relational and logical operators, De Morgan’s Laws, the switch statement and enum data types. Another objective is to discuss team development and illustrate software reusability in the context of a simple but realistic case study, the Craps project.
Java Methods Object-Oriented Programming and Data Structures 2nd AP edition with GridWorld Maria Litvin ● Gary Litvin if (chapter == 7) Boolean Expressions and if-else Statements Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing All rights reserved 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, the switch statement • Learn about enum data types 72 if-else Statement if ( ) { < statements > } else { < other statements > } if ( ) { < statements > } else clause is optional 73 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 private boolean hasMiddleName; boolean isRolling = false; boolean true false Reserved words 74 Boolean Expressions • In if ( ) 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 75 Relational Operators , =, ==, != is equal to is NOT equal to 76 Relational Operators (cont’d) • Apply to numbers or chars: if ( count1 = numSlides) beep.play(); else slide++; } else { if (slide