05-loops - Iteration Statements (Loops) CSE 114: Computer...

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

View Full Document Right Arrow Icon
Iteration Statements (Loops) CSE 114: Computer Science I 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
What We’ve Covered Simple programs Variables and data types Expressions Conditional (selection) statements 2
Background image of page 2
Iterative Programming Many programs perform the same task many times Operations are repeated on different data Ex. Adding a list of numbers Ex. Displaying frames of a movie Fle Repetitive tasks are speciFed using loops 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
Loop Elements All loop constructs share four basic elements: 1. Initialization 2. Testing the loop condition 3. Loop body (the task to be repeated) 4. Loop update The order of these elements may vary 4
Background image of page 4
Initialization This section of code is used to set starting values For example, setting a total to 0 initially This can be done as part of the loop, or separately before the loop code begins 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
Loop Tests Test expressions are used to determine whether the loop should execute (again) Tests compare one value/variable with another If the test evaluates to TRUE, then the loop will execute another time 6
Background image of page 6
Test Operators > Greater than >= Greater than or equal to < Less than <= Less than or equal to == Is equal to != Is not equal to 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
Loop Update This step changes the value(s) of the loop variable(s) before the loop repeats Ex. moving to the next item to process This can be done explicitly as part of the loop, or it can be done inside the loop body 8
Background image of page 8
while Loops while loops execute any number of times Order of execution: 1. Initialization 2. Loop condition test 3. Loop body 4. Loop update 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
General Form initialization while ( loop condition test ) { loop body loop update } 10
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.

Page1 / 40

05-loops - Iteration Statements (Loops) CSE 114: Computer...

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