Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 4 Loops While loop The for loop do… while break and continue"— Presentation transcript:

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

2 Repetition The while loop
lets say we wish to display the numbers from 1 to 10 print “1\n2\n3\n4\n5\n6\n7\n8\n9\n10”; now 1 to x=1; while (x<=100000) { print x; x=x+1; }

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

4 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.

5 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? user enters number of grades before entering grades a sentinel or flag value

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

7 Top down design and coding
This method helps to structure the resulting code. It uses the divide and conquer approach. 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 each smaller piece can be designed, coded and tested separately.

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

9 Nested control structures
commonly if statements are used inside of other if statements or inside of loops. while ( count<=10 ) { input = JOptionPane.showInputDialog(“Enter grade”); grade = Integer.parseInt(input); if (grade < 60 ) failed = failed +1 else passed =passed +1; count++; }

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

11 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);

12 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; }

13 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; }

14 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”

15 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 ..X.. .XXX. XXXXX


Download ppt "Chapter 4 Loops While loop The for loop do… while break and continue"

Similar presentations


Ads by Google