A control statement changes the flow of execution of a program. In Java, these statements are categorized in the selection, iteration and jump statements. A selection statement changes the flow by selecting different paths of execution based on the logical decision. Iteration statement runs a specific block repeatedly. Lastly, a jump statement forces a program execution to stop to follow the linear flow of the program.
Java has If and Switch statement as selection statements. These statements control the execution of a program with the help of conditions which are evaluated at runtime.
-If
It enables a program to execute a particular block if and only if the given condition is true.
Here’s the syntax:
if(expression) { //body }
-If-else
If the condition of If-statement is true then its execution takes place, otherwise, the block of else-statement would be executed.
if(expression){ //body } else{ //body }
-Nested if
It is said to be nested if when If-statement is followed with another If or else statement.
if(expression){ if(expression){ //body } }
public class IfStatement { public static void main(String[] args) { int value=5; // If statement if(value==5) { System.out.println(" This is the block of If Statement "); } // If-else statement if(value>=10) /// 5>=10, which is false { System.out.println(" This is the block of Statement "); } else { System.out.println(" This is the block of else Statement "); } // nested-if statement if(value==5) { if(value<=10) {System.out.println(" This is the block of Nested-If Statement "); } } } }
Output:
Despite using Nested if which makes the code more complex and lengthy, we can use a switch statement. A switch statement allows the execution of different parts of code based on the value of an expression. It works by evaluating an expression and if it matches any of the cases then code paired with that case would be executed. Each and every case must be different and in addition, Java allows a switch statement to use the expression of the type of sting and an integer type. A break statement is put at the end of each of the case to break the statement sequence. Lastly, default is optional and executed when all the cases aren’t matched.
If break and default statement isn’t there in the program
public class SwitchStatement { public static void main(String[] args) { int i=2; switch(i){ case 1: System.out.println("I am one "); case 2: System.out.println("I am two "); case 3: System.out.println("I am three "); } } }
Output:
Since no break statement was declared in the above example. The value of “i ” was 2, which clearly matched with case 2. Resulting in, execution of 2nd case where break statement was missing, with successive execution of the 3rd case. If there were more cases then that would also be executed as sequence didn’t abrupt by any break statement.
public class SwitchStatement { public static void main(String[] args) { int i=2; switch(i){ case 1: System.out.println("I am one "); break; case 2: System.out.println("I am two "); break; case 3: System.out.println("I am three "); break; default: System.out.println("nothing is matched "); } } }
Output:
This post was last modified on September 14, 2019