kareli კლოდი 1
Chapter 5: Control Flow with Karel
Control flow is one of the most important concepts in programming. It allows Karel to make decisions
and repeat actions based on conditions. In this chapter, we'll explore how to use if statements,
while loops, and other control structures to make Karel behave intelligently.
While Loops
A while loop allows Karel to repeat a set of commands as long as a certain condition is true.
This is incredibly useful when we don't know exactly how many times we need to repeat an action.
For example, Karel can move forward until hitting a wall, or pick up beepers until there are none left.
while front_is_clear(): move()
If Statements
The if statement allows Karel to make decisions. Karel can check a condition and execute
different code depending on whether the condition is true or false. You can also use else
to specify what should happen when the condition is false.
front_is_clear(),
beepers_present(), facing_north(), and more.
Try It Yourself!
Below is an interactive code editor where you can write Karel programs. Try modifying the code to make Karel perform different tasks. Click Run Code to see your program in action!
Common Patterns
Here are some common control flow patterns you'll use frequently with Karel:
- Move to wall: Use
while front_is_clear(): move() - Turn right: Call
turn_left()three times - Turn around: Call
turn_left()twice - Clear all beepers: Use
while beepers_present(): pick_beeper()
Comments
Post a Comment