1 / 17

Java Programming, Second Edition

Java Programming, Second Edition. Chapter Six Looping. In this chapter, you will:. Learn about the loop structure Use a while loop Use shortcut arithmetic operators Use a for loop Learn how and when to use a do…while loop Learn about nested loops. Learning about the Loop Structure.

sauda
Download Presentation

Java Programming, Second Edition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Programming, Second Edition Chapter Six Looping

  2. In this chapter, you will: Learn about the loop structure Use a while loop Use shortcut arithmetic operators Use a for loop Learn how and when to use a do…while loop Learn about nested loops

  3. Learning about the Loop Structure • Loop- A structure that allows repeated execution of a block of statements • Loop body- A block of statements; as long as the expression is true, the loop body executes • Iteration- One execution of any loop

  4. Using the while Loop • while Loop- To execute a body of statements continually as long as the Boolean expression continues to be true • Consists of the keyword while followed by a Boolean expression within parentheses followed by the body of the loop • Use when you need to perform a task a predetermined number of times

  5. Infinite Loop • Infinite loop- A loop that never ends

  6. Using a while Loop • Incrementing – Altering a loop by adding one to loop control variable • Decrementing – Altering a loop by subtracting one from a loop control variable

  7. Loops • You are not required to alter the loop variable by adding to it or subtracting from it • When you write a loop that is controlled by an arithmetic result, you need to know how many times you want the loop to execute • Often the loop control variable is altered by user input

  8. Loops • Sentinel value- A value that a user must supply to stop a loop • Accumulating- Adding a value to a variable to get a new value in the same variable

  9. Using Shortcut Arithmetic Operators To increase a variable’s value by exactly one: • prefix ++ • Used before the variable name • ++someValue; • postfix ++ • Used after the variable name • anotherValue++;

  10. Using a for Loop • for loop- A special loop that is used when a definite number of loop iterations is required • Keyword for • Set of parentheses • Three sections within parentheses • Initializing the loop control variable • Testing the loop control variable • Updating the loop control variable

  11. Learning How and When to Use a do…while loop • Checks at the bottom of the loop after one repetition has occurred • Loop body executes at least one time • The loop starts with the keyword do • The body of the loop is contained within curly braces

  12. Nested Loops • Loops can be nested much like if statements • You can place a while loop within a while loop, a for loop within a for loop, a do…while loop within a do…while loop, or use any combination of these loops

More Related