View difference between Paste ID: GbjsL6hm and xbui1TP5
SHOW: | | - or go back to the newest paste.
1
    /* This method will work, everything is scoped correctly. */
2
    public static void countToTen() {
3
4
        int count = 1; //count is declared outside the while loop; 
5
                       //the while loop can access the variable.        
6
        while ( count <= 10 ) {
7
            System.out.print( count + " " );
8
            count = count + 1;
9
        }
10
    }
11
12
    /* This method will not compile.  The variable nCubed is defined with too narrow a 
13
       scope for the return statement to access it. It's declared inside the 'if' 
14
       statement's braces, and the return is outside the 'if' statement's braces.
15
    */
16
    public static int cubed( int n ) {
17
        if ( n < 0 ) {
18
            int nCubed = n * n * n * -1;
19
        } else {
20
            int nCubed = n * n * n;
21
        }
22-
        return nCubed;
22+
        return nCubed; //compile error here
23
    }