Saturday, January 23, 2021

Java if else | Ternary operator java | Conditional operator in java-AapKiApniSchool

 In this blog i will explain you how to use java if else. We will change if else to ternary opertaor java.


Java if else | Ternary operator java | Conditional operator in java | Conditional statements in java


Java if else is used to check boolean condition true or false. There are diffent types of if else statements

1. if statement
2. if else statement
3. nested if else
4. if else if


Java if Statement:

Java if statement execute if the given condition is true.

Syntax:

if(condition)
{
code will be executed
}

Java if else statement:

In Java if else statement code written inside if block will be executed if given condition is true.Otherwise else block code will be executed.

if(Condition)
{
 //This code will be executed if condition is true
}
else{
  //This code will be executed if condition is false
}


Java nested if else:


In Java nested if else will be executed if outer if condition is true.

if(condition)
{
   if(Condition)
{
   //This code will be executed if outer and inner conditions are true.

}
else{
 //This code will be executed if outer if condition is true aand inner conditions is false.
}
}
else{

}


Java if-else-if:

In Java if else if statement first if condition is checked if first if condition is false then else if condition will be checked if else if condition is true then else if block will be executed otherwise else block will be executed.

if(Condition1)
{}
else if(Condition2)
{}
else{
}


Ternary operator java:

We can convert if else statement to ternary operator statement.

if else statement:

if(2<5)
{
  return true;
}
else{
 return false;
}

Ternary Operator:

boolean x=2<5?true:false;

2 comments:

Featured Post

Interface in java-AapKiApniSchool

Interface in java:  interface is a blueprint of class in java that is used to define contracts. It can contain static constant and abstract ...

Popular Posts