220 likes | 312 Views
The while-statement. Syntax and meaning of the while-statement. The LOOP-CONTINUATION-CONDITION is a Boolean expression (exactly the same as in the condition clause of an if -statement ). Flow chart of a while -statement (1). Flow chart of a while -statement (2).
E N D
Syntax and meaning of the while-statement The LOOP-CONTINUATION-CONDITION is a Boolean expression (exactly the same as in the condition clause of an if-statement)
Flow chart of a while-statement (2) When the loop-cont-condition is true, the execution takes the downward branch: It executes the statements which for the body of the while-statement Then it goes back and retests the loop-cont-condition When the loop-cont-condition is false, the execution takes the side branch In this case, the executionproceeds (continues) with the statement following the while-statementI.e.,: the while-statement is completed
Example while-statement: print the numbers 1 through 10 public class While01 { public static void main(String[] args) { int a; a = 1; while ( a <= 10 ) // While-statement { System.out.println(a); // Print a a++; // Increment a } System.out.println("Done"); System.out.println("Exit: a = " + a); } }
Computer programming (1): Find all divisor of the number 12 I think you would have done this: Check if 12 is divisible by 1 Check if 12 is divisible by 2 ... Check if 12 is divisible by 12 Note: We do not need to check numbers > 12 because only number ≤ 12 can be divisors ! When the remainder of the division is equal to 0, we print out the divisor
Computer programming (2): Rough algorithm (pseudo code) to find all divisors: input: n = some integer number for (x = 1, 2, 3, ..., n) do { if ( n is divisible by x ) then print x; (because x is a divisor !) } We have basicallywritten down what we would do to find all divisors in a pseudo programming language !!!
Computer programming (3): int x = 1 while ( x <= n ) // Run x = 1, 2, ..., n { if ( n % x == 0 ) { System.out.println(x); // Print x (because it's a divisor) } x++; // Make sure we more to the next number !! }
Programming example 2: find all common divisors of 2 numbers (1) Problem description: Write a Java program that reads in 2 numbers x and y... and prints all numbers that are divisors of both x and y A concrete example: Input: x = 24 and y = 16 Output: 1, 2, 4, 8
Programming example 2: find all common divisors of 2 numbers (2) input x, y; min = min(x, y); // this is the range of the brute force search for (a = 1, 2, ...., min) do { if (x and y are divisible by a) { print a; } }
Programming example 2: find all common divisors of 2 numbers (3) while ( a <= min ) // Run a = 1, 2, ..., min(x,y) { if ( x % a == 0 && y % a == 0 ) { // a is a divisor of x and y System.out.println(a); } a++; }
The break statement When the break statement is executed inside a loop-statement, the loop-statement is terminated immediately The execution of the program will continue with the statement following the loop-statement
Using the while-statement to process data files What is a file: File = an electronical document stored inside a computer system that contains information (data) A file can be created by humans using a computer program called an editor (e.g., gedit) A file can also be created when a computer program needs to store its output data.
General procedure to access a data file General procedure in computer programming to read data from a data file Open the data file With the informationX, you can then use a "read something from a file"method to read data from the opened file There are other helpful methods on an opened file.Some method let you check if you have reached the end of the file
Opening a data file How toopen a file in Java: File myFile; // Define a "File" type variable myFile = new File("Path-name-of-the-file"); The variable myFile contains information about the opened file The variable myFile will be used in read operations
Scanning an opened file (1) Construct a Scanner object using an opened file: File myFile; // Define a "File" type variable myFile = new File("Path-name-of-the-file"); // Open the file Scanner in; // Define a Scanner typed variable in = new Scanner(myFile); // Construct a Scanner that read // data from opened file"myFile"
Scanning an opened file (2) From this point onwards, you can use in.nextDouble() to read a floating point number from the data file in.nextInt() to read an integer number from the data file in.next() to read a string (word) from the data file
Checking for available input data There are very useful methods available in the Scanner class to test if the input file is empty (exhausted) or not. Check function for input availability on Scanner typed variable in: in.hasNextDouble() in.hasNextInt() in.hasNext()
Programming example : print the content of a data file (1) Open the file "inp1" Construct a Scanner object using the opened file as long as ( there is data in the Scanner object ) { read a word from the Scanner object; print the word; }
Programming example : print the content of a data file (2) import java.io.*; import java.util.Scanner; public class File01 { public static void main(String[] args) throws IOException { File myFile = new File("inp1"); // Open file "inp1" Scanner in = new Scanner(myFile); String x; // Variable to receive a string while ( in.hasNext() ) { x = in.next(); // Read a string (word) System.out.println(x); // Print string read } System.out.println("Done"); } }