Chapter 5 Loops Flashcards

1
Q

The do while loop definition

A

A do-while loop is the same as a while loop except that it executes the loop body first then checks the loop continuation condition.

This means that a do-while loop will always be executed at least once.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

The do while loop syntax

A

do {
<statement(s)>
<update>
} while (<condition>);</condition></update>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Taking user input for do while loops

A

Scannerinput=newScanner(System.in);
intdata;
intsum=0;

do{
System.out.println(“Enteraninteger(enter0tostop)”);
data=input.nextInt();
sum+=data;
}while(data!=0);
System.out.println(“Thesumis”+sum);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you end do-while loop

A

The following while loop is wrong:
int i=0;
while (i < 10);
{
System.out.println(“i is “ + i);
i++;
}

In the case of the do-while loop, the following semicolon is needed to end the loop.
int i=0;
do {
System.out.println(“i is “ + i);
i++;
} while (i<10);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does a for loop consist of

A

1) initialization section - is executed once at the start of the loop
2) continuation section - is evaluated before every loop iteration to check for loop termination
3) next iteration section - is evaluated after every loop iteration to update the loop counter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

for loop syntax

A

for (<initialization>; <condition>; <update>) { <statement(s)>
}</update></condition></initialization>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Write a for loop that prints “Welcome to Java” 100 times

A

int i;
for (i = 0; i < 100; i++) {
System.out.println(“Welcome to Java!”);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

An infinite loop is caused when?

A

loops conditions are incorrect.

for (int i = 0; i < 10; i–){ // Should have been i++
System.out.print(i + “, ”); // Infinite loop: 0, -1, -2, ..
}

int j = 0;
while (j < 10){
System.out.print(j + “, ”); // Infinite loop: 0,0,0,..
} // Forgot to change j in loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you purposely create an infinite loop

A

To purposely create an infinite while loop, use the Boolean literal true as the condition.
If the condition in a for loop is omitted, it is implicitly true and will cause an infinite loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Nested loops definition

A

A loop can be nested inside another 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Note: 1 microsecond is ____ of a second

A

one-millionth (10^(−6))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When do you use a break statement

A

You have used the keyword break in a switch statement. You can also use break in a loop to immediately terminate the loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

When do you use a continue statement

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly