Lecture 4-1: if-else statements

After reading through Chapter 4, you should be able to

Today's Learning Objectives

Flow of Control

Unless indicated otherwise, the order of statement execution (the flow of control) through a method is linear. That is, one statement after the other in the order they are written

Java provides two other ways to change the flow of control:

  1. Conditional statements (branching: if and switch)
  2. Repetition statements (looping: while, for, and do)

Conditional Statements

A conditional statement lets us select which statement will be executed next. By allowing selections, conditional statements give us the power to make basic decision choices in our programs

Java's conditional statements include

  1. if statement
  2. if-else statement
  3. if-else-if statement
  4. switch statement
  5. conditional operator

The if Statement

The if statement has the following syntax:

      if ( condition )
         statement;

The semantics (meaning) of the if statement is if the condition is true, do the statement that immediately follows.

If the condition is false (not true), skip the statement that immediately follows.

Example: if Statement

An example of an if statement:

Code Example 4-1-1

      if ( age >= 21 )
         System.out.println("Congratulations, you're legal");

Age.java

Code Example 4-1-2

   // Declare constant
      final int YOUTH = 21;

   // Declare variables for user input

      int age;
     
   // Create a Scanner object to read input 
     Scanner keyboard = new Scanner(System.in);

   // Get the user's age
      System.out.print("Enter your age: ");
      userInput; = keyboard.nextInt();

   // Re-display the user input

      System.out.println ("\nYou entered: " + age);

   // Example if statement (indent 3 spaces)
       if ( age < YOUTH )
         System.out.println ("\nYouth is a wonderful thing.");

   // Always do this statement

      System.out.println ("\nAge is a state of mind.\n");

Boolean Expressions

A condition often uses one of Java's six relational operators, which all return boolean results:

  1. == equal to
  2. != not equal to
  3. < less than
  4. > greater than
  5. <= less than or equal to
  6. >= greater than or equal to

Note the difference between the equality operator (==) and the assignment operator (=)

The if-else Statement

An else clause can be added to an if statement to make it an if-else statement:

      if ( condition )
         statement1;
      else
         statement2;

The semantics (meaning) of the if-else statement is that if the condition is true, statement1 is executed; otherwise, if the condition is false, then statement2 is executed.

One or the other statements will be executed, but not both

Wages.java

   // Declare constant values
      final double PAY_RATE = 8.25;
      final int HOURS_IN_WORK_WEEK = 40;

   // Declare variables
      int hours;
      double pay;
      
   // Create a Scanner object to read input 
      Scanner keyboard = new Scanner(System.in);

   // Get the number of hours worked this week

      System.out.print ("Enter the number of hours worked: ");
      hours = keyboard.nextInt();

   // Use if-else statement to determine pay
   // overtime is at "time and a half"
      if ( hours > HOURS_IN_WORK_WEEK )
         pay = (HOURS_IN_WORK_WEEK * PAY_RATE) +
               ((hours - HOURS_IN_WORK_WEEK) * (PAY_RATE * 1.5));
      else

         pay = hours * PAY_RATE;

   // Display the amount earned
      System.out.println ("\nGross earnings: $" + pay);

Block Statements

      if ( guess == answer )
         System.out.println ("You got it! Good guessing!");
      else 
      {

         System.out.println ("That is not correct, sorry.");
         System.out.println ("The number was " + answer);
      }

Nested if Statements

      if (num1 < num2) 
      {
         if (num1 < num3)
            min = num1;
         else

            min = num3;
      }
      else 
      {
         if (num2 < num3)
            min = num2;
         else

            min = num3;
      }

URL: http://www.cwu.edu/~schwing/cs110/Java/notes/lecture4-1.html
Title: CS 110: Lecture Notes for Programming Fundamentals 1
Author: Ed Gellenbeck, Central Washington University
Last Updated: Monday, January 16, 2006