Conditional Statements In JAVA

Conditional Statements In JAVA

In programming Conditional statements use when we have to decide one thing among two or more than two statements like if we have to decide either we have to add two no or multiply them then to solve that issue we use conditional statements .

Four most popular types of conditional statements in java are:

  • IF statement
  • IF else Statement
    •  Dangling Else Problem
  • Switch Statement
  • Ternary Operator

1. If Statement:

If statement check only one condition and if the condition is true then the body is executed otherwise nothing will happen.

Syntax:

if(condition){
     //body….
}

Example:

Output:

If we put a=6 and then check the condition the result is nothing.

2. IF Else Statement:

If else statement also has two types with one else and with multiple else in single else if the first if condition is wrong then else execute and if, If execute then it will skip the else part.

Syntax:

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

Example:

Output:

3. IF Else If Statement:

It have multiple else statement if, first if condition is false then check first its else if it’s also wrong then next and if all else if statements are wrong then execute last else statement.

Syntax:

if(condition){
//body here
} else if(condition){
//body here
}else if(condition){
//body here
}else{
//body here
}

Example:

Output:

  • Dangling Else Problem

     

    The Java compiler always link an if with the following else, the problem occurs when the scope is not defined and we put another statement after the first one, as compiler only runs the immediate following statement if the scope is not defined through the parenthesis.  this problem can also occur when we define anything outside the scope of if statement and there is an also following the if. this confuses the compiler, in the case of else for the related if statement.

    Example:

    If Scope is defined:

4. Switch Statement:

Switch statement consists of following keywords:

  • Switch: where the condition is labeled.
  • Case: it will be matched with labeled condition in the switch.
  • Break: it will break the code in some case so that other cases will not execute.
  • Default: if all the cases are wrong the default will run.

Syntax:

Switch(condition){
Case 1: {
//body here
Break;
}
Case 2: {
//body here
Break;
}
Case 3: {
//body here
Break;
}
Default:  {
//body here
Break;
}

}

Example:

Output:

If you enter day=10 for example, the result would be

5. Ternary Operator in Java:

The ternary operator is called ternary operator because it has three parts.

Syntax:

Ans here = (condition) ? if true : if false ;

Like take an example:

Then in above condition it return false and the value of x is false or 0.
Some more examples to define java ternary operator.

Example:

Output:

Hope you like this tutorial. Stay tuned for more upcoming tutorials. Stay Blessed!

Leave a Reply

Your email address will not be published. Required fields are marked *