mirror of
https://github.com/Brandon-Rozek/website.git
synced 2024-12-23 09:53:46 +00:00
1.3 KiB
1.3 KiB
Lecture for February 13
Loops
###Why Loops?
While some check is true, repeat the work.
While the cookies aren't baked, keep baking
Loop Building Process
- Identify one test that must be true when the loop is finished
- Use the opposite form of the test
- Within loop, make progress towards completion of the goal.
While syntax
while (expression) {
// Loop body executes if expression is true
}
// Statements execute after expression is false
Getting Input (Songs in a Playlist Psuedocode)
// Ask user about first song
while (user says play next song) {
// play next song
// ask user about next song
}
Nested Loops
You can have loops inside loops
int outer = 1;
while (outer < 4) {
int inner = 1;
while (inner < 4) {
System.out.println(outer + ":" + inner);
inner++;
}
outer++;
}
This code does the following
1:1
1:2
1:3
2:1
2:2
2:3
3:1
3:2
3:3
Break Down the Problem
Never write the entire program at once! This makes it incredibly hard to debug. Instead break it into small parts.
Write one part -> debug until it works
Write second part -> debug until it works
This way you know which part of your code failed, instead of having everything fail.