Java – Conditional Statements

We would consider if statements in this lesson. Then we would also look at switch statement. Finally, we would examine the conditional operator.

So Java provides decision-making constructs.

  1. if Statement.
  2. If..else Statement.
  3. Nested if Statement.
  4. switch Statement
  5. Conditional if Operator (? : )

 

1. If Statement

Almost every program needs to make decisions at some point. We use if statement for making decisions. The if statement consist of a conditional statement. Then block of one or more statements enclosed in curly braces.

 

The structure is given below:

     if(condition) {
   	//body of if statement
     }
     // other codes

 

You can see the flow diagram for the if statement in Figure 1.0.

Diagram for the if statement in java

Figure 1.0: Flow Diagram for the if Statement

Let’s write a program to illustrate the if statement. The program would decide if we should take an umbrella or not. So the condition would be the weather. If the weather is rainy, then we take an umbrella.

You can run copy and run the program yourself.

public class Tester {

   public static void main(String args[]) {
	    String weather = "rainy";
	    
	    if(weather == "rainy") {
	    	System.out.println("Take and umbrella");
	    }
   } 
}

Listing 1.0: Program to illustrate the if statement

 

 

2. if…else Statement

Next, lets’ consider the if…else statement. Unlike the if statement, the if…else statement has two parts: the if part and the else part.

  • The if part contains the condition. Then followed by code to execute if the condition is true.
  • The else part contains code to execute if condition is false

 

I present the syntax in below.

	if(condition) {
	 //code to execute if condition is false
	}
	else {
	 //code to execute if condition is false
        }

 

Now, also look at the flow diagram for the if..else statement. Notice that it has two blocks of statements. This is shown in Figure 1.1 below

Diagram for if...else Statement in Java

Figure 1.1: Diagram for if…else Statement in Java

 

Let’s write a program to illustrate the if…else statement. So the program would have two parts. First code for if weather is sunny. Then code for if weather is rainy. For rainy weather, we say ‘take an umbrella’. Similarly, for sunny weather, we say ‘wear your sunglasses’.

 

You can observe the code in Listing 1.0.

public class Tester {

   public static void main(String args[]) {
	    String weather = "rainy";
	    
	    if(weather == "rainy") {
	    	System.out.println("Take and umbrella");
	    }
	    else if(weather =="sunny") {
	    	System.out.println("Wear your sunglasses")
	    }
   } 
}

Listing 1.1: Code to Illustate if…else Statement

 

3. Nested if Statement

Sometimes, you may want to have an if statement inside an if statement. As such, you want to test two conditions. Take for example, you want to find Form 6 students that score above 70 in a test. You can use the code below to achieve that.

 

public class Tester {

   public static void main(String args[]) {
	    double score = 76;
	    int form = 6;
	    
	    if(form == 6) {
	    	System.out.println("He is in form 6");
	    	if(score > 70) {
	    		System.out.println("His score is also above 70");
	    	}
	    }
   } 
}

Listing 1.2: Program to Illustrate Nested if Statements

 

4. switch Statement

I’m sure you will find this interesting. Because switch statement makes for a more readable code. Its a better alternative to if statements sometimes.

 

So I present the syntax below:

   switch(condition) {
   	case value1:
   		//statement
   		break
   	case value2:
   		//statement
   		break
   		...
   		...
   	case default:
   		//default statment	   
   }

Listing 1.3: Syntax for the Switch statement

 

Rules for the switch Statement

Finally on the switch statement, here are the rules that apply:

  • values must be either integer, string or enum
  • Case statement if followed by a colon while statements comes under
  • Also, break statement comes after all case blocks
  • The default statement is optional

 

Now, let’s write  a program to prepare a grade comment using switch statement. It is going to work this way:

  • Grade A: Comment = “Excellent”
  • Grade B: Comment = “Good”
  • But Grade C: Comment = “Fair”
  • Then grade D: Comment = “Pass”
  • Finally for grade E: Comment = “Poor”

 

I have written the code for you in Listing 1.4. I therefore recommend you make sure to try it yourself.

public class Tester {

   public static void main(String args[]) {
   char grade = 'C';  
   switch(grade) {
   case 'A':
	  System.out.println("Excellent");
	  break;
   case 'B':
	  System.out.println("Good");
	  break;   
   case 'C':
	  System.out.println("Fair");
	  break;   
   case 'D':
	  System.out.println("Pass");
	  break;   
   case 'E':
	  System.out.println("Poor");
	  break;   
   default:
	   System.out.println("Not a valid grade");		  
   }
	   
   } 
}

Listing 1.4: Switch Statement

 

5. The Conditional Operator (? : )

You can call this a one line if-statement. Although it has been covered in under Basic Java Operators, we want to give a review.

 

This operator is of the form:

	 int a = 10;
	 int x = a>20 ? 10 : 20; 

 

What is means is  this:

  • if a is less than 20, then x =10
  • Otherwise x = 20