# powerpoint3.txt

Home > Preview

The flashcards below were created by user dimeng on FreezingBlue Flashcards.

1. What does a while loop do?
A while loop tests for a condition (boolean value or logical expression), then executes the loop body (one or many statements) while the condition is true.
2. What does a do-while loop do?
A do-while loop executes the loop body one time, then tests for a condition (boolean or logical expression) and executes the body while the condition is true.
3. Draw a flowchart for a while loop.
4. Draw a flowchart for a do-while loop.
5. Which loop is top-tested, a while loop or a do-while loop?
A while loop.
6. Which loop is bottom-tested, a while loop or a do-while loop?
a do-while loop.
7. How many lines of code in the loop body will execute if you don't include curly braces?
Only one. Watch for this on tests!!!
8. What will happen if you do this (if x starts at 0)? while (x < 3); { sout (x); x++;}
Infinite loop - there is a semicolon after the loop condition. If the condition were false it would not be an infinite loop; the loop body would never execute.
9. What will happen if you do this (if x starts at 0)? while (x > 3); x++; sout (x);
• A 0 will be output to the screen. Since 0 is not greater than three the loop body will not execute.
• Since there are no curly braces, x++; is the only statement in the loop body. So the sout is the only statement executed.
10. What are two common errors in a loop that will create an infinite loop?
• 1) no incrementation
• 2) semicolon after the loop condition
11. Which loop executes one or more times: while or do-while?
do-while
12. Which loop may never execute?
while
13. Will a for loop execute at least once?
No.
14. Draw a flowchart for a for loop.
15. Write a while loop that performs the same actions as this for loop:
int max = 6;
int i;

for (i=0; i sout(i);
}
• int max = 6;
• int i = 0;

• while (i < max) {
• sout(i);
• i++;
• }
16. How can you fix the following code to work as intended? What is the problem?
int max = 6;

for (int i=0; i sout(i);
}
sout("last i "+ i);
The sout that is not in the loop will fail (and probably wouldn't compile) because i is declared inside the loop. This code will fix that problem.

• int max = 6;
• int i;

• for (i=0; i sout(i);
• }
• sout("last i "+ i);

### Card Set Information

 Author: dimeng ID: 296409 Filename: powerpoint3.txt Updated: 2015-02-21 14:42:47 Tags: diane cs140 Folders: diane,cs140 Description: diane cs140 powerpoint3 Show Answers:

What would you like to do?

Home > Flashcards > Print Preview