Nesting

From CompSciWiki
Jump to: navigation, search

COMP 1010 Home > Control Structures


Introduction

Conditional.jpg

Sometimes you will find it necessary to test for different conditions depending on what information you have available in your program. For example, if you want to write a program that asks the user to enter a number, and print out whether or not the number is square it would be very difficult to write it using separate if-statements all lined up one after the other:

First you should check that the user did actually enter a number. Then you would probably want to check if the number was positive or negative (since negative numbers cannot be square). Finally, if the user did enter a number, and the number was positive you could do the calculations to see if it is square or not.

Placing if-statements inside each other is called nesting. In this section we will discuss nesting if-statements in more detail.

Nesting

You have briefly been introduced to the concept of nesting in the last section. We explained that nesting if-statements can be achieved by using logical operators. Logical operators can do a great job of simplifying code. However, there is a point where logical operators become a poor choice and nesting is the better option.

What exactly is nesting? Nesting involves placing one control statement inside another. This means that the inner control statement will be located within the outer statement's curly braces. As you should already know, a control statement can be an if, an if-else, or an else statement. There are more control statements that are subject to nesting, but they will not be included in this section. Here is a simple example of some nesting:

 public class Nesting
{
  public static void main(String[] args)
  {
    int i = 7;
    
    if (i > 0)
    {
      // The next "if" is surrounded by the curly braces of the first "if" statement
      if (i > 5)
      {
        // The last "if" is surrounded by the curly braces of the second "if" statement
        if (i > 10)
        {
          
        }
      }
    }
  }
} 

What is the flow of execution? Let's walk through it together. i is greater than zero, so it will begin executing inside the first if-statement. i is also greater than five, so execution now begins in the second if-statement. i is NOT greater than ten, so execution does not branch into the final if-statement. There is no code left in the second if-statement so execution returns to the first if-statement. There is no code left in the first if statement so execution returns to the main. It may look complex at first, but nesting is really easy to follow and understand. The most important thing to remember is getting the braces right. If the braces are missing or extras are added, chances are that the program will not compile at all. A useful hint is to line up opening and closing braces on the same line so you do not lose track of them.

Nesting can continue for an infite amount of levels. In practice, you rarely see if-statements that go more than five levels deep. At this point it will start to get hard to follow where the statements start and end. If your nesting is more than five levels deep, it may be a hint that logical operators could be used to simplify your code.

When nesting any control structures, it is very important to ensure that you line up your braces (the squiggly brackets). Without lining them up properly, it becomes hard to distinguish what goes with what structure, and you'll lose marks on your assignments! Here is an example of bad brace alignment using the code from above:

 public class Nesting
{
  public static void main(String[] args){
    int i = 7;
    
    if (i > 0){
    // The next "if" is surrounded by the curly braces of the first "if" statement
    if (i > 5){
    // The last "if" is surrounded by the curly braces of the second "if" statement
    if (i > 10){
          
    }
    }
    }
  }
} 

Chapter Summary

In this chapter, you have learned how to use the if, if-else or else-if control structures that allow you to make decisions in your code. We have also covered how to deal with multiple conditions through the use of logical and relative operators, and nesting of control statements. You should now have enough expertise in this area to try your hand at the Program-A-Day questions that are found in the Review Questions and Exercises section for this chapter.

Previous Page: Conditions Next Page: Control Stuctures Review Questions and Exercises