Selection statements in Java

Java provides selection statements, such statements can be used where you have to choose actions with two or more then two alternative options. Such statements enrich to take frequent decision based on particular situation. The pseducode of if-else structure is as follows.

if (boolean Expression){  
         statements; 
}
else {  
         statements; 
}

Flow chart for if-else structure is given bellow. If boolean expression is evaluated to true the set of statement will be executed from if body otherwise else part will be executed. if

if(grades>=60)
        System.out.println("Pass");
else
        System.out.println("Fail");

if the current value in variable grade is 80. Boolean expression evaluates the result to true and if part will be in action. On console you will get the result Pass because 80>60 and the boolean expression is correct. Note : no need to place brackets if there is a single statement otherwise add braces to avoid logical errors.

Nested if-else Statements

One can write another if or if-else statement inside  if or if-else structure. such statements are known as nested statements. infact inner if can contain another if or if-else  statement and there is no limit to the depth of nesting.
nested if

switch Statements

Frequently use of nested if-else structure make program difficult to read and understand. Java provides an alternative efficient approach in form of switch statement to overcome such issues. The pseducode of switch statement is as follow.

switch( switch expression) {

case 0: statement ;

case 1: statement;

default: statement;

}
  • The switch-expression must provide a value such as char, short, int, double or byte and must enclosed in parentheses.
  • Once the value of switch expression match the case, it is executed until either a break statement or till end of switch statement.
  • The keyword break is used as optiona to terminate the statement immediately.
  • The default case of switch statement is also optional, can be used when no cases mathc the switch expression.
  • Good programming style is to maintain the logical squence of the case and place the default case at the end.

Switch statement Switch statement2

Example : The output of the following program is “Dec”. Because of month =12 matches with case 12 where monthName is assigned Dec.

int month = 12;
String monthName;
switch (month) {
case 1: monthName = "Jan";
break;
case 2: monthName = "Feb";
break;
case 3: monthName = "Mar";
break;
case 4: monthName = "Apr";
break;
case 5: monthName = "May";
break;
case 6: monthName = "Jun";
break;
case 7: monthName = "Jul";
break;
case 8: monthName = "Aug";
break;
case 9: monthName = "Sep";
break;
case 10: monthName = "Oct";
break;
case 11: monthSName = "Nov";
break;
case 12: monthName = "Dec";
break;
default: monthString = "Invalid input";
break;
}
System.out.println(monthName);

Conditional Expression or Ternary Operator

Ternary operator can be used as an alternative to if-else statement. Where you want to assign a value to a variable that have specific constraints. The phseducode for such expression is given bellow.

booleanExpression ? expression1 : expression2;

Example :

String status = (grade>=60) ? "Pass" : "Fail"; 

No Responses

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.