Professional Documents
Culture Documents
Loop Statements
while statement
while ( condition ) statement;
do {
do statement
for statement
Example
System.out.print( Enter a month (1 to 12): ); int month = scan.nextInt(); while (month < 1 || month > 12) { System.out.println( month + is not a valid month. ); System.out.print( Enter a month (1 to 12): ); month = scan.nextInt(); } // set initial value of month so that the while condition // below is false initially int month = -1; while (month < 1 || month > 12) { System.out.print( Enter a month (1 to 12): ); month = scan.nextInt(); }
The statement is executed once initially, then the condition is evaluated The statement is repetitively executed until the condition becomes false
Flowchart of a do Loop
do { statement; } while ( condition );
statement true condition evaluated false
statement
after the body of the loop is executed Therefore the body of a do loop will execute at least once
Example
Both semi-colons are always required The increment portion is executed at the end of each iteration
initialization
condition evaluated
true statement increment false
int counter = 1
true
sum+= counter
false
do {
do statement
for statement
this is a common type of logical error, and you should always double check to ensure that your loops will terminate normally
update state
e.g., increase the counter, add to sum, set the flag
1 2 3 4 5 6 7
7 6 5 4 3 2 1
number
reverse
1 2 3 4 5
7 6
number
reverse
1 2 3 4 5
7 6 5
int intReverse (int number) { int reverse , lastDigit; reverse = 0; while (number > 0) { lastDigit = number % 10; reverse = reverse * 10 + lastDigit; number = number / 10; } }
1 2 3 4
7 6 5
reverse = 0; while (number > 0) { lastDigit = number % 10; reverse = reverse * 10 + lastDigit; number = number / 10; }
Input next grade (possibly the sentinel) If ( the user has entered the sentinel) break; Add this grade into the running total Add one to the grade counter } If the counter is not equal to zero Set the average to the total divided by the counter Print the average Else Print No grades were entered
Exercise : AverageGrade
Write a program to compute the average of a sequence of (numerical) student grades entered by the user
If the user types something else than a number between 0.0 and 4.0, the program should abort.
23
Exercise : PalindromeTester
Write a program to read in a sequence of strings; for each string, determine whether it is a palindrome
25
Exercise : Stars
Write a program to print a triangle formed by * * ** *** The program should read in the number of rows from the user; the row should be between 1 to 10
Methods
Methods
A useful program can be long and contains many statements A method groups a sequence of statements and should provide a well-defined, easy-to-understand functionality
a method takes input, performs actions, and produces output
parameter list method name The parameter list specifies the type and name of each parameter return type The name of a parameter in the method properties declaration is called a formal argument
Calling a Method
Each time a method is called, the values of the actual arguments in the invocation are assigned to the formal arguments
int num = min (2, 3);
static int min (int num1, int num2) { int minValue = (num1 < num2 ? num1 : num2); return minValue; }
Method Overloading
A class may define multiple methods with the same name---this is called method overloading
usually perform the same task on different data types
Example: The PrintStream class defines multiple println methods, i.e., println is overloaded:
println (String s) println (int i) println (double d)
The following lines use the System.out.print method for different data types:
System.out.println ("The total is:"); double total = 0; System.out.println (total);
33
34
Method Overloading
Version 1 double tryMe (int x) { return x + .375; } Version 2
More Examples
double tryMe ( int x ) { return x + 5; } double tryMe ( double x ) { return x * .375; }
Which tryMe will be called?
tryMe( 1 );
tryMe( 1.0 );
tryMe( 1.0, 2); tryMe( 1, 2);
Variable Scoping
formal arguments
class variables
those defined in the class but not in the method
public int widen (int extra_width) { private int temp1; size += extra_width; } public int lenghten (int extra_lenth) { private int temp2; size += extra_length; }
}
Scope of Variables
public class Box { private int length, width;
public int widen (int extra_width) { private int temp1; size += extra_width; } public int lenghten (int extra_lenth) { private int temp2; size += extra_length; }
}
Class variables are valid in all methods of the class A formal argument is valid within its method Local variables are valid from the point of declaration to the end of the enclosing block
If a modification of the formal argument can change the value of the actual argument,
it is call by reference
If a formal argument is not a primitive data type, an operation on the formal argument can change the actual argument
this is call by reference more discussion in the later part of the course