Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.

Similar presentations


Presentation on theme: "Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University."— Presentation transcript:

1

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 Nested for loops Reading: –Liang 5: Chapter 3: 3.3.4; 3.5; 3.6

5 Nested For Loops It is also possible to place a for loop inside another for loop. int rows, columns; for (rows = 1; rows <= 5; rows++) { for (columns=1; columns<=10; columns++) System.out.print ("*"); System.out.println (); } Outer Loop Inner Loop Output:**************************************************

6 Nested For Loops, Example #2 int rows, columns; for (rows=1; rows<=5; rows++) { for (columns=1; columns<=rows; columns++) System.out.print ("*"); System.out.println (); } Output:*************** Outer Loop Inner Loop

7 break and continue

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

9 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... */ }

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

11 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... */ }

12 Java Keywords  2003 Prentice Hall, Inc. All rights reserved.

13 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

14 Structured Programming: Summary

15 Summary Sequence –Statement follow one another Selection Structures –if –if/else –if/else if/else –switch Repetition Structures –while –do/while –for

16 midterm Thursday March 2 nd during class You will have the entire class period to complete the exam 5short answer (total of 15 points) 1find the errors(15 points) 1what does this do(20 points) 2write a program(total of 50 points)

17 Midterm material From the book: –chapters 1 - 3 Liang 4: Except 2.12 and 3.5.1 Liang 5: Except 1.5, 1.12, 2.13, 2.16, 2.17 –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. The practice is good. (Do not bother with example 2.3 as we have not used the Math class yet) –I also suggest you practice problems from the end of each chapter. Anything in the class notes

18 Upcoming schedule Thursday, February 23: review Tuesday, February 28: review Thursday, March 2: midterm


Download ppt "Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University."

Similar presentations


Ads by Google