Java If Else Statement
This is a conditional statement, That are used to check an expression result. generally all the expression are produce boolean values and if-statement are work on this boolean value when result is true then it will execute if-block of statement otherwise they are check other condition or execute else statement.
This condition statement are change the execution flow of program, and suitable to take dynamic action. Because selection is always base of true value otherwise else that are not working.
If Statements
That is simple and selection based statement that is depending upon given expression condition. Generally most of statements are based on condition, because condition is decide the which statements allowed to the current execution. if-statement are defined by given syntax.
if(expression){
//when expression is true
}
If is a keyword, Which are used to check an expression result. And expression are defining within the parentheses. And conditional based statements defines of within curly braces. for example.
//If statement
class Conditional{
public static void main(String args[]){
int errorStatus=0; //integer variable
//expression
if(errorStatus==0){
//when expression is produce true result
System.out.println("Nice code all test case is passed ");
}
}
}
Output
Nice code all test case is passed
Note that in expression in form of integer but their result are produce in boolean value. So if statement are work on this boolean value.

In this case given expression is produced the true result. So what happening when not satisfied if expression result and that in form of boolean false. In this situation if-block are not execute this block of statements. See this example.
//If statement
class Conditional{
public static void main(String args[]){
int errorStatus=10; //integer variable
//expression
if(errorStatus==0){
//when expression is produce true result
System.out.println("Nice code all test case is passed ");
}
}
}
Note that error Status not equal to zero. Then in this program there are no other statement so this program are not produce any result. In good programming practices should your code is producing at least one output. So in this case we can use else-block.
If-else statement
else-statement is an optional block of if-statement, that are work when if statement are not work then else-block are work.
if(condition){
//statements
}else{
//statements
}
Both are conditional block but we cannot execute if-statement and relative else block statement in same time. And else-statement is totally based on if-statement. For example.
//If-else statement example
class Conditional{
public static void main(String args[]){
int javaBook=0;
if(javaBook>=1){
//good luck, book is available
System.out.println("Book are available");
}else{
//when book are not available
System.out.println("Sorry, book are not available of this time.");
}
}
}
Output
Sorry, book are not available of this time.
In this example program are produce an output. That are execute else-block of this case. But assume that if javaBook are not available in this type so you can choose book of other programming language. How can manage this situation programatically. let see
//If-else statement example
class Conditional{
public static void main(String args[]){
int javaBook=0;
int kotlinBook=10;
if(javaBook > 0){
System.out.println("Java Book Available");
}else if(kotlinBook > 0){
System.out.println("Kotlin Book Available");
}else{
System.out.println(" Java and Kotlin book are not Available");
}
}
}
Output
Kotlin Book Available
Note that in this example are include variant of if-else statement. That is similar to if-statement. And this are work when previous if-condition are not worked. We can implements series of if-else statement in following way.
if(expression1){
//statements
}else if(expression2){
//statements
}
else if(expression3){
//statements
}else if(expression4){
//statements
}else if(expression5){
//statements
}else{
//statements
}
There are no limit to use combination of if-else statement. and inside of if-else block can defined any number of statement. Note the expression is any condition we cannot use unconditional expression inside this statements. See this example.
//If-else with unconditional statement
class Conditional{
public static void main(String args[]){
if(1){
System.out.println("True");
}else{
System.out.println("False");
}
}
}
Error
Conditional.java:7: error: incompatible types: int cannot be converted to boolean
if(1){
^
1 error
In this example 1 is unconditional that are not compare to any value and not in boolean value. so unconditional statement are not valid when data is not in boolean form.
Nested if-else statements
In above all example is simplest form of if-else statement. We can use one conditional statement inside another conditional statement. That is related to nested behaviour of conditional statement. This post is related to if-else statement, Normally other conditional statement also allowed to define and use nested statement. That is depends on situation and work flow.
In nested if-else statement there are possible to define different structure (execution flow) and possible of used to different way. Here given a basic structure of nested statement.
if(expression1){
//Statement
if(expression1){
//Statement
if(expression1){
//Statement
}else if(expression1){
//Statement
}else{
//Statement
}
}else{
//Statement
}
//Statement
}else{
//Statement
}
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment