If then allow us to control the flow of program based on conditions, whether to execute a code or not. In simple word it gives the freedom to take decisions on what code to execute or not. The biggest advantage of if then is that it makes the program to take action based on user input.
If then execute a code of block or single statement if a Boolean expression inside braces is true. The basic syntax of if then is as follows:
if(Boolean expression is true) Execute this single statement;
Boolean expression is enclosed in parenthesis and single statement is terminated with semicolon.
Or
if(Boolean expression is true){ Execute all statement inside code block; }
If you are using code block then each statement inside block should also be terminated with semicolon.
Example of If then:
Lets create a simple program when water evaporate if temperature is 100 degree Celsius:
int tempWater = 100; if(tempWater==100) System.out.println(“Water start evaporating”);
In the above example, since if
Boolean expression is true, so single statement just below will be executed. Thus output will be Water start evaporating.
Lets see same example with different input:
int tempWater = 50; if(tempWater==100) System.out.println("Water start evaporating"); System.out.println("This Will Be Executed");
In the above example, since the if Boolean expression is false so single statement just below if won’t be executed. But the next statement just after it will be executed. Thus the output will be:
This Will Be Executed
Important Note: It is important to understand without using code block only single statement after if is executed, if Boolean expression is evaluated to be true. Otherwise it will jump to the next statement.
Lets see same example with block code:
int tempWater = 100; if(tempWater==100){ System.out.println("Water start evaporating"); System.out.println("This will also be executed"); }
In the above example, since if Boolean expression is true so both statement inside code block will be executed. So the output will be:
Water start evaporating This will also be executed
Important Note: Using code block is meant to use when you want to execute more than single statement. But we recommend to use code block even if you are using single statement because it comes in good programming practice.
What if you want to execute some code if the if condition evaluates to false, that’s when you need if then else in JAVA. The else statement says to execute the single statement or code of block if the if statement evaluates to false.
Example of if then else:
Lets a create a simple program to test whether x number is even or not:
int x = 19; if(x%2==0){ System.out.println("x is even number"); } Else{ System.out.println("x is not an even number"); }
In the above example, since the if boolean expression evaluates to false, so the else statement will be executed. Thus the output will be:
x is not an even number
Sometime we have more than two conditions to evaluate, for such situations we use “else if” statement following an if statement and its body. We can use as many as else if ensuring only one code block is executed.
Lets create a program to find out largest of three number us else if:
int x = 10; int y = 20; int z = 30; If(x>=y && x>=z){ System.out.println("x is the greatest number"); else if(y>=x && y>=z){ System.out.println("y is the greatest number"); } else{ System.out.println("z is the greatest number"); }
In the above example, first if expression will be evaluated to false. After that else if expression will also be evaluated to false. And the third else statement will be executed.
So the output will be:
z is the greatest number
This topic is already discussed in conditional operators but we want to repeat it once again as now it will be easier to understand because it is a shorter syntax of if then else.
Ternary Operator takes three operand in which first one is boolean expression, second and third operands are values. It is called ternary because it accepts three operands.
In Ternary Operator, if the first operand which is boolean expression is true, then the value of second operand is return otherwise value of third operand is return. In other words, if boolean expression is false, the value of third operand will be return otherwise second operand will return.
Example of Ternary Operator:
int x = 10; (x%2 ==0): "Even" : "Not even";
In the above example since the Boolean expression evaluate to true so the value of second operand will be return. So the output will be:
Even
Premium Project Source Code:
Leave a Reply