week05

week05 - Week 5 Loops CS 177 1 Repetition Repetition With...

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

View Full Document Right Arrow Icon
CS 177 Week 5: Loops 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
Repetition Repetition is the ability to do something over and over again With repetition in the mix, we can solve practically any problem that can be solved with a computer Repetition leverages the most famous ability of the computer: speed 2
Background image of page 2
Mechanics The main way that repetition works in Java is through loops A loop is a block of code that will be executed repeatedly some number of times (perhaps even zero times!) As the statements are executed, variables may change It isn’t just repeating the same thing over and over 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
while loop The simplest loop in Java is the while loop It looks similar to an if statement The difference is that, when you get to the end of the while loop, it jumps back to the top If the condition in the while loop is still true , it executes the body of the loop again 4
Background image of page 4
Anatomy of a while loop while( condition ) { statement1; statement2; statementn; } A whole bunch of statements 5
Background image of page 5

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

View Full DocumentRight Arrow Icon
A while loop with only one statement A while loop will usually have multiple statements in its body However, it is possible to make a while loop with only a single statement while( condition ) statement; 6
Background image of page 6
Printing numbers Let’s print the numbers from 1 to 100 int i = 1; while ( i <= 100 ) { System.out.println(i); i++; } 7
Background image of page 7

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

View Full DocumentRight Arrow Icon
Rules for while The while loop checks if the condition is true, and if so, executes each statement one by one When execution gets to the bottom, it jumps back up to the top If the condition is still true (i.e., i <= 100 ), it repeats the loop 8
Background image of page 8
Workings of a while loop //line A while ( condition ) { //line B statement1; statement2; //line C statementn; } //line D Line Condition A n B C D Unknown True Unknown False 9
Background image of page 9

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

View Full DocumentRight Arrow Icon
Summing numbers We can also use while loops to help us deal with input What if we wanted to sum all of the
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/23/2012 for the course CS 177 taught by Professor Staff during the Spring '08 term at Purdue.

Page1 / 41

week05 - Week 5 Loops CS 177 1 Repetition Repetition With...

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

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