the break and continue statements. introduction there are 2 special statements that can affect the...

25
The break and continue statements

Upload: lenard-kennedy

Post on 24-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)

The break and continue statements

Page 2: The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)

Introduction

• There are 2 special statements that can affect the execution of loop statements (such as a while-statement)

• The special statements are:

We will study their meaning and how to use these special statements inside the while-statement

• break

• continue

Page 3: The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)

The break statement

• Syntax:

Effect:

break;

• 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 4: The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)

The break statement (cont.)

• Schematically:

Page 5: The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)

Programming example using the break statement: find the GCD

• Problem description:

• Write a Java program that reads in 2 numbers x and y...

• and prints the largest common divisor of both x and y

Page 6: The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)

Programming example using the break statement: find the GCD (cont.)

• A concrete example:

• Input: x = 24 and y = 16       

• Output: 8

Page 7: The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)

Programming example using the break statement: find the GCD (cont.)

• What would you do to solve this problem ?

• Suppose: x = 24 and y = 16

• The lesser of the values is 16

• Therefore, all divisors are ≤ 16

Page 8: The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)

Programming example using the break statement: find the GCD (cont.)

• Check if 16 and 24 are divisible by 16: no

• Check if 16 and 24 are divisible by 15: no

• ...

• Check if 16 and 24 are divisible by 10: no

• Check if 16 and 24 are divisible by 9: no

• Check if 16 and 24 are divisible by 8: YES

• Print 8 and STOP

Page 9: The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)

Programming example using the break statement: find the GCD (cont.)

• Rough algorithm:

input x, y;

min = min(x, y); // this is the range of the brute force search

for every value a = {min, min-1, min-2, ..., 1} do { if (x and y are divisible by a) { print a;

exit the while loop !!! } }

Page 10: The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)

Programming example using the break statement: find the GCD (cont.)

• Algorithm (structured diagram):

Page 11: The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)

Programming example using the break statement: find the GCD (cont.)

• Java program:

import java.util.Scanner; public class GCD01 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int x, y, a, min = 0; x = in.nextInt(); // Read in number y = in.nextInt(); // Read in number if ( x < y ) min = x; else min = y;

Page 12: The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)

Programming example using the break statement: find the GCD (cont.)

a = min; while ( a >= 1 ) // Run a = min(x,y), min(x,y)-1, ..., 1 { if ( x % a == 0 && y % a == 0 ) { // a is a divisor of x and y System.out.println(a); // Print a (because it's a common divisor) break; // Exit while loop !!! (Only need the largest) } else { a--; // Move to the next number !! } } } }

Page 13: The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)

Programming example using the break statement: find the GCD (cont.)

• Example Program: (Demo above code) – Prog file:

http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/Progs/GCD01.java

• How to run the program:            

• Right click on link and save in a scratch directory

• To compile:   javac GCD01.java

• To run:          java GCD01

Page 14: The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)

The continue statement

• Syntax:

continue;

Page 15: The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)

The continue statement (cont.)

• Effect:

• When the continue statement is executed inside a loop-statement, the program will skip over the remainder of the loop-body to the end of the loop

• Note:

• What happens next when the program reaches the end of a loop depends on the type of loop statement !!!

Page 16: The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)

The continue statement (cont.)

• Effect of a continue statement in a while-loop:

• As given previously:

• In the case of a while-loop, when the program reaches end of the loop, the program will jump back to the testing of the loop-continuation-condition

• the program will skip over the remainder of the loop-body to the end of the loop

Page 17: The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)

The continue statement (cont.)

• Schematically:

Page 18: The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)

Programming example using the continue statement: find all divisors of a number

• Problem description:

• Write a Java program that reads in an integer n...

• and prints all its divisors

Page 19: The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)

Programming example using the continue statement: find all divisors of a number (cont.)

• Previously discussed solution:

We try every number a = 1, 2, ..., n

For each number a, we check if n % a == 0.

Page 20: The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)

Programming example using the continue statement: find all divisors of a number (cont.)

• We can re-write the same algorithm differently using a continue statement as follows:

Page 21: The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)

Programming example using the continue statement: find all divisors of a number (cont.)

Notice that the if-condition has been changed to x % a != 0, meaning: a is not a divisor of x

When a is not a divisor of x, (the then-part), we increment a (to try next number) and jump to the end of the while-loop using the continue statement.

When x % a != 0 is false, the program will print a and increment a (to try next number)

Page 22: The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)

Programming example using the continue statement: find all divisors of a number

(cont.)• Java program:

public class Continue01 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n, a; n = in.nextInt(); // Read in number a = 1;

Page 23: The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)

Programming example using the continue statement: find all divisors of a number

(cont.)while ( a <= n ) // Run a = 1, 2, ..., n { if ( n % a != 0 ) { // a is NOT a divisor of n a++; continue; // Jump to end of while loop } /* ---------------------------------------------- We reach here ONLY when "n % a != 0" is FALSE I.e.: a is a divisor of x ---------------------------------------------- */ System.out.println(a); // Print a (because it's a divisor) a++; // Make sure we more to the next number !! // or else: infinite loop !!! } } }

Page 24: The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)

Programming example using the continue statement: find all divisors of a number

(cont.)• Example Program: (Demo above code)

– Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/Progs/Continue01.java

• How to run the program:            

• Right click on link and save in a scratch directory

• To compile:   javac Continue01.java

• To run:          java Continue01

Page 25: The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)

Programming advice

• Good programming practice:

• A computer program will be easier to understand if it is transparent.

• One way to improve transparency is a consistent flow of control Meaning, the program always take the same path of execution

• The break and the continue commands will alter the flow of control Therefore, they make a computer program less transparent

• It is a general recommendation to avoid using break and continue statements when you can write the algorithm easily without them.