Summary
Overview: 4.6.0
- In chapter 4 we learned about
loops
loops
are used to repeat a statement/block
Concept Summary: 4.6.1
- Body of a Loop
- The statement or block which follows a loop and is to be repeated. If the body contains multiple statements, it must be enclosed by curly brackets
- For Loop
- A loop that has a header with 3 optional parts: initialization, condition, and change.
- Initialization happens once before the loop ececutes
- Checks the condition before every iteration
- Executes the change at the end of every loop
- Infinite Loop
- A loop that never ends.
- Loop
- A way to repeat one or more statements in a program.
- Nested Loop
- One loop inside of another.
- Out of Bounds error
- A run-time error that occurs when you try to access past the end of a string or list.
- Trace Code
- Writing down the values of the variables and how they change each time the body of the loop executes.
- While Loop
- A loop that repeats while a Boolean expression is true.
Keyword Summary: 4.6.2
while
- Begins a while loop
for
- starts a for/for each loop
System.out.println(variable)
- Prints the value of a variable
- Useful for tracing code execution and debugging
Common Mistakes: 4.6.4
- Forgetting to change the thing you are testing in a
while
loop- Infinite loop!
- Getting the start and end conditions wrong for a
for
loop- Results in an
out of bounds error
- Results in an
- Jumping out of a loop too early using a
return
statement
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
This was adapted from the CS Awesome curriculum, which was created by
Barbara Ericson, Beryl Hoffman, and many other CS Awesome contributors. All rights reserved.
CS Awesome is licensed under CC BY-NC-SA 4.0.