Skip to content

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! ๐Ÿ”

for i in range(8):

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! ๐Ÿ 
for i in range(8):  # ๐Ÿš€ Signal flare! "From here on, the loop body begins!"
    print(i)        # ๐Ÿ  The loop body (indentation required!)

Reading it line by line

for i in range(8):if you read it line by line:

  1. for โ†’ "Start looping!" ๐ŸŽฌ
  2. i โ†’ "I'll put a number in the basket!" ๐Ÿงบ
  3. in โ†’ "from"
  4. range(8) โ†’ "Make me 8 from the number factory!" ๐Ÿญ
  5. : โ†’ "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!
  • Lines 15-16: ๐Ÿ  inside the loop's roomis the code here!

    • It's indented, so it runs repeatedly!
    • On every turn, i the 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.2 calculate 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

  1. First iteration: i = 0 ๐Ÿงบ
  2. a cube is made (size: 1 + 0 * 0.2 = 1.0) ๐Ÿ“ฆ

  3. Second iteration: i = 1 ๐Ÿงบ

  4. another cube is made (size: 1 + 1 * 0.2 = 1.2) ๐Ÿ“ฆ

  5. Third iteration: i = 2 ๐Ÿงบ

  6. another cube is made (size: 1 + 2 * 0.2 = 1.4) ๐Ÿ“ฆ

  7. ... it keeps repeating like this... ๐Ÿ”„

  8. Eighth repetition: i = 7 ๐Ÿงบ

  9. 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!