# Karel the Robot

 INTRODUCTION LESSON 1 LESSON 2 LESSON 3 LESSON 4 EXERCISES FAQ EN DE SK
Now we're going to start real programming...

Firstly, what does this mean? Programming in Karel language means to write one or more procedures. A procedure is a sequence of commands which perform something sensible, a command can be either a simple instruction for Karel (as introduced in lesson 2) or a control flow statement (loops and conditional statements) or an execution of another procedure.

The usual approach in programming (and not just for Karel) is to break down complex problems to a number of simpler problems, then each simple problem to an even simpler problem and so on until the partial problems are so simple that you can solve them by simple procedures with a couple of statements. This step is usually called the analysis. Then you can create procedures solving more complex problems by calling other procedures and repeat this process until you have finished with a procedure solving the whole complex problem. This step is usually called development. The development phase should be followed by testing and finally (for programmers this is the most painful part) by writing documentation of the program.

Let's demonstrate with a simple example: Karel is standing in the middle of the world, the world is empty and the mission is to build a wall around the world, as high as possible. Initially this looks like a complex problem so we have to do some analysis first. We don't want to start building the wall from the middle of the world, so we have to get to its edge.  We don't want to start from the middle of the side either, so we have to get to a corner. Now we can start to build, but we have to decide if we want to build it side by side and each side layer by layer or layer by layer and each level side by side. Wait, as we learned in previous lesson Karel cannot step up or down more than one level, so building the wall side by side is not a good idea, we must build it layer by layer. It seems that we identified that we have couple of simple problems.
1. we have to get to the edge of the world
2. we have to get to the corner of the world
3. we have to build the wall
4. we have to build it layer by layer
5. we have to build the level side by side
and now we can start writing procedures to solve them.

First procedure, let's call it GoToEdge, will repeatedly move until Karel reaches the edge of the world. We already know from previous lessons how to move, but how do we repeat this step? It's a construct of programming languages called a loop. In Karel, there are two kinds of loops. The first loop can be used to repeat something a specific number of times (we'll use it later and call it a repeat loop) and the second loop can be used to repeat something while some condition is met (we'll call it a while loop).

And what is the condition? There are a couple of predefined conditions in Karel, each of them can be either True or False. E.g. CanMove (it's True if Karel can move to the next field and False otherwise) and CanNotMove (it is True if Karel can't move to the next field or False otherwise). There is also IsBrick (True if Karel is standing on the field with one or more bricks) or IsBeeper (true if Karel is standing on a field with beeper) or FacingNorth (true if Karel is facing north, which is on top right side of the world). You now understand the meaning of FacingEast, FacingSouth and FacingWest or IsNotBrick, IsNotBeeper, NotFacingNorth, NotFacingEast, NotFacingSouth or NotFacingWest.

So now we can write a procedure in your iPad in Karel programming language like this:

`Procedure GoToEdge`
`  While CanMove`
`    ``Move`
`  ``EndWhile`
`EndProcedure`

First, create the procedure - touch the Edit button on the "Library" panel, than touch the + button, touch the Edit button in the "Procedure" panel, rename the procedure from "NewProcedure" to "GoToEdge", touch the + button again, select the command While, select the condition CanMove and we have entered the first Karel command, in this case an empty while loop.

Now we have to add the body of this loop, in this case the instruction Move. Click green the + sign left from the EndWhile command, select the command Move and we're back in the procedure body and it's complete, so click Done. If you didn't make any mistakes, it should look like this:

It's time see if the procedure is working correctly. Clear the world by clicking the Trash bin icon in the right window and then the Execute button below in the Procedure panel. Karel should make a couple of moves and stop on the edge of the world.

Now we have to write the next procedure to move Karel to the corner. What does this mean? He should turn left and wait... than repeat move until he will reach the edge of the world again. Sounds like we can reuse the GoToEdge procedure, doesn't it? So let's write it:

`Procedure GoToCorner`
`  GoToEdge`
`  TurnRight`
`  GoToEdge`
`ProcedureEnd`

How to put it into your iPad? Touch the <Library button to return to the Library screen, touch + to create another procedure, touch Edit, name it "GoToCorner", touch +, select command Execute, select procedure GoToEdge, touch + again, select command TurnLeft, touch + sign again, select command Execute, select procedure GoToEdge, touch Done and that's it.

Now we can try our first complex procedure. Clear the world again and execute the procedure and if you didn't make a mistake, Karel is at the bottom corner of the world. We are ready to start building the wall.

We will do this by writing two procedures, the first one will build one layer of the wall on one side and the second one will build one layer of the wall around the world by repeating the first step four times.

 `Procedure BuildOneSide` `  TurnRight` `  While CanMove` `    Move` `    PutBrick` `  EndWhile` `EndProcedure` `Procedure BuildOneLayer` `  Repeat 4x` `    Execute BuildOneSize` `  EndRepeat` `EndProcedure`

As you can see, we've used a new construction in last two procedures. Repeat loops for a number of iterations instead of a condition. Now it's time to put the pieces together and write the last procedure of this program, the main procedure:

`Procedure BuildWall`
`  Execute GoToCorner`
`  ``Repeat 9x`
`    Execute BuildOneLayer`
`  EndRepeat`
`EndProcedure`

Now put all the procedures into your iPad, execute the last one and let's see what happens... If you didn't make any mistakes, you should finish with something like this:

Well, this is what you will see if you did everything correctly, but what  if you didn't or you simply want to change something? Fortunately you can correct it easily, open the procedure with the mistake, touch Edit button and you can rename the procedure, add, remove or drag the statements as you need. You can also change the statements themselves by clicking the > sign right from the command you want to change - it just opens the list of commands and you can choose another one.

For practice, let's change the above example a little bit. Karel should build the wall only when there's no beeper in the corner.

Open the procedure BuildWall, touch Edit, touch +, select command If, select condition IsNotBeeper. Now drag the second line (Repeat 9x) by the handle on the right side (three lines) between the If, IsNotBeeper and EndIf commands.

And that's it, we've changed the program. Now it will make Karel to go to the corner, but only continue building the wall when there's no beeper in the corner.

Easy, isn't it? Time to share it with your friends. Go on to lesson 4....