Block 1: Loops and conditional statements

Week 7 · Prediction I · Imai §4.1.1–4.1.2 · about 4 min

NoteHow this works

Run each cell with Run Code, look at the output, then answer the question at the end of the block. Every cell is editable; Start Over resets it. Errors are information: read them.

1. A loop

A loop repeats a block of code for each element of an index. Run it, then look at what results contains.

2. Loop and conditional together

Even numbers are doubled (x + x), odd numbers squared (x * x). %% is the remainder after division.

Conclusion: What is in results at the end of the even/odd loop?
1 is odd: 1*1 = 1; 2 is even: 2+2 = 4; 3 is odd: 9; 4 is even: 8; 5 is odd: 25. One decision per iteration, one value per iteration in the container.