Selection Statements
Java uses all of C’s execution control statements, so if you’ve programmed with C or C++, then most of what you see will be familiar. Most procedural programming languages have some kind of control statements, and there is often overlap among languages. In Java, the keywords include if-else, while, do-while, for, return, break, and a selection statement called switch.
Java supports two selection statements: if and switch. These statements allow you to control the flow of your program’s execution based upon conditions known only during run time.
1.if-else
The if-else statement is the most basic way to control program flow. The else is optional, so you can use if in two forms:
if(Boolean-expression) statement
OR
if(Boolean-expression) statement else statement
Example :
public class ConditionalIf { public static void main(String[] args) { int i = 100; // simple if statement if (i == 100) { System.out.println("I is equal to 100"); } // if else statement if (i < 100) { System.out.println("I is equal to 100"); } else { System.out.println("I is not less than 100"); } } }
2. Nested – If’s
When two or more if or if else statements are occurred simultaneously, we call it nested if statements or nested if-else statements
public class nestedIFElse { public static void main(String[] args) { int a = 10; int b = 20; if (a == 10) { if (b == 20) // nested if block { System.out.println("a = 10 and b=20"); } else { System.out.println("a = 10 but b not equals 20"); } } } }
3. If – else Ladder
Sometimes there are situations where we want to handle many conditions with one if-else block. In these conditions we are using if-else ladder. Example is as :
public class IfElseLadder { public static void main(String[] args) { int grade = 75; if (grade > 70) System.out.println("Excellent grades"); else if (grade > 60 && grade < 70) System.out.println("Good Grades"); else if (grade > 50 && grade < 60) System.out.println("Average Grades"); else if (grade > 40 && grade < 50) System.out.println("poor Grades"); else if (grade <= 40) System.out.println("Failed"); else System.out.println("You are alien"); // will executed if no // statement above matches } }
4. switch
The switch is sometimes called a selection statement. The switch statement selects from among pieces of code based on the value of an integral expression.The switch expression can be of type char, byte, short, or int. Float and String types are not permitted with Switch. Here is an example to demonstrate switch.
public class SwitchExample { public static void main(String[] args) { for (int i = 0; i < 5; i++) { switch (i) { case 0: System.out.println("i is 0"); break; // break is used to break the statement otherwise // next statement will be executed case 1: System.out.println("i is 1"); break; case 2: System.out.println("i is 2"); break; case 3: System.out.println("i is 3"); break; case 4: System.out.println("i is 4"); break; default: // used to handle when no case matches, Not mandatory at // all System.out.println("No case matched"); break; } } } }