Repetition Statements Repetition statements allow us to execute

  • Slides: 23
Download presentation
Repetition Statements • Repetition statements allow us to execute a statement multiple times •

Repetition Statements • Repetition statements allow us to execute a statement multiple times • Often they are referred to as loops • Like conditional statements, they are controlled by boolean expressions • Java has three kinds of repetition statements: – while loop – do loop – for loop • The programmer should choose the right kind of loop for the situation 10/16/2021 CS 101 - Algorithms & Programming I 1

Java Repetition Statements while (condition) statement for ( init; condition; update) statement do statement

Java Repetition Statements while (condition) statement for ( init; condition; update) statement do statement while (condition); where • statement is any Java statement • condition is a boolean expression 10/16/2021 CS 101 - Algorithms & Programming I 2

while Statement • The most general one of these loop statements is while (

while Statement • The most general one of these loop statements is while ( boolean-expression ) statement where statement can be any statement including a compound statement, an if-statement, another loop statement, . . . • Statement is repeated as long as the condition (boolean-expression) is true. When the condition gets false, the statement is not executed anymore. • If the condition never gets false infinite loop • If the condition gets immediately false the loop will be repeated zero times. 10/16/2021 CS 101 - Algorithms & Programming I 3

while Statement – Flow Diagram while ( condition ) statement condition true statement false

while Statement – Flow Diagram while ( condition ) statement condition true statement false 10/16/2021 CS 101 - Algorithms & Programming I 4

while Statement – Counter Controlled Loop(1) • Print 5 asterisk characters • This loop

while Statement – Counter Controlled Loop(1) • Print 5 asterisk characters • This loop will be repeated for 5 times (count: 0, 1, . . . , 4) count = 0; while ( count < 5 ) { System. out. println( “*”); count = count + 1; } System. out. println( “done”); 10/16/2021 CS 101 - Algorithms & Programming I * * * done 5

while Statement – Counter Controlled Loop(2) • Read & sum 5 values sum =

while Statement – Counter Controlled Loop(2) • Read & sum 5 values sum = 0; count = 0; while ( count < 5 ) { value = scan. next. Int(); sum = sum + value; count = count + 1; } System. out. println( “sum is ” + sum); 10/16/2021 CS 101 - Algorithms & Programming I 5 3 7 4 1 sum is 20 6

Counter Controlled Loops • Template of counter-controlled loops initialize counter while ( condition depending

Counter Controlled Loops • Template of counter-controlled loops initialize counter while ( condition depending on counter) { other statements to be repeated update counter } 10/16/2021 CS 101 - Algorithms & Programming I 7

Infinite Loops • The body of a while loop eventually must make the condition

Infinite Loops • The body of a while loop eventually must make the condition false • If not, it is called an infinite loop, which will execute until the user interrupts the program – in console aapplications use Ctrl-C to exit • This is a common logical error • You should always double check the logic of a program to ensure that your loops will terminate normally int count = 1; while (count <= 25){ System. out. println (count); count = count - 1; } 10/16/2021 CS 101 - Algorithms & Programming I 8

Infinite Loops (2) i = 1; while ( i != 50 ) { System.

Infinite Loops (2) i = 1; while ( i != 50 ) { System. out. println( i); i = i + 2; } System. out. println( “done”); i = scan. next. Int(); while ( i != 1 ) { if ( i % 2 == 0) i = i / 2; else i = 3 * i + 1; } System. out. println( “done”); 10/16/2021 CS 101 - Algorithms & Programming I Sometimes, it is difficult to check loop terminating condition. 9

Sentinel-Controlled Loops • What happens if we don’t know how many times a loop

Sentinel-Controlled Loops • What happens if we don’t know how many times a loop will run. • Ex: a loop which reads the scores of the students in an exam and find the average of the scores; and we we don’t know the number of students. How are we going to stop the loops? use a sentinel-value • We choose a sentinel-value which can not be a score (e. g. – 1) • We read the scores until this sentinel-value has been entered. When this sentinel-value has been read, we stop the loop. 10/16/2021 CS 101 - Algorithms & Programming I 10

Sentinel-Controlled Loops – Example(1) int sum = 0; int num. Of. Students = 0;

Sentinel-Controlled Loops – Example(1) int sum = 0; int num. Of. Students = 0; int ascore; double avg; System. out. print(“Enter a score (-1 to stop) >”); ascore = scan. next. Int(); while (ascore != -1) { sum = sum + ascore; num. Of. Students = num. Of. Students + 1; System. out. print(“Enter a score (-1 to stop) >”); ascore = scan. next. Int(); } avg = (double) sum / num. Of. Students; System. out. println(“The number of students : “ + num. Of. Students); System. out. println(“Average Score : “ + avg); 10/16/2021 CS 101 - Algorithms & Programming I 11

Example Program 1 // This program finds the factorial value of the given positive

Example Program 1 // This program finds the factorial value of the given positive integer import java. util. Scanner; public class Factorial { public static void main(String[] args){ int num, fact. Val, counter; // Create a Scanner object Scanner scan = new Scanner(System. in); // Read the positive integer System. out. print("A Positive Integer: "); num = scan. next. Int(); // Find its factorial value counter = 2; fact. Val = 1; while (counter <= num) { fact. Val = fact. Val * counter; counter = counter + 1; } // Write the result System. out. println("Given Positive Integer: " + num); System. out. println("Its Factorial value : " + fact. Val); } } 10/16/2021 CS 101 - Algorithms & Programming I 12

for Statement • Another loop statement in Java is for-statement. • for-statement is more

for Statement • Another loop statement in Java is for-statement. • for-statement is more suitable for counter-controlled loops. for ( initialization ; condition ; increment ) statement which is equivalent to initialization ; while (condition ){ statement ; increment ; } 10/16/2021 CS 101 - Algorithms & Programming I 13

for Statement – Flow Diagram initialization condition false true statement increment 10/16/2021 CS 101

for Statement – Flow Diagram initialization condition false true statement increment 10/16/2021 CS 101 - Algorithms & Programming I 14

for statement -- Example int i; int sum = 0; for (i=1; i<=20; i++)

for statement -- Example int i; int sum = 0; for (i=1; i<=20; i++) sum = sum + i; int i; int sum = 0; i = 1; while (i<=20) { sum = sum + i; i++; } int i; int sum = 0; for (i=100; i>=1; i--) sum = sum + i; int i; int sum = 0; i = 100; while (i>=1) { sum = sum + i; i++; } 10/16/2021 CS 101 - Algorithms & Programming I 15

for statement -- Example int i, j; int count=0; for (i=1, j=10; i<j; i++,

for statement -- Example int i, j; int count=0; for (i=1, j=10; i<j; i++, j--) count++; count is 5 int i, j; int count=0; i=1; j=10; for (; i<j; ) { count++; i++; j--; } 10/16/2021 CS 101 - Algorithms & Programming I 16

do-while statement • The third loop statement in Java is do-while statement. do statement

do-while statement • The third loop statement in Java is do-while statement. do statement while ( condition ) ; which is equivalent to statement while (condition ) statement 10/16/2021 CS 101 - Algorithms & Programming I 17

do-while statement – Flow Diagram statement true condition false 10/16/2021 CS 101 - Algorithms

do-while statement – Flow Diagram statement true condition false 10/16/2021 CS 101 - Algorithms & Programming I 18

do-while statement -- Example int i=1; do { System. out. println(i); i++; } while

do-while statement -- Example int i=1; do { System. out. println(i); i++; } while (i<10); String s; do { System. out. print(“Enter a word > “); System. out. flush(); s = scan. next. Line(); System. out. println(s); } while (! s. equals(“quit”)); 10/16/2021 CS 101 - Algorithms & Programming I 19

Nested Loops • If the body of a loop contains another loop, this is

Nested Loops • If the body of a loop contains another loop, this is called as a nested loop. int sum=0; int i, j; for (i=1; i<=5; i++) for (j=1; j<=6; j++) sum=sum+1; sum is 5*6=30 int sum=0; int i, j; for (i=1; i<=5; i++) for (j=1; j<=i; j++) sum=sum+1; sum is 1+2+3+4+5=15 10/16/2021 CS 101 - Algorithms & Programming I 20

Nested Loops – Example 1 a right angled triangle (n lines, ith line contains

Nested Loops – Example 1 a right angled triangle (n lines, ith line contains i stars) * ** ***. . * 10/16/2021 for (i=1; i<=n; i++) { // for each line for (j=1; j<=i; j++) System. out. print(“*”); System. out. println(“”); } CS 101 - Algorithms & Programming I 21

Nested Loops – Example 2 • a triangle shape (1 st line contains 1

Nested Loops – Example 2 • a triangle shape (1 st line contains 1 star, 2 nd line contains 3 star, . . . , nth line contains 2 n-1 stars) * *****. . *** 10/16/2021 for (i=1; i<=n; i++) { // for each line for (j=1; j<=(n-i); j++) System. out. print(“ ”); for (j=1; j<=(2*i-1); j++) System. out. print(“*”); System. out. println(“”); } CS 101 - Algorithms & Programming I 22

Nested Loops • Nesting can be more than one level int sum=0; for (i=1;

Nested Loops • Nesting can be more than one level int sum=0; for (i=1; i<=5; i++) for (j=1; j<=5; j++) for (k=1; k<=5; k++) sum=sum+1; 10/16/2021 CS 101 - Algorithms & Programming I sum is 125 23