370 likes | 463 Views
Chapter 3. Selection in Java. b oolean. The boolean data type has only two possible values: true and false . Declaration: boolean varname ; Boolean operations generally compare values and determine whether the asserted relationship is true or false.
E N D
Chapter 3 Selection in Java
boolean • The boolean data type has only two possible values: true and false. • Declaration: booleanvarname; • Boolean operations generally compare values and determine whether the asserted relationship is true or false. • Boolean operations are stricter than C/C++ where false is zero and true is non-zero!
Boolean operators • < // 4 < 5 is true, 5 < 4 is false • <= • == // 4 == 2+2 is true, 5 == 3+3 is false • > // 3 > 1 is true, 2 > 4 is false • >= • != // ‘A’ != ‘Z’ is true, 0 != 4-3+1 is false
The if statement • Keyword is lower case • Boolean expression inside parentheses () • Can be followed by a single statement or a block of statements enclosed in curly braces {} • The statement or block will only be executed if the booleanexpression evaluates to true • Do not put a ‘;’ after the boolean expression
The else statement • The else statement is processed if the boolean expression evaluates to false. • It can be a single statement or a block of statements enclose in curly braces {} • Be careful about which if statement an else statement matches with in nested if statements
Logical operators • ! // “not” !true == false, !false == true • && // “and” x && y is true only if both x and y are true, otherwise it is false • || // “or” x || y is true if either x or y is true, it can only be false if both are false • ^ // “exclusive or” x ^ y is true if x and y are opposite values. If both are true or both are false, it is false.
Combining boolean expressions:The case of an historic software error
The switch and case statements • The switch(expression) uses the value of the expression to select among choices represented by the values associated with the list of case value statements • The list of statements after the selected case statement is executed until a break statement is encountered • The default statement is executed if no case value matches the expression
The conditional ? : expression • ( test-exp)? true-exp: false-exp • The test expression is enclosed with () • If the test expression is true, the expression after the ‘?’ is returned • If the test expression is false, the expression after the ‘:’ is returned • Short for if(test-exp) true-expelse false-exp
The printf method • System.out.printf(format, item1, item2, ….); • format is a string expression with embedded specifiers that control formatting • Specifiers consist of ‘%’ with formatting codes • Each item must have a matching specifier that is compatible with the item type • Each item specifier must have a corresponding item in the list
The printf() specifiers • %b // boolean • %c // character • %d // decimal integer • %f // floating point number • %e // number in scientific notation • %s // string • %% // print the ‘%’ character
Tips for printf() • Works like fprintf() in C/C++ language • Escape characters such as /n, /”, etc. work • Format string can be a string expression composed of concatenated expressions and method calls • Items can be expressions that evaluate to the correct type for the print specifier • Specifiers must match items in order
Padding and field sizing • For all specifiers, a number after the ‘%’ specifies the width of the field; for example, %8c will print a character with 7 spaces padding in front • %8.3e specifies a total field width of 8 spaces, including the ‘.’ and ‘e’ with 3 digits to the right of the decimal place.
Operator Precedence (highest to lowest) • var++, var— • ++var, --var, !, (typecast) , unary +, unary – • *, /, % • +, - • <, <=, >, >= • ==, != • ^ • && • || • ? : • =, +=, -=, *=, /=, %=
http://www.cis.upenn.edu/~palsetia/java/precedenceTable.html
Chapter 4 Looping Constructs
Comparing looping constructs • Java has while() {} and do {} while();loops • The do {} while();loop executes at least once before testing the exit condition • The while() {} loop may not execute at all if the exit condition is already met at the start
Break; will exit the inner-most loop immediately and unconditionally • Continue; will jump to the exit condition test and begin a new iteration if it is still true
The for loop • Syntax is just like C/C++ for loop format: for (initialization ; exit test; post loop action) • Initialization can be multiple comma-separated assignments. • Exit test is a single boolean statement. If it is false to begin with, the for loop does not run. • Post loop action can be multiple comma-separated statements.