AP Computer Science in Java
  • Introduction
  • Introduction to Programming in Java with Karel the Dog
    • Introduction to Programming with Karel
    • More Basic Karel
    • Java Programs and the Run Method
    • Karel Can't Turn Right
    • Methods in Karel
    • Top Down Design and Decomposition in Karel
    • Commenting Your Code
    • SuperKarel
    • For Loops
    • While Loops in Karel
    • If Statements
    • If/Else Statements
    • Control Structures Example
    • How To Indent Your Code
  • Basic Java
    • Printing in Java
    • Variables and Types
    • User Input
    • Arithmetic Expressions
    • Casting
    • Booleans
    • Logical Operators
    • Comparison Operators
    • For Loops
    • While Loops
    • If Statements
    • Loop-and-a-Half
    • Short-Circuit Evaluation
    • De Morgan's Laws
    • Strings
  • Methods
    • Java Methods
    • Methods and Parameters
    • Methods and Return Values
    • Javadoc and More Methods
    • Strings Methods
    • Strings and Characters
    • Exceptions
    • String Processing
  • Classes and Object-Oriented Programming
    • Introduction To Classes and Objects
    • Classes vs. Objects
    • Using a Class as a Client
    • Writing Classes
    • Writing Classes and Instance Methods
    • Getter and Setter Methods
    • Class Methods and Class Variables
    • Method Overloading
    • Local Variables and Scope
    • Key Terms for Classes
    • Objects vs Primitives
    • Inheritance
    • Class Design and Abstract Classes
    • Polymorphism
    • Interfaces
  • Data Structures
    • What Are Data Structures?
    • Introduction to Arrays
    • Using Arrays
    • ArrayList Methods
    • Arrays vs ArrayLists
    • 2D Arrays (Matrices or Grids)
    • Hashmaps
  • Algorithms and Recursion
    • What is an Algorithm?
    • Pseudocode
    • Linear Search
    • Binary Search
    • Selection Sort
    • Insertion Sort
    • Advanced: Recursion
    • Mergesort
Powered by GitBook
On this page
  • Asking Questions about Karel's World
  • Introducing Conditions
  • Introducing If Statements

Was this helpful?

  1. Introduction to Programming in Java with Karel the Dog

If Statements

Now that Karel can understand and learn new commands, it is time to write programs that have Karel adapt to different situations. Like methods, these new additions to Karel's language will allow us to write code that can be used in many different situations.

Asking Questions about Karel's World

Karel's world tends to change a lot. Sometimes it is large, other times it is small. It often has walls, but not always. And though there are usually tennis balls scattered throughout the world, the balls are rarely in the same place for each exercise.

Given these changing conditions, there are many questions that would be useful for Karel to ask. For example:

  • Is there a ball where Karel is standing?

  • Is Karel facing north?

  • Is Karel hungry?

Introducing Conditions

These types of questions can be asked using conditions. Conditions are very simple methods that look at the state of Karel's world and return a true or false answer. Here is a list of the conditions that Karel can check:

.

.

frontIsClear()

frontIsBlocked()

leftIsClear()

leftIsBlocked()

rightIsClear()

rightIsBlocked()

facingNorth()

notFacingNorth()

facingSouth()

notFacingSouth()

facingEast()

notFacingEast()

facingWest()

notFacingWest()

ballsPresent()

noBallsPresent()

Like Karel's other commands, it is important to include the parentheses () at the end.

In the following world, the frontIsClear() condition returns true, as Karel's front is clear. The ballsPresent() condition also returns true, because Karel is standing on a ball. Similarly, the facingSouth() condition would return false, as Karel is not facing south.

Introducing If Statements

Checking conditions allows us to write programs that can respond to changes in Karel's world. One important way to handle different conditions is by using if statements. If statements let us check a condition before executing any code. We can use if statements to help control the flow of the program. The basic format of an if statement is:

if(condition)
{
    // code to run
}

An if statement will only execute if the condition is true. Such checks probably sound familiar -- in fact, we use the same type of thinking in everyday life. Here are some examples of real-world if statements:

  • if there is music, then I will dance

  • if I am tired, then I will sleep

  • if it is raining, then I will carry an umbrella

Pay attention to the structure of these statements. The first part is used to check the condition of the world (am I tired? is it raining?). The second part contains the "code" that will execute if the check is true. Thus, if it is raining, I will take my umbrella. On the other hand, if it is not raining, then I will not carry an umbrella.

Karel uses if statements and conditions in a similar way. For example, in the world above, Karel can check if the front is clear before moving:

if(frontIsClear())
{
    move();
}

Using an if statement to check the world before moving helps Karel avoid crashing into the wall. Such a check would be important in a world like this one:

PreviousWhile Loops in KarelNextIf/Else Statements

Last updated 5 years ago

Was this helpful?

Karel Conditions
Karel's front is blocked