CS 111: Introduction to Computer Science

Some examples for class on 9/24/18

Loop 1

total = 0 n = 10 k = 0 while k < n: total = total + (2*k + 1) #print(k, total) k = k + 1 print(total)

Nested for loops

for j in range(4): for k in range(3): print(j, k) j = 0 while j < 4: k = 0 while k < 3: print(j, k) k = k + 1 j = j + 1

Nested for loops again

for j in range(4): for k in range(j): print(j, k)

Nested function calls

def moose(n): return 2 * n + 1 def emu(a, b): return 3 * a + b def kudu(n): return moose(2*n) + emu(n - 1, n + 1) print(kudu(5))