Evaluating Expressions that Contain for Loops
Evaluating Expressions
|
Grab a Piece of Paper! |
Evaluate each expression. Verify your answer by running the code in the Java Playground. |
Practice 1: Theater Seats
- Trace the provide code segment and predict the output.
- Verify your prediction is correct by running the code in the Java Playground.
- If the loop concludes, what is the value of
seatwhen the loop is completed? - If the code results in an infinite loop, explain why. How would you modify the code so that it is no longer an infinite loop? Make this change.
- If the code causes the loop to not execute at all, explain why. How would you modify the code so that the loop iterates at least 1 time?
Practice 2: Time Counts Down
- Trace the provide code segment and predict the output.
- Verify your prediction is correct by running the code in the Java Playground.
- If the loop concludes, what is the value of
minuteswhen the loop is completed? - If the code results in an infinite loop, explain why. How would you modify the code so that it is no longer an infinite loop? Make this change.
- If the code causes the loop to not execute at all, explain why. How would you modify the code so that the loop iterates at least 1 time?
Practice 3: Printing Pages
- Trace the provide code segment and predict the output.
- Verify your prediction is correct by running the code in the Java Playground.
- If the loop concludes, what is the value of
pagewhen the loop is completed? - If the code results in an infinite loop, explain why. How would you modify the code so that it is no longer an infinite loop? Make this change.
- If the code causes the loop to not execute at all, explain why. How would you modify the code so that the loop iterates at least 1 time?
Practice 4: Next Customer
- Trace the provide code segment and predict the output.
- Verify your prediction is correct by running the code in the Java Playground.
- If the loop concludes, what is the value of
customerwhen the loop is completed? - If the code results in an infinite loop, explain why. How would you modify the code so that it is no longer an infinite loop? Make this change.
- If the code causes the loop to not execute at all, explain why. How would you modify the code so that the loop iterates at least 1 time?
Practice 5: Hours Left
- Trace the provide code segment and predict the output.
- Verify your prediction is correct by running the code in the Java Playground.
- If the loop concludes, what is the value of
hoursLeftwhen the loop is completed? - If the code results in an infinite loop, explain why. How would you modify the code so that it is no longer an infinite loop? Make this change.
- If the code causes the loop to not execute at all, explain why. How would you modify the code so that the loop iterates at least 1 time?
Practice 6: To Task
- Trace the provide code segment and predict the output.
- Verify your prediction is correct by running the code in the Java Playground.
- If the loop concludes, what is the value of
idwhen the loop is completed? - If the code results in an infinite loop, explain why. How would you modify the code so that it is no longer an infinite loop? Make this change.
- If the code causes the loop to not execute at all, explain why. How would you modify the code so that the loop iterates at least 1 time?