Summary Section 5.2 Essentials of Counter-Controlled Repetition • Counter-controlled repetition requires a control variable, the initial value of the control variable, the increment by which the control variable is modified each time through the loop (also known as each iteration of the loop) and the loop-continuation condition that determines whether looping should continue. • You can declare a variable and initialize it in the same statement. Section 5.3 for Repetition Statement • The while statement can be used to implement any counter-controlled loop. • The for statement specifies all the details of counter-controlled repetition in its header • When the for statement begins executing, its control variable is declared and initialized. If the loop-continuation condition is initially true, the body executes. After executing the loop’s body, the increment expression executes. Then the loop-continuation test is performed again to determine whether the program should continue with the next iteration of the loop. • The general format of the for statement is for (initialization; loopContinuationCondition; increment) statement where the initialization expression names the loop’s control variable and provides its initial value, loopContinuationCondition determines whether the loop should continue executing and increment modifies the control variable’s value, so that the loop-continuation condition eventually becomes false. The two semicolons in the for header are required. • Most for statements can be represented with equivalent while statements as follows: initialization; while (loopContinuationCondition) { statement increment; } • Typically, for statements are used for counter-controlled repetition and while statements for sentinel-controlled repetition. • If the initialization expression in the for header declares the control variable, the control variable can be used only in that for statement—it will not exist outside the for statement. • The expressions in a for header are optional. If the loopContinuationCondition is omitted, Java assumes that it’s always true, thus creating an infinite loop. You might omit the initialization expression if the control variable is initialized before the loop. You might omit the increment expression if the increment is calculated with statements in the loop’s body or if no increment is needed. • The increment expression in a for acts as if it’s a standalone statement at the end of the for’s body. • A for statement can count downward by using a negative increment—i.e., a decrement. • If the loop-continuation condition is initially false, the for statement’s body does not execute. Section 5.4 Examples Using the for Statement • Java treats floating-point constants like 1000.0 and 0.05 as type double. Similarly, Java treats whole-number constants like 7 and -22 as type int. • The format specifier %4s outputs a String in a field width of 4—that is, printf displays the value with at least 4 character positions. If the value to be output is less than 4 character positions wide, the value is right justified in the field by default. If the value is greater than 4 character positions wide, the field width expands to accommodate the appropriate number of characters. To left justify the value, use a negative integer to specify the field width. • Math.pow(x, y) calculates the value of x raised to the yth power. The method receives two double arguments and returns a double value. • The comma (,) formatting flag in a format specifier indicates that a floating-point value should be output with a grouping separator. The actual separator used is specific to the user’s locale (i.e., country). In the United States, the number will have commas separating every three digits and a decimal point separating the fractional part of the number, as in 1,234.45. • The . in a format specifier indicates that the integer to its right is the number’s precision. Section 5.5 do…while Repetition Statement • The do…while statement is similar to the while statement. In the while, the program tests the loop-continuation condition at the beginning of the loop, before executing its body; if the condition is false, the body never executes. The do…while statement tests the loop-continuation condition after executing the loop’s body; therefore, the body always executes at least once. Section 5.6 switch Multiple-Selection Statement • The switch statement performs different actions based on the possible values of a constant integral expression (a constant value of type byte, short, int or char, but not long), or a String. • The end-of-file indicator is a system-dependent keystroke combination that terminates user input. On UNIX/Linux/Mac OS X systems, end-of-file is entered by typing the sequence <Ctrl> d on a line by itself. This notation means to simultaneously press both the Ctrl key and the d key. On Windows systems, enter end-of-file by typing <Ctrl> z. • Scanner method hasNext determines whether there’s more data to input. This method returns the boolean value true if there’s more data; otherwise, it returns false. As long as the end-of-file indicator has not been typed, method hasNext will return true. • The switch statement consists of a block that contains a sequence of case labels and an optional default case . • In a switch, the program evaluates the controlling expression and compares its value with each case label. If a match occurs, the program executes the statements for that case. • Listing cases consecutively with no statements between them enables the cases to perform the same set of statements. • Every value you wish to test in a switch must be listed in a separate case label. • Each case can have multiple statements, and these need not be placed in braces. • A case’s statements typically end with a break statement that terminates the switch’s execution. • Without break statements, each time a match occurs in the switch, the statements for that case and subsequent cases execute until a break statement or the end of the switch is encountered. • If no match occurs between the controlling expression’s value and a case label, the optional default case executes. If no match occurs and the switch does not contain a default case, program control simply continues with the first statement after the switch. Section 5.7 Class AutoPolicy Case Study: Strings in switch Statements • Strings can be used in a switch statement’s controlling expression and case labels. Section 5.8 break and continue Statements • The break statement, when executed in a while, for, do…while or switch, causes immediate exit from that statement. • The continue statement, when executed in a while, for or do…while, skips the loop’s remaining body statements and proceeds with its next iteration. In while and do…while statements, the program evaluates the loop-continuation test immediately. In a for statement, the increment Expression executes, then the program evaluates the loop-continuation test. Section 5.9 Logical Operators • Simple conditions are expressed in terms of the relational operators >, <, >= and <= and the equality operators == and !=, and each expression tests only one condition. • Logical operators enable you to form more complex conditions by combining simple conditions. The logical operators are && (conditional AND), || (conditional OR), & (boolean logical AND), | (boolean logical inclusive OR), ^ (boolean logical exclusive OR) and ! (logical NOT). • To ensure that two conditions are both true, use the && (conditional AND) operator. If either or both of the simple conditions are false, the entire expression is false. • To ensure that either or both of two conditions are true, use the || (conditional OR) operator, which evaluates to true if either or both of its simple conditions are true. • A condition using && or || operators uses short-circuit evaluation —they’re evaluated only until it’s known whether the condition is true or false. • The & and | operators work identically to the && and || operators but always evaluate both operands. • A simple condition containing the boolean logical exclusive OR (^) operator is true if and only if one of its operands is true and the other is false. If both operands are true or both are false, the entire condition is false. This operator is also guaranteed to evaluate both of its operands. • The unary ! (logical NOT) operator “reverses” the value of a condition. |