H
21

Picked up a tip at a free library workshop that fixed my loop problem

I was stuck on a nested loop for two days. Kept getting wrong outputs and couldn't figure out why. Then I went to this free coding workshop at the downtown library last Saturday. The instructor said to print out each loop iteration step by step. I did that for my 3 levels of nesting and saw the bug in 10 minutes. It was a simple off-by-one error in my inner loop. Has anyone else found that printing intermediate values saves more time than it costs?
2 comments

Log in to join the discussion

Log In
2 Comments
noahs82
noahs821mo ago
Man I spent a whole afternoon printing out loop values once and realized I had my variables swapped in the condition. Felt like a real genius.
2
dixon.willow
Wait, didn't you mean you had the loop condition wrong? Because swapping variables in the condition is actually a pretty common pitfall too, but the real beauty is when you print out values and see your index going from 0 to whatever, and you realize you had the wrong comparison operator the whole time. Like using a less-than when you needed a greater-than, or forgetting your array starts at 0 so you're off by one on every iteration. I've definitely done the "wait, why is this printing 100 times" thing more times than I care to admit.
4