If Statements & Conditionals

In Karel’s world, you can use if statements to check if there is a ball where Karel is standing or if Karel is facing some direction. It returns either a true or false. We can use these returns to prevent Karel from running into walls or try to pick up a ball where there isn’t one. If statements run the code inside them only if the stated condition is true. Example:

if (ballsPresent()) {

takeBall();

}


In addition, if the condition is false, you can add an else after the if statement to run a code if the condition isn’t met. Note that it is not possible for both pieces of code to run. It is either one or the other.