Chunk c code
๐ก Understanding the Code
Now let's take a close look at the loop part of the code!
๐ Syntax map: for i in range(8): fully breaking it down
Let's fully break down the first line of the loop and understand it! ๐
This one line is made up of 4 parts:
| Element | Role | Analogy |
|---|---|---|
for |
that starts the loop Keyword | the "start looping!" command ๐ฌ |
i |
that holds the current number basket (variable) | ๐งบ a basket that holds a number |
in |
meaning "from" connecting word | "take from" |
range(8) |
that makes numbers factory | ๐ญ a 0~7 number factory |
: |
the signal flare - the loop body begins! | ๐ "From here on, the loop body begins!" |
๐ The colon (:) is a signal flare!
Colon (:is just like the signal flareis the same!
- When there's a colon: "From here on, the loop body begins!" ๐
- From the line after the colon: indent and write the loop body! ๐
Reading it line by line
for i in range(8):if you read it line by line:
forโ "Start looping!" ๐ฌiโ "I'll put a number in the basket!" ๐งบinโ "from"range(8)โ "Make me 8 from the number factory!" ๐ญ:โ "From here on, the loop body begins!" ๐
Lines 14-16: the heart of the loop
for i in range(8):
print(curve.out_expo(i/8))
Entity(parent=base_cube, model='cube', color=color.orange, scale=1 + i * 0.2, alpha=.1)
Description:
-
Line 14:
for i in range(8):- this is the start of the loop! ๐- from the number factory (
range(8)), makes the numbers 0~7 ๐ญ - and puts them one at a time into the basket (
i) ๐งบ - repeating 8 times!
- from the number factory (
-
Lines 15-16: ๐ inside the loop's roomis the code here!
- It's indented, so it runs repeatedly!
- On every turn,
ithe value changes, and a cube of a different size is made!
How does a loop work? ๐
A loop works by spinning like a carousel! ๐
๐ Calculator worksheet: scale=1 + i * 0.2 understanding the formula
Line 16's scale=1 + i * 0.2 formula looks hard, but it's easy when you see it in a table!
Check how it's calculated on each turn:
| Turn | and puts them one at a time into the basket (i) |
Calculation steps | Final size (scale) |
|---|---|---|---|
| 1st | i = 0 |
1 + 0 * 0.2 = 1 + 0 = 1.0 |
1.0 ๐ฆ |
| 2nd | i = 1 |
1 + 1 * 0.2 = 1 + 0.2 = 1.2 |
1.2 ๐ฆ |
| 3rd | i = 2 |
1 + 2 * 0.2 = 1 + 0.4 = 1.4 |
1.4 ๐ฆ |
| 4th | i = 3 |
1 + 3 * 0.2 = 1 + 0.6 = 1.6 |
1.6 ๐ฆ |
| 5th | i = 4 |
1 + 4 * 0.2 = 1 + 0.8 = 1.8 |
1.8 ๐ฆ |
| 6th | i = 5 |
1 + 5 * 0.2 = 1 + 1.0 = 2.0 |
2.0 ๐ฆ |
| 7th | i = 6 |
1 + 6 * 0.2 = 1 + 1.2 = 2.2 |
2.2 ๐ฆ |
| 8th | i = 7 |
1 + 7 * 0.2 = 1 + 1.4 = 2.4 |
2.4 ๐ฆ |
๐ Understanding the calculation steps
Formula 1 + i * 0.2if you calculate it step by step:
i * 0.2calculate this first (multiplication first!)- Next
1 +Add
Example: i = 2when
1. 2 * 0.2 = 0.4 (multiplication first)
2. 1 + 0.4 = 1.4 (then addition)
Result: a cube of size 1.4 is made! ๐ฆ
๐ Loop flowchart
If you look step by step at how the loop runs:
๐ฌ Start: for i in range(8):
โ
โโโ ๐งบ Turn 1: i = 0
โ โโโ ๐ฆ Make a cube (size: 1.0)
โ
โโโ ๐งบ Turn 2: i = 1
โ โโโ ๐ฆ Make a cube (size: 1.2)
โ
โโโ ๐งบ Turn 3: i = 2
โ โโโ ๐ฆ Make a cube (size: 1.4)
โ
โโโ ... keeps repeating ...
โ
โโโ ๐งบ Turn 8: i = 7
โโโ ๐ฆ Make a cube (size: 2.4)
โ
โโโ โ
Done! 8 cubes complete!
Step-by-step behavior
- First iteration:
i = 0๐งบ -
a cube is made (size:
1 + 0 * 0.2= 1.0) ๐ฆ -
Second iteration:
i = 1๐งบ -
another cube is made (size:
1 + 1 * 0.2= 1.2) ๐ฆ -
Third iteration:
i = 2๐งบ -
another cube is made (size:
1 + 2 * 0.2= 1.4) ๐ฆ -
... it keeps repeating like this... ๐
-
Eighth repetition:
i = 7๐งบ - the last cube is made (size:
1 + 7 * 0.2= 2.4) ๐ฆ
Result: 8 cubes are made in total! ๐
Without a loop vs. with a loop
Without a loop (inefficient) ๐:
Entity(parent=base_cube, model='cube', color=color.orange, scale=1 + 0 * 0.2, alpha=.1)
Entity(parent=base_cube, model='cube', color=color.orange, scale=1 + 1 * 0.2, alpha=.1)
Entity(parent=base_cube, model='cube', color=color.orange, scale=1 + 2 * 0.2, alpha=.1)
Entity(parent=base_cube, model='cube', color=color.orange, scale=1 + 3 * 0.2, alpha=.1)
# ... keeps repeating up to 8 times ... ๐ซ
With a loop (efficient) โจ:
for i in range(8): # ๐ Repeat 8 times like a carousel
Entity(parent=base_cube, model='cube', color=color.orange, scale=1 + i * 0.2, alpha=.1)
Using a loop makes the code much shorter and easier! โจ
Why are loops great?
- Short and clean: You don't have to write the same code over and over!
- Easy to edit: Just change the number to adjust how many times it repeats!
- Prevents mistakes: No chance of slipping up while writing the same code many times!