კარელი გპტ

Chapter 5 — For Loops (Karel-style)
Karel — Reader (class-friendly)
Chapter 5

Chapter 5: For Loops

Computers are very good at repeating actions. In this chapter we learn how to tell Karel to do the same steps several times using a for loop.

Basic for loop

Before — empty world
After — beepers placed

When you know exactly how many repetitions you need, a for loop is the right tool. The compact form we use in these examples is:

for i in range(count):
    # statements to repeat

Matching postconditions with preconditions

The most important part of loops is ensuring that after each iteration the world is in the right state for the next iteration to start. If the state assumptions don't hold, the loop may fail or behave unexpectedly.

Nested loops

Loop bodies can contain other loops. These nested loops let you repeat complex patterns (e.g., making a grid or drawing multi-line shapes).

def draw_row():
    for i in range(4):
        put_beeper()

for row in range(3):
    draw_row()
    move_to_next_row()
* This page is a class-friendly, static HTML copy for embedding in blogs.

Comments

Popular posts from this blog

პითონით თამაში ვ2

GPT8 (მუშაა)

clode 10 მუშაა ჩასწორებულია