week-05-handout

week-05-handout - 1 COMP 202 Introduction to Computing 1...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 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 COMP 202 Introduction to Computing 1 COMP 202 - Week 5 2 Iterative programming Example Average of grades in 4 subjects? 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); 2 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 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); 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); 3 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 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); 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 4 COMP 202 Introduction to Computing 1 COMP 202 - Week 5...
View Full 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 / 20

week-05-handout - 1 COMP 202 Introduction to Computing 1...

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

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