Java Conditional Statements - The Coding Shala

Home >> Learn Java >> Java Conditional Statement

Java's Conditional Statements

Java Supports two conditional or selection statements: if and switch. These statements are used to control the flow to the program based on the condition that we have put. 
Java Conditional Statements - The Coding Shala

Java If else Statement

The basic statement of Java's If the statement is as follows: 

if(condition){
//code
}
else{
//code
}

or

if(condition1 operators condition 2){ //code
 }

OR

if(codition1){ 
//code
}
else if(condition2){ //code 
}
else{ //code 
}


The condition is an expression returns a boolean value. The else part is optional. 

Note: we can also use nested if statements. (if inside if statement)

The following Java program explains the if statement: 
class IfExample{
    public static void main(String[] args){
        int a = 10;
        int b = 20;
        
        if(a<b){
            System.out.println("a is less than b");
        }else{
            System.out.println("a is greater than b");
        }
        
        boolean aa = true;
        if(aa){
            System.out.println("Value of aa is true");
        }
        
        if(aa){ //nested if statement
            if(a<6){
                System.out.println("Value of a is less than 6 and a is: " + a);
            } else if(a<11){
                System.out.println("Value of a is less than 11 and a is:  " + a);
            }
        }
    }
}



<<<Output of this program is >>>>>>>>

a is less than b
Value of aa is true
Value of a is less than 11 and a is:  10

Java switch Statement

The basic syntax of Java's switch statement is as follows: 

switch(expression) {
  case value1:
          //code
           break;
  case value2:
               //code
               break;
  .
  .
  .
  case valueN:
          //code
          break;
   default:
      //code
}

Here, expression and value specified in the case statement must be type compatible and can be a byte, short, int, or char. The default statement is executed when none of the case value matched with the value of the expression. The default statement is optional. The break statement is used to terminate a statement sequence that means used to jump out of the switch.

Note: we can also use nested switch statements.

The following Java Program explains the switch statement: 

class SwitchExample{
    public static void main(String [] args){
        int num = 4;
        switch(num) {
            case 0:
                System.out.println("Value of num is 0");
                break;
            case 1:
                System.out.println("Value of num is 1");
                break;
            case 4:
                System.out.println("Value of num is 4");
                break;
            default:
                System.out.println("Default statement");
        }
    }
}



<<<<Output of this program is >>>>
Value of num is 4



Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to find sum, product and average of given numbers - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Richest Customer Wealth LeetCode Solution - The Coding Shala

New Year Chaos Solution - The Coding Shala

Add two numbers in Scala - The Coding Shala