Tag Archives: if-else-if

Java : Control Flow Statements


Statements in java source code generally executed from top to bottom, in the order they appear. However, with control-flow statements, that order can be interrupted to implement decision making, branching or looping so that the Java program can run particular blocks of code based on certain conditions. Control flow statements categorize as below:

See Also: Type of Statements in Java

Java : if-else-if Statement


The if-else-if statement is the decision-making statement, Use If the if-statement conditional statement execution result is false then control will reach to check conditions for an else-if statement. If the result is true, block associated with else-if statement will execute otherwise control will reach to else block. We can add multiple else-if statements consecutively.

Syntax:

if(test_expression)
{
   //execute your code
}
else if(test_expression n)
{
   //execute your code
}
else
{
   //execute your code
}

Note :
An if-else-if statement else is an optional clause. Below is the format without the else clause:

if (test-expression)
{
statement
}
else if (test-expression)
{
statement
}

See Also :

if-else-if Statement Example

public class IfStatementTest{

   public static void main(String args[]){
      int a=20;

      if(a>0){
         System.out.println("a is positive number");
      }
	  if(a<0){
         System.out.println("a is negative number");
      }
	  else{
         System.out.println("a is zero");
      }
}

Output


a is positive number

Note :
The if-else-if statement can be a simple statement terminated by a semicolon or a block enclosed in curly braces.
For Example, the Below code is equivalent to the same as the above example and returns the same result.

public class IfElseIf{

   public static void main(String args[]){
      int a=20;

      if(a>0)
         System.out.println("a is positive number");
      else if(a<0)
         System.out.println("a is negative number or zero");
      else
         System.out.println("a is zero");

}

Recommendation :

  • For a better understanding of code always use curly braces ({}) with if, if-else-if or if-else statements.
  • It always creates confusion when more than one statement is in code.

Java: Switch Case Statement


The switch statement is a decision-making statement, also called a multi-way branch statement. It provides an easy way to jump execution to different parts of code based on the case value match with expression. This expression result allowed type as byte, short, char, and int primitive data types.

Note: After java 7+, it also allowed to work with enumerated types ( Enums in java), the String class and Wrapper classes.

Points to remember about Switch-Case statements

  • The value data type for a case must be the same data type as the variable in the switch.
  • The value for a case must be a constant or a literal.
    The value for the case, Variables are not allowed.
  • Duplicate case values are not allowed.
  • The break statement (optional) is used inside the switch to terminate a statement sequence.
  • If break statment is omitted, execution will continue on into the next case.
  • The default statement (optional) can appear anywhere inside the switch block. In case, if it is placed not at the end, then a break statement must be kept after the default statement to omit the execution of the next case statement.

See Also :

Syntax:

switch(variable)
{
case 1:
   //execute your code
break;

case n:
   //execute your code
break;

default:
   //execute your code
break;
}

Note:

  • The switch statement is fast when compared with the case of multiple else if because it directly jumps to match condition.

Switch-Case Example

ublic class SwitchCaseTest {

	public static void main(String[] args){
		        int month = 5;
		        String monthString; 

		        // switch statement with int data type
		        switch (month) {
		        case 1:
		        	monthString = "January";
		            break;
		        case 2:
		        	monthString = "February";
		            break;
		        case 3:
		        	monthString = "March";
		            break;
		        case 4:
		        	monthString = "April";
		            break;
		        case 5:
		        	monthString = "May";
		            break;
		        case 6:
		        	monthString = "June";
		            break;
		        case 7:
		        	monthString = "July";
		            break;
		        case 8:
		        	monthString = "August";
		            break;
		        case 9:
		        	monthString = "September";
		            break;
		        case 10:
		        	monthString = "October";
		            break;
		        case 11:
		        	monthString = "November";
		            break;
		        case 12:
		        	monthString = "December";
		            break;
		        default:
		        	monthString = "Invalid Month";
		            break;
		        }
		        System.out.println("Selected Month "+month+" is "+monthString);
		    }
		}

Output


Selected Month 5 is May