week-05 - COMP 202 Week 5 Programming With Iterations...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
COMP 202 – Introduction to Computing 1 COMP 202 - Week 5 1 COMP 202 – Week 5 Programming With Iterations Learning outcome : •How to generalize multiple works to fit them in a single iterative task •Introducing iterative programming constructs – for, while, do-while loops
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP 202 – Introduction to Computing 1 COMP 202 - Week 5 2 Iterative programming • Example –Average of grades in 4 subjects?
Background image of page 2
COMP 202 – Introduction to Computing 1 COMP 202 - Week 5 3 Solution:1 Scanner kb = new Scanner(System.in); System.out.print(“Enter grade of subject 1: ”); int grade1 = kb.nextInt(); System.out.print(“Enter grade of subject 2: ”); int grade2 = kb.nextInt(); System.out.print(“Enter grade of subject 3: ”); int grade3 = kb.nextInt(); System.out.print(“Enter grade of subject 4: ”); int grade4 = kb.nextInt(); System.out.println(“Avergae grade” +(grade1+grade2+grade3+grade4)/4.0);
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP 202 – Introduction to Computing 1 COMP 202 - Week 5 4 Solution:2 Scanner kb = new Scanner(System.in); int sum = 0; System.out.print(“Enter grade of next subject : ”); sum += kb.nextInt(); System.out.print(“Enter grade of next subject: ”); sum += kb.nextInt(); System.out.print(“Enter grade of next subject: ”); sum += kb.nextInt(); System.out.print(“Enter grade of next subject: ”); sum += kb.nextInt(); System.out.println(“Avergae grade = “+sum/4.0); doing same thing 4 times
Background image of page 4
COMP 202 – Introduction to Computing 1 COMP 202 - Week 5 5 Solution:3 Scanner kb = new Scanner(System.in); int sum = 0; do 4 times { System.out.print(“Enter grade of next subject : ”); sum += kb.nextInt(); } System.out.println(“Avergae grade = “+sum/4.0);
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP 202 – Introduction to Computing 1 COMP 202 - Week 5 6 Solution:4 Scanner kb = new Scanner(System.in); int sum = 0; count from 1 to 4 and do each time { System.out.print(“Enter grade of subject ” + count + “: “); sum += kb.nextInt(); } System.out.println(“Avergae grade = “+sum/4.0);
Background image of page 6
COMP 202 – Introduction to Computing 1 COMP 202 - Week 5 7 Iterative programming • Example –Average of grades of unknown number of subjects? • Taking input will end if the user enters 0 as the grade
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP 202 – Introduction to Computing 1 COMP 202 - Week 5 8 Solution:5 Scanner kb = new Scanner(System.in); int sum = 0; int count = 0; int input = -1; as-long-as (input != 0) do { System.out.print(“Enter grade of subject ” + count + “: “); input = kb.nextInt(); sum += input; count = count+1; } System.out.println(“Avergae grade = “+sum/4.0);
Background image of page 8
COMP 202 – Introduction to Computing 1 COMP 202 - Week 5 9 Iterative programming • We talked about 2 types of iterations – – Do the following for N times or Count from 1 to N and do the following each time – Do the following as-long-as Condition is true • Java (and also C/C++) provides only the later kind of structure • Because, you can implement the first kind using the second kind
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP 202 – Introduction to Computing 1 COMP 202 - Week 5 10 The while Statement •T h e while statement has the following syntax: while ( condition ) statement ; while is a reserved word If the condition is true, the statement is executed.
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/28/2008 for the course COMP 202 taught by Professor Verbrugge during the Fall '07 term at McGill.

Page1 / 60

week-05 - COMP 202 Week 5 Programming With Iterations...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online