Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Introduction to Computer Science I - Repetition - Slides | CS 110, Study notes of Computer Science

Material Type: Notes; Class: Intro to Computer Science I; Subject: Computer Science; University: University of San Francisco (CA); Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-bzy
koofers-user-bzy 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Repetition
Examples
When is repetition necessary/useful?
Types of Loops
Coun ting loop
Know how many time s to loop
Sent inel-controlled loop
Expect specific input value to end loop
Endf ile-controlled loop
End of data file is end of loop
Input validation loop
Valid input ends loop
Gene ral conditional loop
Repeat until condition is met
while
while condition:
statements
x=1
while x < 10:
print x
x = x + 1
while
x=1 #initialization of control variable
while x < 10: #condition
print x #task to be repeated
x = x + 1 #update - VERY VERY IMPORTANT
Sentinel-controlled
num = input("Enter number - 0 to quit: ")
while num != 0:
print “You entered “, num
num = input("Enter number - 0 to quit: ")
Which is the control varia ble?
pf3
pf4

Partial preview of the text

Download Introduction to Computer Science I - Repetition - Slides | CS 110 and more Study notes Computer Science in PDF only on Docsity!

Repetition

Examples

• When is repetition necessary/useful?

Types of Loops

  • Counting loop
    • Know how many times to loop
  • Sentinel-controlled loop
    • Expect specific input value to end loop
  • Endfile-controlled loop
    • End of data file is end of loop
  • Input validation loop
    • Valid input ends loop
  • General conditional loop
    • Repeat until condition is met

while

while condition: statements x= while x < 10: print x x = x + 1

while

x=1 #initialization of control variable while x < 10: #condition print x #task to be repeated x = x + 1 #update - VERY VERY IMPORTANT

Sentinel-controlled

num = input("Enter number - 0 to quit: ") while num != 0: print “You entered “, num num = input("Enter number - 0 to quit: ")

• Which is the control variable?

Input Validation

num = input("Enter number between 0 and 100: ") while num < 0 or num > 100: #a more complex condition print "Invalid input" num = input("Enter number between 0 and 100: ")

for

for x in range(10): print x mystring = "CS is cool!" for c in mystring: print c

• Loop iterates over a list

• Initialization and update happen automatically

Infinite Loops

• If your program “hangs” – you probably forgot to

update your control variable

x= while x==1: print “x is 1”

• Why is this bad?

x= end_value= while x != end_value: #do something

Infinite Loops

  • Why is this bad? x= end_value= while x != end_value: #do something x *= 2 x= end_value= while x < end_value: #better #do something

Alternative

while 1: num = input(“Enter a number - 0 to quit: “) if num == 0: break #combines intialization and update

Exercises

1. Write a while loop that prints all of the even

numbers between 1 and 100.

  • Create two versions of this loop, one that uses an if

statement and one that does not

2. Write a program that uses the module random

to select a random number between 1 and 10

(example below) and asks the user to

repeatedly enter a number until he/she has

guessed the random number.

#import the module random import random #call the randint function passing in the range num = random.randint(1, 10)

Exercise

2. Design a program which prompts the

user for a number of rows between 0

and 10 and prints the following pattern: