CS 303E Class 13 do Loops and Nested Loops PowerPoint PPT Presentation

presentation player overlay
1 / 6
About This Presentation
Transcript and Presenter's Notes

Title: CS 303E Class 13 do Loops and Nested Loops


1
CS 303E Class 13do Loops and Nested Loops
Let's play two! - Ernie Banks When you open your
mind to unlimited potential, you can accomplish
anything. -Ernie Banks
2
do-while Loop
  • Syntax
  • do
  • //body of loop
  • First_Statement
  • ...
  • Last_Statement
  • while(Boolean_Expression)
  • Initialization code may precede loop body

3
do-while Loop
  • Loop test is after loop body so the body must
    execute at least once (minimum of at least one
    iteration)
  • May be either count controlled or event
    controlled loop
  • Good choice for event controlled loop
  • Something in body of loop should eventually cause
    Boolean_Expression to be false
  • Another tool, but stick to while and for loops

4
Nested Loops
  • Any kind of code may be contained in a loop
  • including another loop
  • Tracing of code and variables is very important
    in understanding the behavior of nested loops
  • What is the output of the following code?
  • for(int limit 1 limit lt 10 limit limit
    1)
  • for(int num 0 num lt limit num num 1)
  • System.out.print( (num 2 1) " ")
  • System.out.println()

5
Using Nested Loops
  • Write a program that find all the prime numbers
    between 2 and N.

6
More on Nested Loops
  • Write a program to create a chessboard pattern on
    an applet.
  • getWidth and getHeight methods from Applet class
Write a Comment
User Comments (0)
About PowerShow.com