Lecture 8 - Object Oriented Programming

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

Info iconThis 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
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
Background 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

This note was uploaded on 11/04/2010 for the course CPSC 233 taught by Professor Li during the Fall '09 term at University of Calgary.

Page1 / 3

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

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online