Loops and Logic Part 2

Nested if Statements
The statement that is executed when an if expression is true can be another if, as can the statement in an else clause. This enables you to express such convoluted logic as "if my bank balance is healthy, then I will buy the car if I have my check book with me, else I will buy the car if I can get a loan from the bank." An if statement that is nested inside another can also itself contain a nested if. You can continue nesting ifs one inside the other like this for as long as you still know what you are doing—or even beyond if you enjoy confusion.

To illustrate the nested if statement, I can modify the if from the previous example:

if(number%2 == 0) {    // Test if it is even 
  if(number < 50) {     // Output a message if number is < 50
    System.out.println(“You have got an even number < 50, “ + number);
}
 

} else {
   System.out.println(“You have got an odd number, “ + number); // It is odd
}


Now the message for an even value is displayed only if the value of number is also less than 50. There are three possible outcomes from this code fragment: If number is even and less than 50, you see a message to that effect; if number is even and is not less than 50, there is no output; and finally; if number is odd, a message is displayed.

The braces around the nested if are necessary here because of the else clause. The braces constrain the nested if in the sense that if it had an else clause, it would have to appear between the braces enclosing the nested if. If the braces were not there, the program would still compile and run but the logic would be different. Let's see how.

With nested ifs, the question of which if statement a particular else clause belongs to often arises. If you remove the braces from the preceding code, you have:

if(number%2 == 0)    // Test if it is even 
if(number < 50 )    // Output a message if number is < 50
System.out.println(“You have got an even number < 50, “ + number); 

else
System.out.println(“You have got an odd number, “ + number); // It is odd


This has substantially changed the logic from the previous version, in spite of the fact that the indentation implies otherwise. The else clause now belongs to the nested if that tests whether number is less than 50, so the second println() call is executed only for even numbers that are greater than or equal to 50. This is clearly not what was intended because it makes nonsense of the output in this case, but it does illustrate the rule for connecting elses to ifs, which is:

An else always belongs to the nearest preceding if in the same block that is not already spoken for by another else.

You need to take care that the indenting of statements with nested ifs is correct. It is easy to convince yourself that the logic is as indicated by the indentation, even when this is completely wrong.

Let's try the if-else combination in another program.

Create the class LetterCheck, and code its main() method as follows:

public class LetterCheck {
public static void main(String[] args) {
char symbol = ‘A’; 

symbol = (char)(128.0*Math.random()); // Generate a random character
 

if(symbol >= ‘A’) {                   // Is it A or greater?
if(symbol <= ‘Z’) {                   // yes, and is it Z or less?
// Then it is a capital letter




Related Posts: