Download presentation
Presentation is loading. Please wait.
2
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University
3
review What are the three parts of every loop? Where are these three parts in a for loop? Where are these three parts in a while loop? Which two parts of the for loop will always be executed? True or False: You must know at compile time how many iterations you want to execute when using a for loop?
4
Road map break continue Reading: –Liang 6: Chapter 4: 4.9 –Liang 7: Chapter 4: 4.9
5
break and continue
6
break; We have seen the keyword break used in a switch statement: switch (userInput) { case 1: userInput++; break; } You can also use break inside of a for, while or do/while loop to immediately exit from the loop.
7
Example of break use public class Break { public static void main (String [] args) { int x; for ( x = 1 ; x <= 10; x++) { if (x == 5) break; System.out.println("The number is : " + x); } /* end for x = 1... */ }
8
continue Continue is a statement which can be used inside a for, while or do/while loop in order to skip the rest of the remaining code in the loop, and start the next iteration.
9
Example of continue public class Continue { public static void main (String [] args) { int x; for ( x = 1 ; x <= 10; x++) { if (x == 5) continue ; System.out.println("The number is : " + x); } /* end for x = 1... */ }
10
Java Keywords 2003 Prentice Hall, Inc. All rights reserved.
11
Problem Use a while loop with a sentinel to read in letter grades of the form A, B, C, D, F. Convert the grades to numerical equivalents, based on the following table, then print out the average grade in numerical form. 0.01.02.03.04.0Num FDCBALetter
12
Structured Programming: Summary
13
Summary Sequence –Statement follow one another Selection Structures –if –if/else –if/else if/else –switch Repetition Structures –while –do/while –for
14
midterm Monday, October 26 th during class You will have the entire class period to complete the exam 5short answer(total of 15 points) 1find the errors(15 points) 2 - 4medium answer(20 points) 2write a program(total of 50 points) –Remember the difference between write the program and write the main method.
15
Midterm material From the book: –All the sections listed in each of the slide presentations. –Reading the case studies in the chapters is a good idea even though we did not cover all of them in class. That does not mean I will ask you to write one of those programs, but the practice is good –I also suggest you practice problems from the end of each chapter. Anything in the class notes. Next class: On Wednesday, October 21 we will continue today’s review session (practice more exam- like questions.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.