Java
Java intro
Java basics
Java variables
Java conditionals
Java loops
Java arrays
Java OOP 1
Java OOP 2
Java interfaces
Java Strings
Java user input
Java exceptions
Java packages
Java rand numbers
Java GUI's
Java GUI layout
Java events
Java applets
Java graphics
Java sounds
Java summary

Programming

Programming intro

Markup

First webpage guide HTML
XHTML

Style & Layout

CSS

Browser scripting

Javascript
VBScript
AJAX

Server scripting

PHP
ASP

Making money online

Make money online

Java conditional logic

You can implement Java code which makes decisions based on certain things using conditional logic.

This lesson focuses on:

The if statement

The if statement tests if a certain condition is true or false and acts upon it accordingly.

Syntax:

if(condition){
perform this action;
}

If the condition in the parenthesis is true, then the code following the condition will be executed, otherwise it will not.

Example:

class TestCondition{

   public static void main(String[] args){

	//declare a numeric variable
	int aNumber = 5;

	//check if aNumber equals 5 and if it is
	//print a message accordingly
	if (aNumber == 5){

	System.out.print("aNumber is equal to 5");

	}
   }
}

Output:

aNumber is equal to 5

NOTE: Use two equal signs (==) in the condition when comparing values. One equal sign (=) is used to assign values, while two equal signs (==) are used to compare values.

The else statement

If the condition in the parenthesis in an if statement is true, then the code following the condition will be executed. But what if you wanted something to happen if it is false? What if you wanted one thing to happen if the condition is true, and something else to happen if the condition is false? This is where the else statement comes in. The else statement works in conjunction with the if statement and executes certain code if the condition in the if statement is false.

Syntax:

if(condition){
perform this action;
}

else{
perform this action if the above condition is false;
}

If the condition in the if statement is false, then the action dictated by the else statement will be performed.

Example 1:

class TestCondition{

   public static void main(String[] args){

	//declare a numeric variable
	int aNumber = 5;

	//check if aNumber equals 5 and if it is
	//print a message accordingly
	if (aNumber == 5){

	System.out.print("aNumber is equal to 5");

	}

	//otherwise print 
	//a different message	
	else{

	System.out.println("aNumber equals a 
                            number other than 5");
	
	}
   }
}

Output:

aNumber is equal to 5

Example 2:

class TestCondition{

   public static void main(String[] args){

	//declare a numeric variable
	int aNumber = 10;

	//check if aNumber equals 5 and if it is
	//print a message accordingly
	if (aNumber == 5){

	System.out.print("aNumber is equal to 5");

	}

	//otherwise print
        //a different message	
	else{

	System.out.println("aNumber equals a 
                            number other than 5");
	
	}
   }
}

Output:

aNumber equals a number other than 5

The else-if statement

The if statement tests a single condition and performs an action if that condition is true and the else statement performs an action if the condition in the if statement is false, but what if there are more than two possibilities? Surely, any condition can be only true or false, but what if you needed to test a variable for more than one value? This is where the else if statement comes in. The else if statement is used in conjunction with the if statement. Unlike the else statement, it does not specifically perform a certain action if the condition in the if statement is false, but rather it performs an action if the condition in the if statement is another specific value specified in the else if statement itself.

Syntax:

if(condition is one value){
perform this action;
}

else if(condition equals another value){
perform this action; 
}

Example:

class TestCondition{

   public static void main(String[] args){

	//declare a numeric variable
	int aNumber = 7;

	//check if aNumber equals 5 and if it is
	//print a message accordingly
	if (aNumber == 5){

	System.out.print("aNumber is equal to 5");

	}

	//check if aNumber equals 7 and if it is
	//print a message accordingly	
	else if (aNumber == 7){

	System.out.print("aNumber is equal to 7");
	
	}
   }
}

Output:

aNumber is equal to 7

Using if, else, and else if together

You can use the if, else, and else if statements together when you want to check a variable for a certain value many times. If it is not any of the checked values then the code specified by the else statement will be executed.

Syntax:

if(condition){
perform this action;
}

else if(condition equals another value){
perform this action;
}

else if(condition equals another value){
perform this action;
}

else if(condition equals another value){
perform this action;
}

else{
if the condition was not equal to any of the values tested in 
the if statement and all the else if statements, then perform 
this action;
}

Example:

class TestCondition{

   public static void main(String[] args){

	//declare a numeric variable
	int aNumber = 5;

	//check if aNumber equals 9 and if it is
	//print a message accordingly
	if (aNumber == 9){

	System.out.print("aNumber is equal to 9");

	}

	//check if aNumber equals 7 and if it is
	//print a message accordingly
	else if (aNumber == 7){

	System.out.print("aNumber is equal to 7");
	
	}

	//check if aNumber equals 3 and if it is
	//print a message accordingly
	else if (aNumber == 3){

	System.out.print("aNumber is equal to 3");
	
	}

	//check if aNumber equals 15 and if it is
	//print a message accordingly
	else if (aNumber == 15){

	System.out.print("aNumber is equal to 15");
	
	}

	//otherwise print
	//a different message
	else {

	System.out.print("aNumber is not equal to 9, 7, 3, 
                          or 15. aNumber is equal to " 
                          + aNumber); 

	}
   }
}

Output:

aNumber is not equal to 9, 7, 3, or 15. aNumber is equal to 5

NOTE: When using if, else if, and else statements - if the action dictated by these statements contains more than one line of code, it should be sorrounded by curly braces, otherwise the curly braces are optional. Although, curly braces should always be used with conditional statements. Doing so is good convention.

Example:

class TestCondition{

   public static void main(String[] args){

	//declare a numeric variable
	int aNumber = 5;
	int anotherNumber = 7;
	
	if (aNumber == 9) {
	System.out.println("aNumber is equal to 9");
	System.out.print("anotherNumber is equal to 7");
	
	}

	else if (aNumber == 7) System.out.print("aNumber is 
                                                 equal to 7");

	else if (aNumber == 3) {
	
	System.out.println("aNumber is equal to 3");
	System.out.print("anotherNumber is equal to 7");
	
	}

	else if (X == 15) {

	System.out.println("aNumber is equal to 15");
	System.out.print("anotherNumber is equal to 7");

	}

	else System.out.print("aNumber is not equal to 9, 7, 
                               3, or 15, it is equal to 5");

   }
}

In the above example, some of the conditional statements dictate actions with one line of code - not sorrounded by curly braces, and some conditional statements dictate actions with more than one line of code - sorrounded by curly braces.

The switch statement

The switch statement is specifically designed for comparing one variable to a number of possible values. It can be thought of as a substitute for the if, else if, else structure. There is an important keyword used within the switch structure, and that keyword is the break keyword. The break keyword is used to make sure that the switch structure will not fall through to the next possible value, even if that value is incorrect within the switch structure.

Syntax:

switch(variable){

case possible value:
perform this action;
break;

case possible value:
perform this action;
break;

case possible value:
perform this action;
break;

default:
perform this action if none of the values match;
}

Example:

class TestCondition{

   public static void main(String[] args){

	//declare a numeric variable
	int aNumber = 7;

	 //use the variable aNumber 
        //within the switch statement
	switch(aNumber){

	//test if aNumber equals 1 and if it is
	//print a message accordingly
	case 1:
	System.out.print("aNumber is equal to 1");
	break;

	//test if aNumber equals 2 and if it is
	//print a message accordingly
	case 2:
	System.out.print("aNumber is equal to 2");
	break;

	//test if aNumber equals 3 and if it is
	//print a message accordingly
	case 3:
	System.out.print("aNumber is equal to 3");
	break;

	//test if aNumber equals 7 and if it is
	//print a message accordingly
	case 7:
	System.out.print("aNumber is equal to 7");
	break;

	//test if aNumber equals an unspecified value
	//and if it is, print a message accordingly
	default:
	System.out.print("aNumber is not equal to any 
                          of the values specified");
	}	
   }
}

Output:

aNumber is equal to 7

The ternary operator

The ternary operator is the question mark symbol (?), it works the same way as the if-else structure.

Syntax:

variable = (condition) ? value the variable will take if 
condition is true: value the variable will take if condition 
is false

Example:

class TestCondition{

   public static void main(String[] args){

	//declare a numeric variable
	int aNumber = 1;
	int anotherNumber = 7;

	//check if anotherNumber equals 10 and if it is
	//aNumber takes the value of 5
	//otherwise aNumber takes the value of 15
	aNumber = (anotherNumber==10) ? 5: 15;

	System.out.print("aNumber = " + aNumber);


   }
}

Output:

aNumber = 15

Other operators

Other than the ternary operator, there are other operators used when working with conditional logic as well as variables:

Operator Name Purpose
% Modulus Performs division of two numbers and returns the remainder
&& And Will return true if two or more conditions are true
|| Or Will return true if at least one of two conditions is true
++ Increment Will add one to a numeric variable
-- Decrement Will subtract one from a numeric variable

Practice

Online code editor
Practical examples
Practical exercises
Step-by-step tutorials

Reference

Terms glossary
Reference material

Rate this site

Rate this site
Visitor comments