lec 5 nested control structures
Download
Skip this Video
Download Presentation
Lec 5 Nested Control Structures

Loading in 2 Seconds...

play fullscreen
1 / 8

Lec 5 Nested Control Structures - PowerPoint PPT Presentation


  • 98 Views
  • Uploaded on

Lec 5 Nested Control Structures. Calculating the sum or average. int sum = 0; int k = 0, num; while ( k < 5) { num = scan.nextInt(); sum = sum + num; k = k + 1; } ...println("sum is " + sum); ... println ("avg is " + sum/5.0);. Nesting.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Lec 5 Nested Control Structures' - naava


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
calculating the sum or average
Calculating the sum or average

int sum = 0;

int k = 0, num;

while ( k < 5) {

num = scan.nextInt();

sum = sum + num;

k = k + 1;

}

...println("sum is " + sum);

...println("avg is " + sum/5.0);

nesting
Nesting
  • The idea of nesting is to put one thing inside of another:
    • nested: ( [ { } { } ] { } )
    • not nested: ( ) [ ] { } [ ]
    • NOT nested: ( [ { ) ] }
nesting control structures
Nesting control structures
  • We have seen several control structures
    • do loops
    • while loops
    • if statements (including compound if statements with else if and else )
  • We can nest any of these structures inside another to get different behaviors
while loop with if inside
While loop with if inside

int i = 0;

while ( i < 3) {

if ( i == 0) {

...println("zero");

}

else {

...println( i );

}

i = i + 1;

}

while loop with if inside1
While loop with if inside

int i = 0, big=0, num;

while ( i < 5) {

num = scan.nextInt();

if ( num > 50) {

big = big+1;

}

i = i + 1;

}

...println("number of big: " + big);

work on loop drills in class
Work on Loop Drills in class
  • Due next Tuesday
  • you may finish today
  • these are exam style questions so make sure you master them
lab 5
Lab 5
  • Guessing Game
  • pick a random number and store it
  • Do Loop:
    • get user guess
    • if guess is high or low, say it
    • when guess is correct leave loop
ad