Java has several types of selection statements: simple if statements, if .. else statements, nested if statements, switch statements, and conditional expressions.
A simple if statement executes an action if and only if the condition is TRUE. The syntax for a simple if statement:
if (booleanExpression) {
statement(s);
}
If the booleanExpression evaluates to TRUE, the statements in the block are executed.
if (radius >=0) {
area = radius * radius * PI;
System.out.println("The area for the circle of radius " + radius + " is " + area);
}
In the above example, if the value of radius is greater than or equal to 0, then the area is computed and the result is displayed; otherwise, the two statements in the block will not be executed.
The booleanExpression is enclosed in parentheses ().
if ((i>0) && (i<10)) {
System.out.println("i is an integer between 0 and 10 ");
}
The braces {} can be omitted if they enclose a single statement. For example:
if ((i>0) && (i<10))
System.out.println("i is an integer between 0 and 10 ");
If there is no statement, just put an empty braces, such as:
if (radius >= 0) { };
A simple if statement takes an action if the specified condition is TRUE. If the condition is FALSE, nothing is done.
The actions that an if .. else statement specifies differ based on whether the condition is true or false.
The syntax for this type of statement is:
if (booleanExpression) {
statements-for-the-true-case;
}
else {
statements-for-the-false-case;
}
If the booleanExpression evaluates to TRUE, the statements for the true case are executed; otherwise the statements for the false case are executed.
The following code segment is to identify either the input is an odd or even number.
int n = sc.nextInt();
if (n %2 == 0)
System.out.println(n + " is an even number");
else
System.out.println(n + " is an odd number");
The output will be:
5
5 is an odd number
The following code segment is to identify either the input number is positive or negative number.
int n = sc.nextInt();
if (n>0)
System.out.println(n + " is positive number.");
else if (n<0)
System.out.println(n + " is negative number.");
So, if the input is number 5, then the output will be:
5
5 is positive number
What is the output if the input is number 0?
The inner if statement is said to be nested inside the outer if statement. The inner if statement can contain another if statement; in fact, there is no limit to the depth of the nesting.
if (i>k) { // outer if statement
if (j>k) // inner if statement
System.out.println("i and j are greater than k");
}
else
System.out.println("i is less than or equal than k");
The if (j>k) statement is nested inside the if (i>k) statement.
The nested if statement can be used to implement multiple alternatives.
For instance:
double score = 0;
char grade;
if (score >= 90.0)
grade = 'A';
else
if (score >= 80.0)
grade = 'B';
else
if (score >= 70.0)
grade = 'C';
else
if (score >= 60.0)
grade = 'D';
else
grade = 'F';
but .. it is better write this way (to avoids deep indentation and makes the program easy to read):
double score = 0;
char grade;
if (score >= 90.0)
grade = 'A';
else if (score >= 80.0)
grade = 'B';
else if (score >= 70.0)
grade = 'C';
else if (score >= 60.0)
grade = 'D';
else
grade = 'F';
However, for the following case:
if (i>j)
if (i>k)
System.out.println("Case A");
else
System.out.println("Case B"); }
is better write like this (with correct indentation) because the else clause matches the most recent if clause:
if (i>j) {
if (i>k)
System.out.println("Case A");
}
else
System.out.println("Case B"); }
or, otherwise, we have to arrange like this:
if (i>j)
if (i>k)
System.out.println("Case A");
else
System.out.println("Case B");
because the 'else' statement is paired with the nearest 'if' statement.
Overuse of nested if statements makes a program difficult to read. Java provides a switch statement to handle multiple conditions efficiently.
if (marks >= 90.0)
grade = 'A';
else if (marks >= 80.0)
grade = 'B';
else if (marks >= 70.0)
grade = 'C';
else if (marks >= 60.0)
grade = 'D';
else
grade = 'F';
The above statements can be rewrite using switch statements. Since the switch-expression cannot in double, so we need to do a little bit adjustment.
switch (score){
case 90 .. 100: grade = 'A';
break;
case 80 .. 89: grade = 'B';
break;
case 70 .. 79: grade = 'C';
break;
case 60 .. 69: grade = 'D';
break;
case 0 .. 59: grade = 'F';
default: System.exit(0);
}
The full syntax for switch statement:
switch (switch-expression) {
case value1: statements1;
break;
case value2: statements2;
break;
...
case valueN: statementsN;
break;
default: statements-for-default;
}
the switch-expression must yield a value of char, byte, short or int type and must always be enclosed in parentheses.
the value1, ..., and valueN must have the same data type as the value of the switch-expression. Note that value1, ..., and valueN are constant expressions, meaning that they cannot contain variables such as 1 + x.
when the value in a case statement matches the value of the switch-expression, the statements starting from this case are executed until either a break statement or the end of the switch statement is reached.
the keyword break is optional. The break statement immediately ends the switch statement.
the default case, which is optional, can be used to perform actions when none of the specified cases matches the switch-expression.
the case statements are checked in sequential order, but the order of the cases (including the default case) does not matter. However, it is good programming style to follow the logical sequence of the cases and place the default case at the end.
Consider the following code:
if (x>0)
y = 1;
else
y = -1;
Alternatively, we can use a conditional expression to achieve the same result.
y = (x>0) ? 1 : -1;
Conditional expressions are in a completely different style, with no explicit if in the statement.
The syntax is shown below:
booleanExpression ? expression1 : expression2;
if booleanExpression is TRUE, then execute expression1 else execute expression2.
Suppose we want to assign the larger number between variable num1 and num2 to max. So, we can simply write a statement using the conditional expression:
max = (num1 > num2) ? num1 : num2;
Displays the message "num is even" if num is even and otherwise displays "num is odd".
System.out.println((num % 2 == 0) ? "num is even" : "num is odd");