Docsity

Lecture Notes for CPSC 121 Fall 2012: While Loop Exercises and Function Computation, Study notes of Computer Science

Partial preview of the text

Download Lecture Notes for CPSC 121 Fall 2012: While Loop Exercises and Function Computation and more Study notes Computer Science in PDF only on Docsity!

Lecture Notes CPSC 121 (Fall 2012)

Today ...

  • Go over quiz and exam questions
  • While loop exercises

Reading

  • Ch 7

S. Bowers 1 of 2

Lecture Notes CPSC 121 (Fall 2012)

While versus for loops

Q: What does this function compute?

def f(n): r = 1 for i in range(1, n+1): r = r * i return r

  • Returns the product of the first n integers

Q: How can we write this using a while loop?

def f(n): r = 1 i = 1 while i <= n: r = r * i i = i + 1 return r

S. Bowers 2 of 2