Java: If Statements and Ternary Operator

Since the comparison operators’ concept has been discussed in a previous lesson, this lesson will now discuss how to use them to regulate any program’s flow. Firstly, the if statement will be discussed.

If Statement

The if statement is the most widely known and used control flow statements. It enables the assessment of a condition, and the final action is based on the assessment outcome.   

Let us look at the format of an if statement:

if (condition 1 is met) {
    do Task A 
else if (condition 2 is met) {
    do Task B 
else if (condition 3 is met) {
    do Task C 
else {
    do Task D 

In the first line, the first condition is being evaluated. In case the condition is met, the methods after line one that are enclosed within the curly brackets will be executed. The rest of the program will not even be checked by the compiler.   

In case the first condition is returned false, the subsequent else if conditions can be assessed, as they contain several conditions. A program can have several else if blocks.   

Lastly, if none of the previous conditions is met, the else block (lines 13 to 16) gets executed. Please note that an else if or even else blocks are not mandatory. A program can contain only an if statement, too, if there are no other conditions to assess. 

To understand the functionality of the if statement, let’s take a look at an example. Launch NetBeans and create a new project called exampleDemo. Substitute the current code generated with the following code.

import java.util.Scanner; 

public class ExampleDemo {
    public static void main(String[] arg) {
        Scanner input = new Scanner(; 
        System.out.print("\nPlease type the age: "); 
        int ageOfUser = input.nextInt();
        if (ageOfUser < 0 || ageOfUser > 100) {           
            System.out.println("Age is not valid"); 
            System.out.println("Please type a number between 0 and 100");
        else if (ageOfUser < 18) {         
            System.out.println("You are a minor"); 
        else if (ageOfUser < 21) {       
            System.out.println("Please seek parental consent"); 
        else {            
            System.out.println("You are eligible to register with us!");

Initially, user’s age is sought and when obtained, is stored in the variable ageOfUser. Then, the first condition is: 

if (ageOfUser 0 || ageOfUser 100)

Which assesses if the entered value is greater than 100 or lesser than 0. In case either of the conditions returns true, the curly brackets following the if the statement is executed (since the Or operator was used). The program displays “Age is not valid,” followed by “Please type a number between 0 and 100”.

If both the conditions in the ‘if statement’ are false, the next block of ‘else if condition’ (userAge < 18) is evaluated. In case the ageOfUser is found out to be less than 18 (However over 0 as the first condition of ‘if’ is proven to be false), the program will display “You are a minor”.

The statement that reads

System.out.println("You are a minor");

Has not been enclosed within curly braces, because if there is only one statement that has to be executed, curly braces are not required.  

In case the user entered a number between 18 and 21, the next else if block is executed. The problem will print, “Please seek parental consent.” 

Lastly, if the value that is entered is more than or equal to 21, but falls under hundred, the else block is executed, printing “Perfect” followed by “You are eligible to register with us!”

Now, the program can be run five times with different inputs: -30, 131, 13, 19, and 23. The outputs will be:

Please type in the age: -30
Age is not valid
Please type a number between 0 and 100
Please type in the age: 130
Age is not valid
Please type a number between 0 and 100
Please enter the age: 13
You are a minor
Please enter the age: 19 
Please seek parental consent 
Please enter the age: 23
You are eligible to register with us!

Ternary Operator

The ternary operator (?) is a very useful operator that simplifies the if statement, and helps assign a value to a variable based on the if condition.

The syntax is:

condition ? value to return if condition is true : value to return if condition is false;

For instance, the statement

7 > 4 ? 30 : 2;

Will return the value as 30 because 4 is lesser than 7 and the condition 7>4 is true. Programmers can directly assign this value to a variable too.  


int exampleIf = 7 > 4 ? 30 : 2;

exampleIf variable will now hold 30.

Leave a Reply

%d bloggers like this: