Programming and Problem Solving with Java
(^220) | File Objects and Looping Statements System.out.println(lineThree); // Write three lines to outFile in reverse order out ...
5.2 Looping | 221 in which the statements appear in the code; the logical orderis the order in which we want the statements to b ...
(^222) | File Objects and Looping Statements The body of a loop can consist of a block of statements, which allows us to execute ...
5.2 Looping | 223 Phases of Loop Execution The body of a loop is executed in several phases: The moment that the flow of contr ...
(^224) | File Objects and Looping Statements loopCount = 1; // Initialization while(loopCount <= 10) // Test { . . // Repeate ...
5.2 Looping | 225 A loop that never exits is called an infinite loopbecause, in theory, the loop executes for- ever. In the prec ...
(^226) | File Objects and Looping Statements We can solve the problem by reading the first data value beforeentering the loop. T ...
5.2 Looping | 227 data = dataFile.readLine(); // Get first data line sentinel = data.substring(0, 1); // Extract sentinel charac ...
(^228) | File Objects and Looping Statements The test in this whilestatement uses the relational operator !=instead of the equal ...
5.2 Looping | 229 takes place and then the value returned by the expression is discarded. For example, in the comparison (x = 2 ...
(^230) | File Objects and Looping Statements until it comes to a zero. (Both numberand countare of type int.) The loop in the fo ...
5.2 Looping | 231 if (inLine == null) // If EOF number = 0; // Set number to 0 else number = Integer.parseInt(inFile.readLine()) ...
(^232) | File Objects and Looping Statements adds the current value of sum(0) to numberto form the new value of sum. After the e ...
5.2 Looping | 233 odd number is read; it is an event counter. We initialize an event counter to 0 and increment it only when a c ...
(^234) | File Objects and Looping Statements Now we need statements that make sure the loop starts correctly and statements that ...
5.2 Looping | 235 Initialize the flag variable to trueor false, as appropriate. Update the flag variable as soon as the cond ...
(^236) | File Objects and Looping Statements The Loop Exit When the termination condition occurs and the flow of control passes ...
5.2 Looping | 237 commaCount = 0; // Initialize event counter index = 0; // Initialize loop control count while(index < inLin ...
(^238) | File Objects and Looping Statements Let’s look at another example. For nested count-controlled loops, the pattern looks ...
5.2 Looping | 239 We’ll keep track of the variables line,starCount, and loopCount, as well as the logical expres- sions. To do s ...
«
8
9
10
11
12
13
14
15
16
17
»
Free download pdf