X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

Decision Making in Java :

There are two types of decision making statements in Java. They are :-

  • if statement




  • switch statement



if statement :

An if statement consists of a boolean expression (returns true or false) followed by one or more statements.

Syntax : if(boolean_expression) { //Statements will execute if the boolean expression is true }

Example :

class Example { public static void main(String [ ] args) { int a = 5; int b = 10; if(b > a) { System.out.println(b); } } }

Output :

Here, the expression is true therefore, it will output as the value of 'b' i.e 10.

Curly braces are optional and without curly braces we can take only one statement and which should not be declarative statement.

if(true)
System.out.println("Algo"); // valid
if(true)
int a = 10; // invalid
if(true)
{
int a = 10;
} // valid
if(true); // valid

if...else statement :

An if statement can be followed by an optional else statement, which executes when the boolean expression is false.

Syntax : if(boolean_expression) { //Statements will execute if the boolean expression is true } else { //Statements will execute if the boolean expression is false }

Example :

class Example { public static void main(String [ ] args) { int a = 5; int b = 10; if(b < a) { System.out.println(b); } else { System.out.println(a); } } }

Output :

Here, the expression is false therefore, it will output as the value of 'a' i.e 5.

The argument to the if statement should be boolean type. If we are providing any other type then we will get compile time error.

Example 1 :

class Example { public static void main(String [ ] args) { int a = 0; if(a) { System.out.println("Algo"); } else { System.out.println("Valley"); } } }

Output :

Compile time error : incompatible types required : boolean found : int

Example 2 :

class Example { public static void main(String [ ] args) { int a = 10; if(a = 20) { System.out.println("Algo"); } else { System.out.println("Valley"); } } }

Output :

Compile time error : incompatible types required : boolean found : int

Example 3 :

class Example { public static void main(String [ ] args) { int a = 10; if(a == 20) { System.out.println("Algo"); } else { System.out.println("Valley"); } } }

Output :

Valley

Example 4 :

class Example { public static void main(String [ ] args) { boolean b = false; if(b = true) { System.out.println("Algo"); } else { System.out.println("Valley"); } } }

Output :

Algo

Example 5 :

class Example { public static void main(String [ ] args) { boolean b = false; if(b == true) { System.out.println("Algo"); } else { System.out.println("Valley"); } } }

Output :

Valley

if...else if.....else statement :

An if statement can be followed by an optional else if statement , which is very useful to test various conditions using single if...else statement.

Syntax : if(boolean_expression1) { //Statements will execute if the boolean expression1 is true } else if(boolean_expression2) { //Statements will execute if the boolean expression2 is true } else if(boolean_expression3) { //Statements will execute if the boolean expression3 is true } else { //Statements will execute if the boolean expressions are false }

Example :

class Example { public static void main(String [ ] args) { int a = 5; int b = 10; if(b > a) { System.out.println(b); } else if(a > b) { System.out.println(a); } } }

Output :

Here, the expression is true therefore, it will output as the value of 'b' i.e 10.

Nested if....else statement :

It is always legal to nest if-else statements which means we can use one if or else if statement inside another if or else if statement.

Syntax : if(boolean_expression1) { //statement will execute when the boolean expression1 is true if(boolean_expression2) { //Statement will execute when the boolean_expression2 is true } }

Example :

class Example { public static void main(String [ ] args) { int a = 5; int b = 10; if(a == 5) { if(b == 10) { System.out.println("a = 5 and b = 10"); } } } }

Output :

Here, output will be a = 5 and b = 10 because both boolean expressions are true.


X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.