

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


CS101J, Cornell 0 Local variables Chapter Local variable : A variable declared in a method body. /** = smallest of x, y, and z */ public void m( int x, int y, int z) { if (x > y) { // Swap x and y. int temp; temp= x; x= y; y= temp; } if (x > y) { // Swap x and z. int temp; temp= x; x= z; z= temp; } return x; } CS101J, Cornell 1 Local variables Chapter Local variable : A variable declared in a method body. Form of declaration : < type > < variable-name > ; or < type > < variable-name > = < expression > ; // Swap x and z. int temp= x; x= z; z= temp;
CS101J, Cornell 6 Long parameter names complicate /** = sum of values in range first_value..last_value. Precondition: first_value <= last_value+1. / public void p( int first_value, int last_value) { int s= first_value; // inv: s = sum of first_value..k– 1 for ( int k= first_value; k <= last_value; k= k+1) { s= s + k; } return s; } CS101J, Cornell 7 Short parameter names simplify /* = sum of values in range m..n. Precondition: m <= n+1. */ public void p( int m, int n) { int s= m; // inv: s = sum of m..k– 1 for ( int k= m; k <= n; k= k+1) { s= s + k; } return s; } Short parameter names are better —as long as the specification mentions the parameters appropriately. Short local-variables names are better —as long the local variables are appropriately described. — as do short local-variable names