Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lec 5 Nested Control Structures

Similar presentations


Presentation on theme: "Lec 5 Nested Control Structures"— Presentation transcript:

1 Lec 5 Nested Control Structures

2 Calculating the sum or average
num k k < 10 Console 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);

3 Nesting The idea of nesting is to put one thing inside of another:
nested: ( [ { } { } ] { } ) not nested: ( ) [ ] { } [ ] NOT nested: ( [ { ) ] }

4 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

5 While loop with if inside
int i = 0; while ( i < 3) { if ( i == 0) { ...println("zero"); } else { ...println( i ); i = i + 1; i i<3 i==0 Console

6 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); big num i i<5 Console

7 Work on Loop Drills in class
Due next Tuesday you may finish today these are exam style questions so make sure you master them

8 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


Download ppt "Lec 5 Nested Control Structures"

Similar presentations


Ads by Google