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
  • Introducing If/Else Statements
  • If/Else Example
  • If Statements vs. If/Else Statements

Was this helpful?

  1. Introduction to Programming in Java with Karel the Dog

If/Else Statements

One of the driving forces behind writing conditionals is the need to vary the program's behavior based on certain conditions. When writing comprehensive conditionals, you will often want to have your program do something specifically when a particular if statement is evaluated as false. This is when the else statement becomes useful.

Introducing If/Else Statements

Think of the else statement as the "otherwise" conditional. Consider the following pseudocode for an alarm clock function:

  • If it is the weekend, go off at 10 AM.

  • Otherwise, go off at 7 AM.

The "otherwise" clause allows us to account for all possible scenarios. In this example, we could have executed the same logic using two if statements, as follows:

  • If it is the weekend, go off at 10 AM.

  • If it is a weekday, go off at 7 AM.

However, when writing comprehensive programs, it may not be practical to account for every possible scenario. Suppose we want the alarm to go off at 7 AM on Tuesday and Thursday, and 8 AM on all other days. Using an "otherwise" clause, the logic is as follows:

  • If it is Tuesday or Thursday, go off at 7 AM.

  • Otherwise, go off at 8 AM.

Without using an "otherwise" clause, the logic follows:

  • If it is Tuesday or Thursday, go off at 7 AM.

  • If it is Sunday, Monday, Wednesday, Friday, or Saturday, go off at 8 AM.

It is clear that the former is more concise and pragmatic.

If/Else Example

Suppose we want Karel to move if he can, and turn left otherwise. Keep in mind the following rules of thumb when writing if/else statements:

  • Every else statement must be preceded by an if statement

  • Not every if statement must have an else statement

  • Else statements have no conditions, i.e., there are no parentheses following the "else"

Your code should then look like this:

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

Note that, stylistically, the else statement may begin on the same line as the if statement's closing bracket, or on the following line. It is recommended that you stay consistent with whichever method you prefer. The code above may also be written as:

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

If Statements vs. If/Else Statements

In general, else statements are not required.* You can usually achieve the effect of an else statement using a series of if statements. While this is valid, it is preferred that you use else statements for clarity and conciseness.

*You may later be faced with scenarios in which else statements are required when writing helper functions with return values. More on this to come.

PreviousIf StatementsNextControl Structures Example

Last updated 5 years ago

Was this helpful?