Download presentation
Presentation is loading. Please wait.
Published byLenard Day Modified over 9 years ago
2
Demonstrate use of a “for loop” in the design and development of a Java program to calculate the total of a one-dimensional array of 6 integers. Design your test data (use numbers that will be easy to check for the correct answer): ◦ 10, 20, 30, 40, 50, 60 ◦ Expected total is 210. Design and code a solution to the problem.
3
while (condition) { // if false then loop not executed statement(s) inside the loop } // end while do { // always done at least once statement(s) inside the loop } while (condition) // end do for (control initial value; (condition) ; control update) { // if false then loop not executed statement(s) inside the loop } // end for Condition is always either true or false. while and do: something must be updated inside the loop which affects the condition (so that it is not an endless loop). for: something is updated inside the “for” statement which affects the condition (so that it is not an endless loop). e.g. while (i < 4) { System.out.println("i "= " + i); i = i + 1; } // end while
4
for (it is raining; while it rains; check if it is raining) { I will need my umbrella } // end for (the for loop has ended therefore the condition must now be false) // I do not need my umbrella because it is not raining. The for loop will not be executed if the condition is false, just like the while loop. condition then clause Remember the while loop example? while (it is raining) { then I will need my umbrella } // end while // Now I do not need my umbrella // (because it must have stopped raining)
5
for (it is raining; while it rains; check if it is raining) { I will need my umbrella } // end for // (now the condition must be false) // Now I do not need my umbrella because it must not be raining. condition then clause initialisation statement loop condition increment statement Normally we use a for loop when we know how many times we want to execute the loop.
6
for (i = 0; i < 5; i++) { statement(s) } // end for Has the same effect as: condition then clause loop control variable is i initialisation statement i = zero loop condition while i < 5 is true int i = 0; while (i<5){ statement(s) i++; // must not forget to update the control variable } // end while increment statement add 1 to i
7
if (it is raining) { then I will need my umbrella for (it is raining; while it is raining; check for rain) { then I will need my umbrella } // end for } // end if Now I do not need my umbrella if (your_age < 65) for (the_age = your age; the_age < 65; the_age++) { then you cannot get your state pension } // end for } // end if Now you can get your state pension condition then clause initialisation statement loop condition increment statement
8
for (it is raining or snowing; it is raining or snowing; check weather) then I will need my umbrella } // end while // I will not need my umbrella //(because it is not raining and it is not snowing) for (it is sunny and hot; it is sunny and hot; check weather) { then I will need a sunshade and a cold drink } // end while // I will not need a sunshade and a cold drink // (this means it is not both sunny and hot // it might be sunny but cold, or it might be cloudy but hot, // either way I have no sunshade and get no drink) initialisation statement loop condition increment statement
9
import javax.swing.JOptionPane; public class week9 { public static void main (String[] args) { int i=0; int j=0; for (i = 1; i < 5 && j < 5; i++) { // statements } System.out.println("i = " + i + " j = " + j); } initialisation statement loop condition increment statement
10
import javax.swing.JOptionPane; public class week9 { public static void main (String[] args) { int i=0; int j=0; for (i = 1; i < 5 || j < 5; i++) { // statements } System.out.println("i = " + i + " j = " + j); } What would happen if we ran this program? initialisation statement loop condition increment statement
11
for (i = 1; i < 5; i = i + 1) { statement(s) for (j = 1; j < 5; j = j + 1) { statement(s) } // end for initialisation statement loop condition increment statement
12
Demonstrate use of a “for loop” in the design and development of a Java program to calculate the total of a one-dimensional array of 6 integers. Design your test data (use numbers that will be easy to check for the correct answer): ◦ 10, 20, 30, 40, 50, 60 ◦ Expected total is 210. Design and code a solution to the problem. Think about the meaning. How can we use a for loop here? Normally we use a for loop when we know how many times we want to execute the loop. We could use a loop to access each element of the array one at a time. We will need a variable to hold the total, and a variable to count the elements of the array.
13
1. Get unknown data and create variables. 1.1 create an array with values 10, 20, 30, 40, 50, 60. 1.2 create and zeroise total. 1.3 create and zeroise counter. 2. Processing. 2.1 count for six times 2.2 add current array value to total 2.3 end for 3. Output. 3.1 Output the total.
14
22 [count >= 6] Zeroise total Total = total + array[count] [count < 6] Output total Array = 10, 20, 30, 40, 50, 60 Zeroise counter Add 1 to count
15
//1. Get unknown data and create variables. //1.1 create an array with values 10, 20, 30, 40, 50, 60. //1.2 create and zeroise total. // 1.3 create and zeroise counter. //2. Processing. //2.1 count for six times //2.2 add current array value to total //2.3 end for //3. Output. //3.1 Output the total.
16
//1. Get unknown data and create variables. //1.1 create an array with values 10, 20, 30, 40, 50, 60. int [] number = {10, 20, 30, 40, 50, 60}; //1.2 create and zeroise total. int total = 0; // 1.3 create and zeroise counter. int count = 0; //2. Processing. //2.1 for six times for (count=0;count<6;count++) { //2.2 add current array value to total total = total + number[count]; //2.3 end for } // end for //3. Output. //3.1 Output the total. System.out.println("The total is " + total); Remember in Java the first element of an array is referred to as position zero.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.