7 Looping

Oracle Academy Section

Section 6: Loops

  • Lesson 1: for Loops

  • Lesson 2: while and do-while Loops

  • Lesson 3: Using break and continue Statements

Section 6 Quiz

Java Foundations Certification Exam Topics

Using Looping Statements

  • Describe looping statements

  • Use a for loop including an enhanced for loop

  • Use a while loop

  • Use a do- while loop

  • Compare and contrast the for, while, and do-while loops

  • Develop code that uses break and continue statements

Most Important Concepts / Code

while loops are condition controlled.

for loops are count controlled.

Use a while loop when you don't know how many times it will have to repeat, like a user entering bad input.

There must always be something inside the loop that changes the loop condition. If not, you get an infinite loop.

The name for a variable that keeps a running total is an accumulator, i.e. runningTotal += anotherNumber

The name for one loop execution is an iteration.

Sentinel - special value to stop loop

for (initialization; termination; increment) {

// ...

}


int continueProgram = 1;

while (continueProgram == 1) {

// your program

System.out.println("Press 1 to continue or 2 to quit.");

continueProgram = keyboard.nextInt();

}

Hour 1

  • Quiz and Concepts Review

  • Create an algorithm before coding!

  • General loop advice: get it working without a loop first, then try to make all of the similar code the same, delete what is repeated, and put what is left in a loop.

  • while loops

    • How to type

    • while loop example program

    • sentinel

  • for loops

    • How to type

    • for loop sequence

    • while to for

  • Importance of curly braces

  • Eclipse tip

    • Use the red square to stop a program

  • Make a class

Hour 2

  • Motivational speech

  • Programming Jokes of the week

    • Why was the programmer stuck in the shower?

    • A wife calls her programmer husband and tells him "while you're out, buy some milk". He never returns home.