Lecture 8 - Object Oriented Programming

Lecture 8 - Object Oriented Programming - o Skips any code...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
The for Loop General form: for ( initialization; expression; increment ) statement; Use braces for more than one statement for ( initialization; expression; increment ) { statements; } The expression must evaluate to true or false o If omitted(left blank), defaults to true o The loop repeats while true Initialization occurs once, before the loop starts (ex: counters) o Can consist of many initializations, separated by commas o Can be omitted Increment occurs at the end of the loop o Multiple increments are possible, separated by commas o Can be omitted Eg: for (i = 1, j = 10; i <= 10, i++, j--) System.out.println(i + “ “ + j); Loops can be nested o Eg: for (i = 10; i <= 100; i = i + 10) { for (j = 0; j < 3; j++) { System.out.println(i + j); } } continue and break Statement continue forces an early iteration of a loop
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: o Skips any code between itself and the conditional expression that controls the loop o Eg: print out even numbers between 0 and 100 for (i = 0, i < 100, i++) { if ((i % 2) == 1) continue; System.out.println(i); } o break causes an immediate exit from a loop Eg: for (i = 0; i < 100, i++) { if (i == 10) break; System.out.println(i); } o If loops are nested, a break in the inner loop causes control to go to the outer loop Both loops are not exited Infinite Loops • Can be formed with: for (;;) { // do something } OR while (true) { // do something } • Usually have a conditional break inside to stop the looping o Eg: for (;;) { ... if (i < 0) break; ... } READING AND EXERCISES Chapter 3...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern