Presentation is loading. Please wait.

Presentation is loading. Please wait.

22 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

Similar presentations


Presentation on theme: "22 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems."— Presentation transcript:

1 22 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk Spring 2013 Week 7: loops

2 Overview Java Lab 6, Exercises 2 and 3 while statement for statement See Java for Everyone, Ch. 4 22 February 2013Birkbeck College, U. London2

3 JavaLab 6, Qu. 2 22 February 2013Birkbeck College, U. London3 ScoreGrade 90-100A 80-89B 70-79C 60-69D <60E Obtain a score of type int from the keyboard and print out the corresponding letter grade.

4 Solution to Qu. 2: first part import java.util.Scanner; public class QuizGrading { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Please type in the score: "); int score = in.nextInt(); … // more code here System.out.print( " The grade is: " +grade); } 22 February 2013Birkbeck College, U. London4

5 Solution to Qu. 2: second part char grade = ' '; if (score >= 90) {grade = 'A';} else if (score >= 80) {grade = 'B';} else if (score >= 70) {grade = 'C';} else if (score >= 60) {grade = 'D';} else {grade = 'E';} 22 February 2013Birkbeck College, U. London5

6 Diagram for Question 2 22 February 2013Birkbeck College, U. London6 508070 60 10090 >=A >= D >= C >= B E

7 JavaLab 6, Qu. 3 Obtain a year of type int from the keyboard. Print true if it is a leap year, otherwise print false. Usually years that are divisible by 4 are leap years. However, years that are divisible by 100 are not leap years, unless the year is also divisible by 400. 22 February 2013Birkbeck College, U. London7

8 Decision Tree 22 February 2013Birkbeck College, U. London8 divisible by 4? yesno not a leap year divisible by 100 ? leap year no yes divisible by 400 ? not a leap year no yes leap year

9 Two Paths through the Decision Tree a: (year%4 == 0); b: (year%100 !=0); c: (year%100 == 0) && (year%400 == 0); First path: a && b; Second path: a && c; 22 February 2013Birkbeck College, U. London9

10 Boolean Test for a Leap Year Solution: (a && b)||(a && c) Equivalent solution: a && (b||c) Proof of equivalence: check a = false (both are false) check a = true (both reduce to b||c) 22 February 2013Birkbeck College, U. London10

11 Solution to Qu. 3: first part import java.util.Scanner; public class LeapYear { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Please type in the year: "); int year = in.nextInt(); … // more code here } 22 February 2013Birkbeck College, U. London11

12 Solution to Qu. 3: second part boolean a = year%4 == 0; boolean b = year%100 != 0; boolean c = (year%100==0) && (year%400==0); if (a && (b||c)) { System.out.println("Leap year"); } else { System.out.println("Not a leap year"); } 22 February 2013Birkbeck College, U. London12

13 Syntax for the while Loop while (boolean condition) { statements } /* The statements are carried out while the condition tests true. */ 22 February 2013Birkbeck College, U. London13

14 Flowchart for the while Loop 22 February 2013Birkbeck College, U. London14 test condition true false execute statements

15 Example: compound interest double balance = 10000; int year = 0; while (balance < TARGET) { year++; double interest = balance*RATE/100; balance = balance+interest; } 22 February 2013Birkbeck College, U. London15

16 Access to Variables double balance = 10000; … while (balance < TARGET) { double interest = balance*RATE/100; … } /* The variable balance is declared outside the loop and is available inside and outside the loop. The variable interest is declared inside the loop and is only available inside the loop. A new version of the variable interest is created on each iteration. */ 22 February 2013Birkbeck College, U. London16

17 Compute Time to Double an Investment public class DoubleInvestment { public static void main(String[] args) { final double RATE = 5; final double INITIAL_BALANCE = 10000; final double TARGET = 2*INITIAL_BALANCE; double balance = INITIAL_BALANCE; int year = 0; /* while loop here */ System.out.println("Investment doubled after "+year+" years"); } 22 February 2013Birkbeck College, U. London17

18 Test Cases Use very simple test data to check that the while loop is correct. Eg. if RATE = 100.1%, TARGET = 2*INITIAL_BALANCE then the balance is slightly more than doubled at the end of the first year. Check the value of year on exiting the loop. 22 February 2013Birkbeck College, U. London18

19 while Loop Example 1 int i = 0, sum = 0; while(sum < 10) { i++; sum = sum+i; System.out.print(i+" "+sum+", "); } /* output: 1 1, 2 3, 3 6, 4 10, */ 22 February 2013Birkbeck College, U. London19

20 while Loop Example 2 int i = 0, sum = 0; while(sum < 10) { i++; sum = sum-i; System.out.print(i+" "+sum+", "); } /* output: 1 -1, 2 -3, 3 -6, 4 -10, … */ 22 February 2013Birkbeck College, U. London20

21 The for Loop for(int i = 1; i <=10; i++) { System.out.println(i); } /* Use a for loop when the number of iterations is known in advance. The for loop is count controlled. The while loop is event controlled. */ 22 February 2013Birkbeck College, U. London21

22 Parts of the for Statement for (int i = 0; i <= 10; i++) { … } /* i = 0: initialisation, executed once on entering the loop. i <= 10: condition to be checked before each iteration. i++: update executed after each iteration. */ 22 February 2013Birkbeck College, U. London22

23 Examples of for Loops 22 February 2013Birkbeck College, U. London23 Loopvalues of Icomment for(int i = 0; i <= 5; i++)0 1 2 3 4 5The loop is executed 6 times for(int i = 5; i >= 0; i--)5 4 3 2 1 0The value of i can decrease for(int i = 0; i < 9; i = i+2)0 2 4 6 8Step size not equal to 1 for(int i = 0; i != 9; i = i+2)0 2 4 6 8 …Infinite loop for(int i = 1; i <= 20; i = i*2)1 2 4 8 6Any rule for modifying i can be used for(int i = 0; i<str.length(); i++)0 1 … str.length()-1 Step through the characters in a string

24 Avoid Confusing for Loops double x, sum = 0; for(System.out.print(Input: ); in.hasNextDouble(); sum += x) { x = in.nextDouble(); } /* The compiler does not check whether the initialisation, condition and update are related. in.hasNextDouble() tests to see if the next input is a number. */ 22 February 2013Birkbeck College, U. London24

25 Sum and Average Value double total = 0; int count = 0; while (in.hasNextDouble()) { double input = in.nextDouble(); total = total+input; count++; } double average = 0; if (count > 0){average = total/count;} 22 February 2013Birkbeck College, U. London25

26 Counting Matches int upperCaseLetters = 0; // initialise a counter for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); //obtain the ith character in str if (Character.isUpperCase(ch)) //test for upper case { upperCaseLetters++; } /* Character.isUpperCase is a method in the class Character which is in the package java.lang. */ 22 February 2013Birkbeck College, U. London26


Download ppt "22 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems."

Similar presentations


Ads by Google