Download presentation
Presentation is loading. Please wait.
1
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 4/29/2019 CS102 - Algorithms & Programming II
2
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 4/29/2019 CS102 - Algorithms & Programming II
3
CS102 - Algorithms & Programming II
while Statement The most general one of these loop statements is while. 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. 4/29/2019 CS102 - Algorithms & Programming II
4
while Statement – Flow Diagram
while ( condition ) statement true condition statement false 4/29/2019 CS102 - Algorithms & Programming II
5
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”); * * * * * done 4/29/2019 CS102 - Algorithms & Programming II
6
while Statement – Counter Controlled Loop(2)
Read & sum 5 values sum = 0; count = 0; while ( count < 5 ) { value = scan.nextInt(); sum = sum + value; count = count + 1; } System.out.println( “sum is ” + sum); sum is 20 4/29/2019 CS102 - Algorithms & Programming II
7
Counter Controlled Loops
Template of counter-controlled loops initialize counter while ( condition depending on counter) { other statements to be repeated update counter } 4/29/2019 CS102 - Algorithms & Programming II
8
CS102 - Algorithms & Programming II
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 applications 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; } 4/29/2019 CS102 - Algorithms & Programming II
9
CS102 - Algorithms & Programming II
Infinite Loops (2) i = 1; while ( i != 50 ) { System.out.println( i); i = i + 2; } System.out.println( “done”); i = scan.nextInt(); while ( i != 1 ) { if ( i % 2 == 0) i = i / 2; else i = 3 * i + 1; Sometimes, it is difficult to check loop terminating condition. 4/29/2019 CS102 - Algorithms & Programming II
10
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. 4/29/2019 CS102 - Algorithms & Programming II
11
Sentinel-Controlled Loops – Example(1)
int sum = 0; int numOfStudents = 0; int ascore; double avg; System.out.print(“Enter a score (-1 to stop) >”); ascore = scan.nextInt(); while (ascore != -1) { sum = sum + ascore; numOfStudents = numOfStudents + 1; } avg = (double) sum / numOfStudents; System.out.println(“The number of students : “ + numOfStudents); System.out.println(“Average Score : “ + avg); 4/29/2019 CS102 - Algorithms & Programming II
12
CS102 - Algorithms & Programming II
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, factVal, counter; // Create a Scanner object Scanner scan = new Scanner(System.in); // Read the positive integer System.out.print("A Positive Integer: "); num = scan.nextInt(); // Find its factorial value counter = 2; factVal = 1; while (counter <= num) { factVal = factVal * counter; counter = counter + 1; } // Write the result System.out.println("Given Positive Integer: " + num); System.out.println("Its Factorial value : " + factVal); 4/29/2019 CS102 - Algorithms & Programming II
13
CS102 - Algorithms & Programming II
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 ; } 4/29/2019 CS102 - Algorithms & Programming II
14
for Statement – Flow Diagram
initialization false condition true statement increment 4/29/2019 CS102 - Algorithms & Programming II
15
for statement -- Example
int i; int i; int sum = 0; int sum = 0; for (i=1; i<=20; i++) i = 1; sum = sum + i; while (i<=20) { sum = sum + i; i++; } int i; int i; int sum = 0; int sum = 0; for (i=100; i>=1; i--) i = 100; sum = sum + i; while (i>=1) { 4/29/2019 CS102 - Algorithms & Programming II
16
for statement -- Example
int i, j; int count=0; for (i=1, j=10; i<j; i++, j--) count is 5 count++; i=1; j=10; for (; i<j; ) { i++; j--; } 4/29/2019 CS102 - Algorithms & Programming II
17
CS102 - Algorithms & Programming II
do-while statement The third loop statement in Java is do-while statement. do statement while ( condition ) ; which is equivalent to while (condition ) 4/29/2019 CS102 - Algorithms & Programming II
18
do-while statement – Flow Diagram
true condition false 4/29/2019 CS102 - Algorithms & Programming II
19
do-while statement -- Example
int i=1; do { System.out.println(i); i++; } while (i<10); String s; System.out.print(“Enter a word > “); System.out.flush(); s = scan.nextLine(); System.out.println(s); } while (! s.equals(“quit”)); 4/29/2019 CS102 - Algorithms & Programming II
20
CS102 - Algorithms & Programming II
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 for (j=1; j<=i; j++) sum=sum+1; sum is =15 4/29/2019 CS102 - Algorithms & Programming II
21
CS102 - Algorithms & Programming II
Nested Loops – Example1 a right angled triangle (n lines, ith line contains i stars) * for (i=1; i<=n; i++) { // for each line ** for (j=1; j<=i; j++) *** System.out.print(“*”); System.out.println(“”); } *** .. * 4/29/2019 CS102 - Algorithms & Programming II
22
CS102 - Algorithms & Programming II
Nested Loops – Example2 a triangle shape (1st line contains 1 star, 2nd line contains 3 star,..., nth line contains 2n-1 stars) * 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(“”); } 4/29/2019 CS102 - Algorithms & Programming II
23
CS102 - Algorithms & Programming II
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; sum is 125 4/29/2019 CS102 - Algorithms & Programming II
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.