Chapter 10 - Chapter10 Repetition Loops...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 10 Repetition
Background image of page 1

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

View Full DocumentRight Arrow Icon
Loops Use loops to repeat the same code many  times with small modifications to each  iteration.
Background image of page 2
For-Loops Syntax: for (command; boolean_exp; command) { //code to be executed } Example: int x = 1; for (int i=10; i>-1; i--) { x = x * i; }
Background image of page 3

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

View Full DocumentRight Arrow Icon
For-Loops The first command always gets executed  once.  The second command and the code in the  body only get executed while the  expression is true
Background image of page 4
Loops Each pass through the body of the loop is  one  iteration . //This loop iterates 0 times for (int i=0; i<0; i++){ } //This loop iterates 20 times for (int i=0 i<20; i++){ }
Background image of page 5

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

View Full DocumentRight Arrow Icon
Counting Loops The for-loop is known as a  counting-loop   because you (theoretically) know how  many times it will execute just by looking  at the declaration.
Background image of page 6
Except… It is totally possible to break out of a for- loop early: for (int i=0; i<20; i++) { i = 20; } //or for (int i=0; i<20; i++) { break; }
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/28/2011 for the course CS 1131 taught by Professor Staff during the Fall '08 term at Michigan Technological University.

Page1 / 15

Chapter 10 - Chapter10 Repetition Loops...

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

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