Java Reference
In-Depth Information
As an other example, consider the following code snippet, which uses a switch
case statement:
Program 3.4 Function with branching structures
class FunctionWithConditionalStatement {
public static void main ( String [ ]
arguments )
{ double x=Math . E ;
System . out . println ( "Choose function to evalute for x=" +x )
;
System . out . print ( "(1) Identity, (2) Logarithm, (3) Sinus.
Your choice ?" );
int t=TC. lireInt () ;
System . out . println ( "F(x)=" +F ( t , x ) ) ;
}
// Observe that here we deliberately chose the function to
be declared after the main body
public static double F( int generator , double x)
{ double v=0.0;
switch (generator)
{ case 1: v=x; break ;
case 2: v=Math. log(x) ; break ;
case 3: v=Math. sin(x) ; break ;
}
return v;
}
}
This is one very nice feature of typed programming languages that check that
all return paths of functions have appropriate return type TypeR (eventually
by implicitly casting types).
3.3 Static (class) variables
Once functions have been introduced, we see that variable declarations
potentially appear in the body of all respective functions, and are no longer
found only in the main function. The variable scopes are nevertheless restricted
to the body of the function delimited by the braces; These variables cannot be
 
Search WWH ::




Custom Search