Advertisement
Guest User

Untitled

a guest
Feb 26th, 2013
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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; //compile error here
  23.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement