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
  • Basics of While Loops
  • Examples of While Loops

Was this helpful?

  1. Introduction to Programming in Java with Karel the Dog

While Loops in Karel

PreviousFor LoopsNextIf Statements

Last updated 5 years ago

Was this helpful?

Lets say Karel is standing on one side of the world, and we want to move to the next side. Up to this point, the best way would be to write a for loop that repeats the move(); command a set number of times. What if we don't know the size of Karel's world? In this case we would use a while loop, which will execute a command while a condition is true.

Basics of While Loops

While loops, unlike for loops, execute the target statement as long as the given condition is true.

A while loop is usually formatted like:

while(condition)
{
    // code to execute
}

It is important to know that a while loop will only repeat the target statement until the condition is no longer true. Consider the following diagram:

Examples of While Loops

Consider the following situation:

  • While a vehicle's engine is on, it will use gas.

  • When the vehicle's engine is off, it will no longer use gas.

In this case the vehicle's engine represents the condition of our while loop. The use of gas represents our target statement that will only execute when the condition is met. Your code for this situation should look like:

while(engineOn())
{
    useGas();
}

Here is another example of a while loop, as seen in Karel's world:

  • While a ball is present in Karel's current position, move one space.

  • When Karel lands on a position that does not contain a ball, don't move.

In this situation Karel will only move when a ball is in Karel's current position. As soon as Karel lands in a position that does not contain a ball the loop will exit, and Karel will stop moving. The code for this situation should look like:

while(ballsPresent())
{
    move();
}
While Loop Diagram