Lecture notes for the Introduction to Computer Science I by James Tam Return to the course web page

Example Programs: Loops

Description

Program name

A while loop that counts up from 1 to 4

while1.py

A for loop that sums a series from 1 - 4.

for1.py

A for loop that sums a series from 5 to 1.

for2.py

An example of a sentinel controlled loop that sums a series of non-negative numbers

sum.py

A problem solving example: writing a number guessing game

guessGame.py

Infinite loop: the loop control is never updated

infinite1.py

Infinite loop: the loop control is updated but never brought closer to the stopping condition

infinite2.py

A nested loop example: two counting for loops, one inside of the other

nested.py