Advanced Loops - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Advanced Loops

Description:

Each time the outer loop is repeated, the inner loop in executed (in it's entirety) ... System.out.println ('Balcony'); ticketPrice = 15.00; break; default: ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 21
Provided by: debrac7
Category:

less

Transcript and Presenter's Notes

Title: Advanced Loops


1
Advanced Loops Switch
2
Nested Loops
  • Outer Loop
  • One or More Inner Loops
  • Each time the outer loop is repeated, the inner
    loop in executed (in its entirety)

3
Example
  • Clock
  • Outer Loop ? Hours (12 hours)
  • Inner Loop ? Minutes (60 minutes/hour)
  • Inner Loop ? Seconds (60 seconds/min)
  • For every hour, the minute loop has to run all 60
    times
  • For every minute, the seconds loop has to run all
    60 times

4
  • public class Clock public static void main
    (String args) // begin outer hours loop
  • for (int hours 1 hours lt 12 hours )
    // begin inner minutes loop
  • for (int minutes 0 minutes lt 59
    minutes) // begin inner seconds loop
  • for (int seconds 0 seconds lt 59
    seconds)
    System.out.println (hours "" minutes ""
    seconds) // end seconds
    // end minutes // end hours // end
    main // end clock
  • Clock code

5
Multiplication Tables
  • Write a program that will print a multiplication
    table (1 9) as shown

6
Analysis
  • Input None
  • Process???
  • Output Multiplication Table

7
Process
  • Display the title Row
  • Print the table body
  • Display the Products

8
Process
  • Display the Column Headings
  • Display the numbers 1 9
  • Display Row number
  • Display Results (Multiplication)
  • Column Heading Row Heading

9
Display Column Headings
10
Produce Table Body
11
Format for Output
12
Multiplication Table Code
  • MultiplicationTable.java

13
Break / Continue
  • Keywords to allow additional control to the loop
  • break
  • Immediately ends the innermost loop
  • Often used with an if statement
  • continue
  • Ends the current iteration of the loop
  • ALWAYS inside a loop
  • Program control goes to the end of the loop body
  • Generally used with an if statement

14
Break
  • Write a program that adds the integers from 1
    20 until the sum is greater than or equal to 100.
  • Input Numbers 1-20
  • Process Add the next number (1-20) to the
    previous sum until the sum is greater than or
    equal to 100
  • Output The sum of the numbers

15
Break
16
Continue
  • Write a program that will add the integers from
    1-20, except the multiples of 5.
  • Input Numbers 1-20
  • Process If number is not a multiple of 5, add
    to sum
  • Output Sum of numbers 1-20 that are not
    multiples of 5

17
Continue
18
Switch Statements
  • Construct to simplify nested decision statements
  • Multiple if statements can make the program
    difficult to read

19
Example Nested If
  • Ticket Prices
  • if (seatingLevel 1)
  • ticketPrice 40.00
  • else if (seatingLevel 2)
  • ticketPrice 30.00
  • else if (seatingLevel 3)
  • ticketPrice 15.00
  • else
  • ticketPrice 10.00

20
Example - Switch
  • switch (seatingLevel)
  • case 1
  • System.out.println (Orchestra)
  • ticketPrice 40.00
  • break
  • case 2
  • System.out.println (Mezzanine)
  • ticketPrice 30.00
  • break
  • case 3
  • System.out.println (Balcony)
  • ticketPrice 15.00
  • break
  • default
  • System.out.printly (Standing)
  • ticketPrice 10.00
  • break
  • // end case
Write a Comment
User Comments (0)
About PowerShow.com