If else statements without curly braces

  • It is also correct without braces, if there is only one statement under the if / else condition.
  • but if you have to put multiple statement under the if / else condition then you must provide braces, without braces if / else condition will only read first statement and output will be different from as you expected.

Scenario 1:

  • Here, We have single statement under the if else condition.
  • So, There is no error.
public class Practice6 {

	public static void main(String[] args) {
		int no = 3;

		if (no < 5)
			System.out.println("Line 1");

		else
			System.out.println("Line 2");

	}
}

Output:

Line 1


Scenario 2:

  • Here, we have two statement under the if & also have single statement under the else.
  • But, Compiler considered like:
    • Line 1 is If condition statement
    • Line 3 is else condition statement
    • Line 2 is outside of if else condition….
    • We mentioned Line 2 statement wrongly in between if else condition
    • So, Compiler throws an error.
public class Practice3 {

	public static void main(String[] args) {
		int no = 3;

		if (no < 5)
			System.out.println("Line 1");
		    System.out.println("Line 2");
		
		else
			System.out.println("Line 3");

	}
}

Output: Compilation error

error: Class names, ‘Practice3’, are only accepted if annotation processing is explicitly requested


Scenario 3:

  • Here, we have put two statements under if condition.
  • There is no another if or else conditions.
  • Compiler considered like:
    • Line 1 is if condition statement.
    • Line 2 is outside of if condition.
    • So compiler prints Line 2 statement also.
public class Practice6 {

	public static void main(String[] args) {
		int no = 3;

		if (no < 5)
			System.out.println("Line 1");
		    System.out.println("Line 2");
	}
}

Output:

Line 1
Line 2


Scenario 4:

  • Here, if condition have single statement & else condition have two statements.
  • There is no conditions after else.
  • So, Compiler thinks like
    • Line 2 is else condition statement.
    • Line 3 is outside of if else conditions.
    • So, it prints Line 3 statement also.
public class Practice6 {

	public static void main(String[] args) {
		int no = 6;

		if (no < 5)
			System.out.println("Line 1");
		
		else
			System.out.println("Line 2");
			System.out.println("Line 3");
	}
}

Output:

Line 2
Line 3


Scenario 5:

  • Here, if condition have single statement.
  • else if condition have two statements Line 2 & Line 3.
  • Continuously we have else condition also.
  • Compiler thinks like:
    • Line 2 is else if condition statement.
    • We have placed Line 3 Statement wrongly in between if else conditions.
    • So, it throws an error.
public class Practice3 {

	public static void main(String[] args) {
		int no = 6;

		if (no < 5)
			System.out.println("Line 1");
		
		else if(no > 10)
			System.out.println("Line 2");
			System.out.println("Line 3");
			
		else
			System.out.println("Line 4");
				
	}
}

Output: Compilation Error

error: Class names, ‘Practice3’, are only accepted if annotation processing is explicitly requested


Scenario 6:

  • Here, if & else if condition have single statement.
  • else condition have two statement. There is no more conditions after that.
  • Compiler considered the Line 4 statement is the outside of if else conditions.
  • So, compiler print Line 4 Statement.
public class Practice6 {

	public static void main(String[] args) {
		int no = 0;

		if (no < 5)
			System.out.println("Line 1");

		else if (no > 10)
			System.out.println("Line 2");

		else
			System.out.println("Line 3");
		    System.out.println("Line 4");
	}
}

Output:

Line 1
Line 4


NOTE:

Good coding practice is using braces for all conditional statements.


Leave a comment

Design a site like this with WordPress.com
Get started