In chapter 3 of Karel the Robot, I have learned that Karel uses Control Statements. In chapter 2, Karel simply followed commands in order. Some of these commands were separated in to methods so that a single command could be used to issue several commands from a block, but in essence, Karel would do in order what you told him to do.
Control Statements change things in that you can tell Karel to do something based on a condition of his surroundings or based on a loop so he can do something while something else is true… such as “move forwards if there is no wall ahead”.
Conditional Statements
Two types of control statements are introduced in chapter 3 of Karel the Robot. The first type are called Conditional Statements. These are coded by using what is called the if statement. What this means is that a certain part of the program will run “if” a condition holds true. That condition could be something like “if Karel has beepers in his bag, run this section of code”. When his beepers run out, the program will then stop or move on to the next part.
Example:
if (condition) {
statements put between the curly braces.
}
Lets break down the example in to a real example. In previous chapters you will have only been presented with 4 commands which are to move, pick beeper, put beeper and turn left. We created a turn right by defining a method. Chapter 3 of the eBook shows that there are 18 more commands that Karel understands. One example is noBeepersPresent() which is used to check if the position at which Karel is standing contains a beeper or not. Based on this command, we can use it in a conditional statement. In the example below I want to tell Karel to put a beeper at his current spot, but only if a beeper is not already present. In this example, the code would be written as follows:
if (noBeepersPresent()) {
putBeeper();
}
Lets break this down a little, but before I do, please note that simply pasting these 3 lines of code in to Eclipse will not work alone… they need to form part of a larger program which you will have already learned about in Chapter 2 of Karel.. ie, the private void fillPotHole() method or similar.
Line 1 checks the “condition” and runs when the answer is true. ie… if there is no beeper present then execute the block of code below. If there was a beeper already present then the next 2 lines would be skipped over and nothing would be done.
Line 2, if executed, will put a beeper on the corner. Note the semi colon ; at the end of the statement. This is required by Java at the end of each statement although not at the end of the if and closing curly brace.
Line 3 just closes off the “block” of code. In this case, the block of code is just a single statement but if you want Karel to do something else after putting a beeper within the “if” condition, then you can add more statements below line 2 but before line 3.
This is simply what conditional statements are. In Karels world they are relatively simple to explain but can be complicated to execute when you need to do something more complicated. As nearly ALL programs will require some kind of logic, I suggest you get familier with if statements as they will form a good part of your code when you come to write simple and more complex problems.
One thing to take in to consideration is that control statements can be nested within other control statements. One example shared in the book is to use the code above to check if noBeepersPresent() and if that is true then before you put a beeper down, check Karels beeper bag to make sure he has one to put one down. In this example, the code would be written as follows:
if (noBeepersPresent()) {
if (beepersInBag()) {
putBeeper();
}
}
Line 1 is the initial conditional check. If not true then the program would skip to beyond line 5. If true, it would then evaluate the condition on line 2 and if beepers are available, one would be put down. If no beepers available then none would be put down and the nested condition statement would end as would the main condition statement. This type of style of coding is great for catching bugs. Had we not put the nested condition in there, Karel may or may not have been successful at putting a beeper down. One thing that you need to remember with programming, or in this case programming Karel, is that you cannot assume he would know what to do if you ask him to put a beeper down when he has no beepers available. If no beeper is in his bag and you tell him to put a beeper down the program would crash.
Extending the if statement with else
Using the condition statements above you can achieve some powerful programming tasks, but the if condition alone is more powerful than I already presented. At the moment it simply says… if true then do this and if not, do nothing. If can be extended by using an else condition. What this means is that if something is true then do this, if not then do something else.
Example:
private void invertBeepers() {
if (noBeepersPresent()) {
putBeeper();
} else {
pickBeeper();
}
}
What we are doing in the example above is checking to see if there is no beeper present and if not, put a beeper. If that statement returns false and there is a beeper present then it runs the else portion of the code and picks the beeper up. The method name is on line 1 of the code and is called invertBeepers and this is what the code checks for. This is written in simple form. You could be more advanced and add the nested if statement within that code so you can check if you have a beeper to put to prevent the program crashing out.
This section of my post has covered the basics that I have recently learned. I suggest you get familier with control statements as reading on a bit, it appears that they will be used a lot.
Iterative Statements
Iterative statements are another part of control statements as they control the way in which the program runs. Iterative statements form what is called a loop. Two types of iterative statements exist which are the for loop and the while loop. A for statement is primarily used to perform a command or group of commands a specific number of times. When the script runs, it will keep looping around X number of times and when done, it will drop out of the loop and continue executing the remaining parts of the program.
A while loop performs the same block of code while the statement holds true. An example here could be that there are 20 beepers on a corner and you tell Karel to pick one up. If you put that in a while loop, Karel will keep picking up beepers until there are none left. At that point the while loop can be exited and the remaining part of the program can carry on running.
A for loop example
As mentioned just above, for loops are used to loop through a certain number of times. If you want Karel to move 8 times, you could put the move() command in 8 times and be done, but why write 8 lines of code when you can do it in 3 and mix in more power to what is done within the loop?
The for loop basics:
for (int i = 0; i < count; i++) {
statements.
}
The above forms the basics of a for loop. Let me break it down for you quickly. Line 1 sets up the loop with three parts. "int i = 0" sets what is called a variable of integer type to have the value of zero... more on variables later. The next part which is "i < count" is the condition check or test. At this point the program checks to see if i is less than a specified count. That count could be the number 8... so if i = 0 then i is less than 8 and therefore, run the statements. i++ is a command that tells the i variable to be incremented by 1. This means that when the loop runs the first time it has a zero and then when it goes through a second time i is then equal to 1 and then 2, 3, 4. When it gets to "count" (which could be 8) that test is no longer true and the program drops out of the loop and continues. Example:
for (int i = 0; i < 8; i++) {
move();
}
The example code above has the count set at 8 which means that the move command will be run 8 times before the loop drops out and the program continues. Remember that counting starts at zero and the last run time will be when i = 7 as 7 is less than 8. Zero plus the 1 - 7 equals the 8 cycles in the loop.
A while loop example
The while loop works a little differently in that it runs an indeterminate number of times until a condition is no longer true. The standard for this is written as follows:
while (test) {
statements to be repeated
}
The "test" is condition of what the program is testing. Perhaps Karel has been asked to move forward if there is no wall ahead. One command that would work here is frontIsClear(). A while loop would see Karel moving forwards repeatedly until he was up against a wall. The example of this code is as follows:
while (frontIsClear()) {
move();
}
frontIsClear is defined in what is called SuperKarel and simply checks if the front is clear. If the front is clear then move Karel 1 space and check if the front is still clear... if so move him, if there is a wall then drop out of the loop.
Fencepost Errors
One common problem you will find when programming Karel is that you hit what is called a fencepost. What you can inadvertently do is assume your code is correct, but actually exit out of a loop too early as you haven't taken an end wall in to consideration. When Karel hits a wall the program might end, but instead, you might have wanted to do one final check. Make sure you account for what happens when you exit the loop. As you start coding in the practices and assignments, you will come across this problem. Watch out for it and try resolve it yourself by inserting a bit more logic through a control statement.
Watch out for bugs
When writing control statements, it can be easy to get bugs in your code. When writing the code and adding the control statements, it might look correct on first glance but you might find that some type of check is missing or that you had assumed something too much. For this reason, test your code and make sure it does what it should before moving on to the next part of the test.
In Conclusion
You have been given a basic run down of how control statements work and the 3 types within which are classed as conditional and iterative. By learning how to use these and practicing various ways to work with them, you can create some very powerful logic that can automate solving various problems.
Leave a Reply
You must be logged in to post a comment.