Download presentation
Presentation is loading. Please wait.
Published byLoraine Lee Modified over 9 years ago
1
The for-statement
2
Different loop-statements in Java Java provides 3 types of loop-statements: 1. The for-statement 2. The while-statement 3. The do-while-statement
3
The for-statement (1) The for-statement is ideally suited for making the following type of program: Let some variable x take on a series of value one at a time For each value taken on by a variable x, the body of the for-statement is executed once. repeat ( for x = 1, 2, 3,.... 10 ) execute this statement;
4
The for-statement (2)
5
Flow chart of a for-statement
6
The most common way to use a for- statement (2)
7
The most common way to use a for- statement (3) public class For01 { public static void main(String[] args) { int a; for ( a = 1 ; a <= 10 ; a = a + 1 ) { System.out.println(a); // Print a } System.out.println("Done"); }
8
The most common way to use a for- statement (1) The for-statement was originally invented to let some variable take on a series of value for ( var = START_VALUE ; var <= STOP_VALUE ; var = var + INCR ) { /* for-body (statements) */ } For each of the value, the statements in the for-body are executed
9
Flow chart of this program:
10
Programming example 1: find all divisors of a number (1) Write a Java program that reads in an integer n... and prints all its divisors Input: n = 12 Output: 1, 2, 3, 4, 6, 12
11
Programming example 1: find all divisors of a number (2) Check if 12 is divisible by 1 Check if 12 is divisible by 2... Check if 12 is divisible by 12 We do not need to check numbers > 12 because only number ≤ 12 can be divisors !
12
Programming example 1: find all divisors of a number (3) for (x = 1, 2, 3,...., 10) do { print x; } This notation can be converted to a for-statement very naturally: for ( x = 1; x <= 10; x = x + 1 ) { print x; }
13
Programming example 1: find all divisors of a number (4) public class Divisors01 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n, x; n = in.nextInt(); // Read in number for ( x = 1; x <= n; x++ ) // Run x = 1, 2,..., n { if ( n % x == 0 ) { // x is a divisor of n System.out.println(x); }
14
Programming example 2: compute the sum 1+2+3+...+n (1) input: n = some integer number sum = 0; // Clear the running total ! for ( x = 1, 2, 3,..., n ) do { Add x to sum } Print sum;
15
Programming example 2: compute the sum 1+2+3+...+n (2) public class Divisors01 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n, x, sum = 0; System.out.print("Enter a number n: "); n = in.nextInt(); // Read in number for ( x = 1; x <= n; x++ ) // Run x = 1, 2,..., n { sum = sum + x; // Add x to sum } System.out.println(sum); // Print final sum }
16
Programming example 3: compute factorial n! (1) Write a Java program that reads in an integer n... and prints the factorial n! = 1×2×3×...×n Input: n = 5 Output: 120 (because 1 × 2 × 3 × 4 × 5 = 120)
17
Programming example 3: compute factorial n! (2) input: n = some integer number product = 1; // Set running product to 1 for ( x = 1, 2, 3,..., n ) do { Multiply x into product } Print sum;
18
Programming example 3: compute factorial n! (3) public class Factorial01 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n, x, product = 1; System.out.print("Enter a number n: "); n = in.nextInt(); // Read in number for ( x = 1; x <= n; x++ ) { product = product * x; // Multiply x into product } System.out.println(product); // Print final product }
19
The break and continue statements Introduction There are 2 special statements that can affect the execution of loop statements (such as a for-statement) The special statements are: ■ break ■ continue We will study their meaning and how to use these special statements inside the while-statement
20
Effect of the break and continue statements on the for-statement (1)
21
The continue statement Syntax: continue; 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 body ◦Note: ■What happens next when the program reaches the end of a loop depends on the type of loop statement !!!
22
Effect of the break and continue statements on the for-statement (2)
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.