the while-statement. syntax and meaning of the while-statement the loop-continuation-condition is a...

22
The while-statement

Upload: shanna-cameron

Post on 18-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

The while-statement

Page 2: 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

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)

Page 3: 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

Flow chart of a while-statement (1)

Page 4: 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

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 execution proceeds (continues) with the statement following the while-statementI.e.,: the while-statement is completed

Page 5: 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

Structure diagram representing a while-statement

Page 6: 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

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

Page 7: 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

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

Page 8: 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

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 basically written down what we would do to find all divisors in a pseudo programming language !!!

Page 9: 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

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

Page 10: 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

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

Page 11: 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

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;

}

}

Page 12: 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

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++;

}

Page 13: 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

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

Page 14: 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

The continue statement

Page 15: 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

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.

Page 16: 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

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 information X, 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

Page 17: 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

Opening a data file

• How to open 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

Page 18: 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

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"

Page 19: 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

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

Page 20: 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

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

Page 21: 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

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;

}

Page 22: 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

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