Concept map: For loops

Jan 31, 2013 • Karthik Ram

Here is my version of a simple concept map explaining when to use a for loop.

for_loop

Questions to test understanding of for loops:

You have 450 pencils numbered sequentially. 1 through 50 have been sharpened. The rest need to be completed. Please fill out in plain English the instructions that need to go into boxes A, B, C, and D in the loop concept flow above.
Answer:

A: Pick up pencil 51.
B: Stop if the current pencil in hand is numbered 450.
C Grab the next pencil in the sequence.
D Sharpen pencil, return to box.

 

Q2: Your boss gives  you another assorted box of 450 pencils. But since he has a weird form of OCD, he only wants you to sharpen the black ones and ignore the others. How will you modify your previous answer to solve this problem? Hint: any box can contain more than one instruction.

 

Answer:

A. Start at pencil #1

D. Am I holding a black pencil?
If yes, then sharpen it and put in the box.
otherwise
just put it back.