Java - Conditional Statements

If Statement

  • It tells your program to execute a certain section of code only if a particular test evaluates to true.
  • If it evaluates to false, the statements in the if block are not executed.

1. Syntax : If Statement

							  
if (booleanExpression) {
    statement(s)
}

							  

Example :

							  
class IfDemo {
    public static void main(String[] args) {

        int marks = 76;
        String grade;

        if (marks >= 40) {
            grade = "Pass";
        }

        if (marks < 40) {
            grade = "Fail";
        }

        System.out.println("Grade = " + grade);
    }
}		  

							  

Output : Grade = Pass


2. Syntax : If-Else Statement

							  
if (booleanExpression) {
    statement(s)
} else {
    statement(s)
}
							  
							  

Example :

							  
class IfElseDemo {
    public static void main(String[] args) {

        int marks = 76;
        String grade;

        if (marks >= 40) {
            grade = "Pass";
        }
        else
        {
            grade = "Fail";
        }

        System.out.println("Grade = " + grade);
    }
}
							  
							  

Output : Grade = Pass


3. Syntax : Else-If Ladder Statement

							  
if (booleanExpression1) {
    // statements
} else if (booleanExpression2) {
    // statements
}
...
else {
     // statements
}

							  

Example :

							  
class ElseIfLadderDemo {
    public static void main(String[] args) {

        int marks = 76;
        char grade;

        if (marks >= 90) {
            grade = 'A';
        } else if (marks >= 80) {
            grade = 'B';
        } else if (marks >= 70) {
            grade = 'C';
        } else if (marks >= 60) {
            grade = 'D';
        } else {
            grade = 'F';
        }
        System.out.println("Grade = " + grade);
    }
}
					
							  

Output : Grade = Pass


Switch Case

  • It is alternative to else-if ladder.
  • Switch allows you to choose a block of statements to run from a selection of code, based on the return value of an expression.

Syntax : Switch Case

							  
switch (expression) {
case value_1 :
     statement(s);
     break;
case value_2 :
     statement(s);
     break;
 .
 .
 .
case value_n :
     statement(s);
     break;
default:
     statement(s);
}
							  
							  

Example :

							  
class SwitchDate{
  public static void main(String[] args){
  int week = 3;

  switch (week){
      case 1:
            System.out.println("monday");
            break;
      case 2:
            System.out.println("tuesday");
            break;
      case 3:
            System.out.println("wednesday");
            break;
      case 4:
            System.out.println("thursday");
            break;
      case 5:
            System.out.println("friday");
            break;
      case 6:
            System.out.println("saturday");
            break;
      case 7:
            System.out.println("sunday");
            break;
      default:
            System.out.println("Invalid week");
            break;
      }
  }
}

							  

Output : wednesday


BytecodeTechnosolutions.com | Raju