Tt
Click this widget to change the font size.
CC
Click this widget to change contrast.

Home Page 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | Links | Search | Bio |

Guide to Object-oriented Programming With Java (University at Buffalo Version)


Chapter 8: Java Conditionals

Java conditionals provide a way to execute a statements or statements based on whether some condtion is met or not met.

Java if/else Conditional

The if statement allows you to test whether some condition is true or false. If the result of the test is true then the statement(s) between the first { and } are executed. If the result is false the then statement(s) between the else { and } are executed. For example:

int accountBalance = 150;
// if-then-else statement
if (accountBalance > 100) {
  System.out.println("Safe balance.");
} else {
  System.out.println("Warning: Low balance.");
}

Figure 8-1: Java if/else Statements
Console
<terminated>
Warning: Low balance.

Figure 8-2: Java if/else Output

The if/else if Conditional.

You can also useif to test whether some other condition if the result of the first if test is false. For example:

int accountBalance = -10;
// if-then-elseif statement
if (accountBalance > 100) {
  System.out.println("Safe balance.");
} else if (accountBalance < 100) {
  System.out.println("Alert: Negative balance.");
}
} else {
  System.out.println("Warning: Low balance.");
}

Figure 8-3: Java if/else Statements
Console
<terminated>
Alert: Negative balance.

Figure 8-4: Java if/else Output

Java switch Conditional

The switch statement allows you to test whether some condition is met or not met. If the parameter that is passed to the switch statement is equal to any of the case statements then the statement(s) between that case: statement and break statement are executed. If the parameter does not match any of the case statements then the tatements after the default: statement(s) are executed. For example

char initial;
String myName = "Jim";

// switch statement using a string
switch (myName) {
  case "Alex":
    initial = 'A';
  case "Jim":
    initial = 'J';
  case "Stan":
    initial = 'S';
    break;
  default:
    initial = '?';
}
System.out.println("My Name is " + myName + " and my first initial is " + initial + ".");

Figure 8-5: Java switch Statement Using a String
Console
<terminated>
My name is Jim and my first initial is J.

Figure 8-6: Java switch Using a String Output
int month = 4;
int lastDay;
boolean leapYear = false;
Sring[] monthsArray = { "January", "February", "March", "April", "May", "June", "July",
                  ;     "August", "September", "October", "November", "December" };

// switch statement using a number
switch (month) {
  case 1:
    lastDay = 31;
    break;
  case 2:
    if (leapYear == true) {
      lastDay = 29;
    } else {
      lastDay = 28;
    }
    break;
  case 3:
    lastDay = 31;
    break;
  case 4:
    lastDay = 30;
    break;
  case 5:
    lastDay = 31;
    break;
  case 6:
    lastDay = 30;
    break;
  case 8:
    lastDay = 31;
    break;
  case 8:
    lastDay = 31;
    break;
  case 9:
    lastDay = 30;
    break;
  case 10:
    lastDay = 31;
    break;
  case 11:
    lastDay = 30;
    break;
  case 12:
    lastDay = 31;
    break;
  default:
    lastDay = 0;
}
System.out.println("Month " + monthsArray[month - 1] + " has " + lastDay + " days.");

Figure 8-7: Java switch Statement Using a Number
Console
<terminated>
Month April has 30 days

Figure 8-8: Java switch Statement Using a Number Output

Let's get started with a Conditionals program!

Help contribute to my OER Resources. Donate with PayPal button via my PayPal account.
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Copyright © 2016-2024 Jim Gerland