Chapter 4 Loops While loop The for loop

  • Slides: 15
Download presentation
Chapter 4 Loops While loop The for loop do… while break and continue labeled

Chapter 4 Loops While loop The for loop do… while break and continue labeled break

Repetition The while loop lets say we wish to display the numbers from 1

Repetition The while loop lets say we wish to display the numbers from 1 to 10 print “ 1n 2n 3n 4n 5n 6n 7n 8n 9n 10”; now 1 to 100000 x=1; while (x<=100000) { print x; x=x+1; }

The while loop parts 3 key parts x=1; while (x<=100000) { print x; x=x+1;

The while loop parts 3 key parts x=1; while (x<=100000) { print x; x=x+1; } initialize counter variable aka control variable the exit condition the change value

Loop issues initializing the counter infinite loops for clarity indent the loop body memorize

Loop issues initializing the counter infinite loops for clarity indent the loop body memorize the basic while loop be able to adjust it to perform other tasks.

Lets talk through some examples a counter controlled loop to total 10 grades and

Lets talk through some examples a counter controlled loop to total 10 grades and print an average and total. lets say we want the user to determine the number of grades to process? describe 2 ways? n n user enters number of grades before entering grades a sentinel or flag value

Top-down, step-wise refinement what is that? first write spuedocode to describe the problem at

Top-down, step-wise refinement what is that? first write spuedocode to describe the problem at a high level n determine class average for the quiz; this is the top then break this into smaller pieces n n n initialize variables input, total and count grades calculate a print result Then further break each of these down into smaller pieces as needed.

Top down design and coding This method helps to structure the resulting code. It

Top down design and coding This method helps to structure the resulting code. It uses the divide and conquer approach. n Take a large formidable problem and break it into many smaller problems that can be solved more easily Allow problems to be solved by groups of people n each smaller piece can be designed, coded and tested separately.

Top down design Not always used by experienced programmers developing familiar solutions.

Top down design Not always used by experienced programmers developing familiar solutions.

Nested control structures commonly if statements are used inside of other if statements or

Nested control structures commonly if statements are used inside of other if statements or inside of loops. while ( count<=10 ) { input = JOption. Pane. show. Input. Dialog(“Enter grade”); grade = Integer. parse. Int(input); if (grade < 60 ) failed = failed +1 else passed =passed +1; count++; }

The for loop for (int x = 1; x<100; x++) { print x; }

The for loop for (int x = 1; x<100; x++) { print x; } // x no longer exists This declares and initializes the control variable int x; for (x = 1; x<100; x++) { print x; } // x still exists

The do…. while Condition evaluated after at least one pass through the loop body.

The do…. while Condition evaluated after at least one pass through the loop body. The while and for loops may never enter the loop body x=0; do { print x; x++; }while (x<100);

The break statement used to exit a loop consider this example. what will be

The break statement used to exit a loop consider this example. what will be displayed? for (c=1; c<10; c++) { if (c==5) break; print c; }

The continue statement used to continue the next iteration in a loop consider this

The continue statement used to continue the next iteration in a loop consider this example. what will be displayed? for (c=1; c<10; c++) { if (c==5) continue; print c; }

Labeled code blocks and break and continue What will this display mycode: { print

Labeled code blocks and break and continue What will this display mycode: { print “in my code”; for (int x=1; x<10; x++) { if (x==5) break mycode; print x; } print “done with loop” }

Homework Page 214 -215 Do number 1, 7 Due in Monday Oct 2 Also

Homework Page 214 -215 Do number 1, 7 Due in Monday Oct 2 Also write a program to draw a Diamond with a odd input size for example 5. . XXXXX. .