Week 5 For Loop.pdf
Document Details
Uploaded by SimplifiedLaplace
Tags
Full Transcript
Iteration Structures for loop for Statement • The for loop is used to execute one or more statements a specified number of times • Syntax / General form for(initialization; conditional test; increment/dec) { //loop body statement(s) } for Loop • The initialization section is executed only once...
Iteration Structures for loop for Statement • The for loop is used to execute one or more statements a specified number of times • Syntax / General form for(initialization; conditional test; increment/dec) { //loop body statement(s) } for Loop • The initialization section is executed only once • An initial value is assigned to the loop-control variable • Loop continues while conditional test is true and terminates when it becomes false • Conditional test performed at the start or top of the loop each time the loop is repeated • The increment/decrement is executed at the end of each pass through the loop for Flowchart for(initialisation; cond exp; incr/decr) { statements; } initialisation Conditional expression false true statement Increment/decrement for Flowchart for(int counter = 1; counter<=10; counter++) { System.out.print(counter + “ ”); } int counter = 1 counter <=10 false true System.out.print(counter); counter++ Sample Program 1 //prints numbers 1 to 10 public class ForSample1 { public static void main(String[] args) { for(int counter = 1; counter <= 10; counter++) { System.out.print(counter +" "); } } //end main method } //end class ***NOTE: the variable counter is local to the loop and only exists while the loop is executing*** Sample Program 2 //prints numbers 1 to 10 public class ForSample2 { public static void main(String[] args) { int counter; for(counter = 1; counter <= 10; counter++) { System.out.print(counter +" "); } } //end main method } //end class ***Variable can be declared outside loop but not usual 3 parts of for loop • int counter =1 • counter <=10 • counter ++ initialization conditional test increment Dissect the program • The first line of the for statement contains three expressions, enclosed in parentheses – First expression assigns an initial value 1 to the integer variable counter – The second expression continues the looping action as long as the current value of counter does not exceed 10 at the beginning of each pass – The 3rd expression increases the value of counter by 1 at the end of each pass through the loop. • The System.out.print() statement is the body of the loop and produces the desired output. Sample Program 3 //prints numbers 10 to 0 and then Blastoff public class ForSample3 { public static void main(String[] args) { for(int counter = 10; counter >= 0; counter--) ` { System.out.println(counter); } System.out.println("blastoff!!!"); } //end main method } //end class Accumulating Values • Often you need to sum values • Set the total variable to 0 • The accumulation instruction total = total + counter; tells the computer to add the value of counter to the old value of total and to store the result in total • The value of counter is incremented allowing progression from one value to the next until the end of the loop. Sample Program 4 //find sum of numbers from 1 to 5 public class ForSample4{ public static void main(String[] args){ int total = 0; for(int counter = 1; counter <= 5; counter++) { total = total + counter; //add current value of counter to total } System.out.println("total of numbers 1 to 5 is : " +total); } //end main method } //end class Calc the average of 5 numbers using a for loop. 1. Assign a value of 0 to variable total 2. Assign a value of 1 to the integer variable counter, where counter is an index that counts the number of passes through the loop 3. Repeat as long as the value of counter does not exceed 5 4. 5. 1. 2. Read in next number in the list, and store in variable num Add the value of num to the current value of total 3. Increase the value of counter by 1 Divide the value of total by 5 to obtain the desired average Display the calculated value for the average pseudocode SET total to 0 FOR each value of counter from 1 to 5 GET num ADD num to total CALC average (total / 5) DISPLAY average //program to find average of n values import java.util.*; public class AverageValues { public static void main(String[] args) { Scanner keyboardIn = new Scanner(System.in); int num, total = 0; double average; for(int counter = 1; counter <= 5; counter++) { System.out.println("Enter value " +counter +": " ); num = keyboardIn.nextInt(); //add num to total total = total + num; } //calc average average = (double)total/5; //display average System.out.println("Average is " +average); } //end main method } //end class Calc the average of a list of n numbers using a for loop. 1. Assign a value of 0 to variable total 2. 3. 4. 5. 6. Read in a value for the integer variable n Assign a value of 1 to the integer variable counter, where counter is an index that counts the number of passes through the loop Repeat as long as the value of counter does not exceed n 1. 2. Read in next number in the list, and store in variable num Add the value of num to the current value of total 3. Increase the value of counter by 1 Divide the value of total by n to obtain the desired average Display the calculated value for the average pseudocode SET total to 0 GET n FOR each value of counter from 1 to n GET num ADD num to total CALC average (total / n) DISPLAY average //program to find average of n values import java.util.*; public class AverageNValues { public static void main(String[] args) { Scanner keyboardIn = new Scanner(System.in); int num, n, total = 0; double average; System.out.print("How many values do you want to enter?" ); n = keyboardIn.nextInt(); for(int counter = 1; counter <= n; counter++) { System.out.println("Enter value " +counter +": " ); num = keyboardIn.nextInt(); //add num to total total = total + num; } //calc average average = (double)total/n; //display average System.out.println("Average is " +average); } //end main method } //end class • As a rule of thumb: – while and do-while loops are generally used when the number of passes is not known in advance – for loops are generally used when the number of passes is known for vs while public class ForEx1 { public static void main(String[] args) { for( int count=0; count <= 9; count++) { System.out.print(count +" "); } public class WhileEx1 { public static void main(String[] args) { int count = 0; while(count <= 9) { System.out.print(count +" "); count ++; } } } } } for vs while public class ForSample1 { public static void main(String[] args) { for( int count=0; count <= 9; count++) { System.out.print(count +" "); } public class While1 { public static void main(String[] args) { int count = 0; while(count <= 9) { System.out.print(count +" "); count ++; } } } } } Initialisation for vs while public class ForSample1 { public static void main(String[] args) { for( int count=0; count <= 9; count++) { System.out.print(count +" "); } public class While1 { public static void main(String[] args) { int count = 0; while( count <= 9) { System.out.print(count +" "); count ++; } } } } } Initialisation Conditional expression for vs while public class ForSample1 { public static void main(String[] args) { for( int count=0; count <= 9; count++) { System.out.print(count +" "); } public class While1 { public static void main(String[] args) { int count = 0; while( count <= 9) { System.out.print(count +" "); count++; } } } } } Initialisation Conditional expression Increment