The while-statement - PowerPoint PPT Presentation

The while-statement

1 / 22
The while-statement

The while-statement

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

1. The while-statement

2. 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)

3. Flow chart of a while-statement (1)

4. 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

5. Structure diagram representing a while-statement

6. 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); } }

7. 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

8. 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 !!!

9. 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 !! }

10. 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

11. 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; } }

12. 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++; }

13. 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

14. The continue statement

15. 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.

16. 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

17. 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

18. 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"

19. 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

20. 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()

21. 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; }

22. 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"); } }