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

Was this helpful?

  1. Introduction to Programming in Java with Karel the Dog

Control Structures Example

PreviousIf/Else StatementsNextHow To Indent Your Code

Last updated 5 years ago

Was this helpful?

Control structures allow us to shape the flow of our programs and write code that can adapt to the conditions of the world. Control structures can be combined to produce responsive and powerful programs.

Control Structures

There are two main categories of control structures that we have learned so far. The first group, if statements and if-else statements, ask questions about the world. The second group, for loops and while loops, repeat patterns of code.

Ask Questions

Repeat Code

If Statement

For Loop

If/Else Statement

While Loop

In the world above, Karel needs to put down five tennis balls in every empty spot. If a spot is not empty, Karel should only put down one more tennis ball.

We will need to use several different control structures to solve this problem. First, we need Karel to determine whether there is a ball present or not. If there is not a ball, Karel should put down five tennis balls. Otherwise (if there is already a ball), Karel should just put down one ball. This can be easily written using an if-else statement.

Counting out five tennis balls suggests the use of a loop. In this case, a for loop works well.

Finally, this process needs to occur across the entire world. A while loop can be used to repeat the code as long as the front is clear.

A typical solution for this challenge may look like:

public class LayFiveTennisBalls extends Karel
{
    public void run()
    {
        while(frontIsClear())
        {
            checkAndPutBalls();
            move();
        }
        checkAndPutBalls();
    }

    private void checkAndPutBalls()
    {
        if(noBallsPresent())
        {
            for(int i = 0; i < 5; i++)
            {
                putBall();
            }
        }
        else
        {
            putBall();
        }
    }
}
Karel needs to place tennis balls
Karel needs to place tennis balls