The forstatement Different loopstatements in Java provides 3






















- Slides: 22
The for-statement
Different loop-statements in Java provides 3 types of loop-statements: 1. The for-statement 2. The while-statement 3. The do-while-statement
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;
The for-statement (2)
Flow chart of a for-statement
The most common way to use a forstatement (2)
The most common way to use a forstatement (3) public class For 01 { 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"); } }
The most common way to use a forstatement (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
Flow chart of this program:
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
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 !
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; }
Programming example 1: find all divisors of a number (4) public class Divisors 01 { public static void main(String[] args) { Scanner in = new Scanner(System. in); int n, x; n = in. next. Int(); // 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); } }
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;
Programming example 2: compute the sum 1+2+3+. . . +n (2) public class Divisors 01 { 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. next. Int(); // 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 } }
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)
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;
Programming example 3: compute factorial n! (3) public class Factorial 01 { 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. next. Int(); // Read in number for ( x = 1; x <= n; x++ ) { product = product * x; // Multiply x into product } System. out. println(product); // Print final product }
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
Effect of the break and continue statements on the for-statement (1)
The continue statement • Syntax: continue; Effect: ◦ When the continue statement is executed inside a loopstatement, 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 !!!
Effect of the break and continue statements on the for-statement (2)