CS161_Topic#7

CS161_Topic#7 - 1 CS161 CS161 Introduction to Introduction...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: 1 CS161 CS161 Introduction to Introduction to Computer Science Computer Science Topic #7 CS161 Topic #7 2 Today in CS161 Repetition in Programs while, do while, for loops questions with program #3 CS161 Topic #7 3 Repetition in Programs What if we wanted to give the user another chance to enter their input... This would be impossible without loops Algorithms that require loops look something like: Step 1: Receive Input Step 2: Echo the Input Step 3: Ask the user if this is correct Step 4: If not, repeat beginning at step #1 CS161 Topic #7 4 Three types of Loops There are three ways to repeat a set of code using loops: while loop do while loop for loop Each of these can perform the same operations... it is all in how you think about it! ....lets see.... CS161 Topic #7 5 Using a While Loop Lets give the user a 2nd (and 3rd, 4th, 5th...) chance to enter their data using a while loop. While loops have the form: (notice semicolons!) while (logical expression) single statement; while (logical expression) { many statements; } CS161 Topic #7 6 Using a While Loop The while statement means that while an expression is true, the body of the while loop will be executed. Once it is no longer true, the body will be bypassed. The first thing that happens is that the expression is checked, before the while loop is executed. THIS ORDER IS IMPORTANT TO REMEMBER! CS161 Topic #7 7 Using a While Loop The Syntax of the While Loop is: while (loop repetition condition) <body> Where, the <body> is either one statement followed by a semicolon or a compound statement...
View Full Document

This note was uploaded on 03/17/2010 for the course CS 161 taught by Professor Fant during the Winter '08 term at Portland State.

Page1 / 24

CS161_Topic#7 - 1 CS161 CS161 Introduction to Introduction...

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