Chapter 5

Ace your homework & exams now with Quizwiz!

What is the output of the following code? for (int i = 1; i < 10; i += 2) { System.out.printf("%1d", i); }

13579

How many times is the following loop executed? for (int count = 1; count < 5; count += 3) { // Execute the body }

2

How many times is the following loop executed? int count = 1; do { count += 3; } while (count < 5);

2

How many times is the following loop executed? int count = 1; while (count < 5) { count += 3; }

2

What is the output of the following code? int x = 85; for (int i = 2; i < x; i++) { if (x % i == 0) { System.out.print(i); break; } }

5

What is the output of the following code? int x = 85; for (int i = 2; i < x; i++) { if (x % i == 0) { System.out.print(i + " "); continue; } }

517

What is count after the loop is finished? int count = 1; do { count += 3; } while (count < 5);

7

infinite loop

A common programming error involves infinite loops (i. e., the loop runs forever). If your program takes an unusually long time to run and does not stop, it may have an infinite loop

do-while loop

A do-while loop is the same as a while loop except that it executes the loop body first and then checks the loop continuation condition. The do - while loop is a variation of the while loop. Its syntax is: do { // Loop body; Statement(s); } while (loop-continuation-condition);

for loop

A for loop has a concise syntax for writing loops. Often you write a loop in the following common form: i = initialValue; // Initialize loop control variable while (i < endValue) // Loop body ... i++; // Adjust loop control variable } A for loop can be used to simplify the preceding loop as: for (i = initialValue; i < endValue; i++) // Loop body ... } In general, the syntax of a for loop is: for loop for (initial-action; loop-continuation-condition; action-after-each-iteration) { // Loop body; Statement(s); }

iteration

A one-time execution of a loop body is referred to as an iteration (or repetition) of the loop.

while loop

A while loop executes statements repeatedly while the condition is true. The syntax for the while loop is: while loop while (loop-continuation-condition) { // Loop body Statement(s); }

sentinel value

Another common technique for controlling a loop is to designate a special value when reading and processing a set of values. This special input value, known as a sentinel value, signifies the end of the input. A loop that uses a sentinel value to control its execution is called a sentinel-controlled loop.

loop

Java provides a powerful construct called a loop that controls how many times an operation or a sequence of operations is performed in succession. Using a loop statement, you simply tell the computer to display a string a hundred times without having to code the print statement a hundred times, as follows: loop int count = 0; while (count < 100) { System.out.println("Welcome to Java!"); count++; }

nested loop

Nested loops consist of an outer loop and one or more inner loops. Each time the outer loop is repeated, the inner loops are reentered, and started anew

off-by-one error

Programmers often make the mistake of executing a loop one more or less time. This is commonly known as the off-by-one error . For example, the following loop displays Welcome to Java 101 times rather than 100 times. The error lies in the condition, which should be count < 100 rather than count <= 100

output redirection

Similarly, there is output redirection , which sends the output to a file rather than displaying it on the console. The command for output redirection is: output redirection java ClassName > output.txt

break statement

The break and continue keywords provide additional controls in a loop. You have used the keyword break in a switch statement. You can also use break in a loop to immediately terminate the loop.

posttest loop

The do-while loop is called a posttest loop because the condition is checked after the loop body is executed.

loop body

The part of the loop that contains the statements to be repeated is called the loop body

pretest loop

The while loop and for loop are called pretest loops because the continuation condition is checked before the loop body is executed.

input redirection

This command is called input redirection . The program takes the input from the file input .txt rather than having the user type the data from the keyboard at runtime. Suppose the contents of the file are

continue statement

You can also use the continue keyword in a loop. When it is encountered, it ends the current iteration and program control goes to the end of the loop body. In other words, continue breaks out of an iteration while the break keyword breaks out of a loop.


Related study sets

final heme review, quiz and exam questions

View Set

Chapter 15. The Autonomic Nervous System and Visceral Reflexes

View Set