next up previous contents
Next: 4 Arrays, nested loops Up: 3 Iteration Previous: Flow control   Contents


Scope

Now that you have learnt about the main control statements if, for, while, you need to know about something called scope. The scope of a variable is the region of the program within which the variable can be referred to. A variable will only exist from the point at which it is declared until the end of the block of code it is in. A block of code is contained within curly brackets. If you declare a variable inside a loop, it will not be available outside the loop. At the end of the braces, the variable ``goes out of scope'' which is to say that it is no longer recognised by the compiler or available to use in your program. Study the examples below:

// Scope example 1 - this won't compile because of the last line.
// the scope of i is inside the loop
for (int i=0; i<10; i++)
{
    System.out.println(i);
}
System.out.println("final i=" + i); // wrong, outside the scope of i

// Scope example 2 - this is a corrected version of scope example 1
// the scope of i is outside the loop, because i is declared before
// the for loop.
int i;
for (i=0; i<10; i++)
{
    System.out.println(i);
}
System.out.println("final i=" + i); // correct, i is still in scope

// Scope example 3 - this won't compile either
boolean test = true;
if (test)
{
    boolean success = true;
    System.out.println(success); // this is ok
}
System.out.println(success); // wrong - outside the scope of `success'

If the third example is modified so that the boolean variable `success' is declared before the if statement, then it will work.

In general it is a good idea to declare variables within the most limited scope that they need, and to declare them as near as possible before they are used. This means if you use the variable by mistake elsewhere, you are more likely to get a compiler error rather than strange run-time behaviour, which is hard to debug.

So, if a variable is only needed inside a loop, it is best to declare it inside the loop. For example, if you like to use i as the iterator in for loops, you can declare it in the initial expression of the for loop, as shown in the example at the beginning of this section. Since i is now limited to the scope of the loop, you can declare it again in the next loop and be sure you are getting a different variable every time.

Exercise 3.3


(4 marks)



Which of the following examples will not compile due to a scope-related error? Explain why not, before you try to compile them. You can use the compiler to see if you are right. NB compiler error messages are not an acceptable answer to this question -- please give a full explanation of any flaws you identify.

// Example 1
// Sum the integers from 0 to 25?
for (int i = 0; i<=25; i++)
{
    int sum;
    sum = sum + i;
} 
System.out.println(sum);
// Example 2
// Sum the absolute value of integers from -25 to 25?
int sum = 0;
for (int i = -25; i<=25; i++)
{
    if ( i < 0)
    {
        int j = -i;
    }
    else
    {
        int j = i;
    }
    sum = sum + j;
} 
System.out.println(sum);




next up previous contents
Next: 4 Arrays, nested loops Up: 3 Iteration Previous: Flow control   Contents
RHUL Dept. of Physics