Download presentation
Presentation is loading. Please wait.
Published byCarrie Terrell Modified over 10 years ago
1
Programming Methodology (1)
2
Iteration
3
Learning objectives explain the term iteration; repeat a section of code with a for loop; repeat a section of code with a while loop; repeat a section of code with a do...while loop; select the most appropriate loop for a particular task; explain the term input validation and write simple validation routines.
4
When to use a loop?
5
Display a square of stars (five by five) on the screen as follows: * * * * * * * * * * * * * * * * * * * * * * * * * System.out.println("*****"); System.out.println("*****"); System.out.println("*****"); System.out.println("*****"); System.out.println("*****"); REPEAT 5 times { } System.out.println("*****");
6
The ‘for’ loop
7
for ( ; ; ) { } start countertest counterchange counter // instruction(s) to be repeated go here i = 1i++i <= 5 System.out.println(“*****”); int i;
8
Variable Scope
9
public static void main (String [] args) { } if ( /* some test */) { } int x; int y; x = 10; System.out.print(x); System.out.print(y); System.out.print(y); y = x+1;
10
for ( ; ; ) { } i = 1 i++i <= 5 System.out.println(“*****”); RUN ***** ***** ***** ***** ***** i 1 23456 int i; int i = 1
11
Different ways of using the loop counter
12
for ( ; ; ) { } i ++i <= 5 System.out.println(“*****”); ***** int i = 1
13
for ( ; ; ) { } i = i + 2i <= 5 System.out.println(“*****”); ***** int i = 1
14
for ( ; ; ) { } i = i + 2i <= 10 System.out.println(“*****”); ***** int i = 1
15
for ( ; ; ) { } i = i + 2i <= 10 System.out.println(“*****”); RUN ***** ***** ***** ***** ***** i 1 357911 int i = 1
16
for ( ; ; ) { } start countertest counterchange counter // instruction(s) to be repeated go here int i = 10i--i >= 1 System.out.println( i ); RUN 10 9 8 7 6 i 10 98765 5 4 3 2 1 43210
17
The body of the loop
18
for ( ; ; ) { } i ++i <= 3 System.out.println(“First line”); ***** int i = 1
19
for ( ; ; ) { } i ++i <= 3 System.out.println(“First line”); ***** int i = 1 System.out.println(“Second line”); First line Second line First line Second line First line Second line
20
for ( ; ; ) { } i --i >= 1 System.out.println( i ); ***** int i = 10 10 9 8 7 6 5 4 3 2 1
21
for ( ; ; ) { } i --i >= 1 if ( i > 5 ) { System.out.println( i ); } int i = 10 10 9 8 7 6
22
Nested loops
23
for (int i = 1; i <= 5; i++) { } System.out.println("*****");
24
for (int i = 1; i <= 5; i++) { } System.out.print("*"); System.out.println( ); System.out.print("*"); System.out.print("*"); System.out.print("*"); System.out.print("*");
25
for (int i = 1; i <= 5; i++) { } for (int j = 1; j <= 5; j++) { } System.out.print("*"); System.out.println( );
26
Allowing the user to fix the size of the square * * * * * * * * * * * * * * * * * * * * * * * * *
27
* * * * * * * * *
28
for (int i = 1; i <= 5 ; i++) { for (int j = 1; j <= 5 ; j++) { System.out.print("*"); } System.out.println( ); } System.out.println("Size of square?"); num = sc.nextInt(); num; i++) num; j++)
29
Running the program Size of square? 8 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
30
Size of square? 3 * * * * * * * * *
31
Non-fixed repetitions
32
The ‘while’ loop
33
3422
34
Error!
35
7343
36
Error!
37
1234
38
CASH
39
System.out.print(“Enter PIN: ”); pin = sc.nextInt( ); System.out.print(“Invalid PIN, please re-enter: ”); pin = sc.nextInt( ); System.out.println(“Correct PIN, have some money! ”); while ( ) { } */ test goes here */pin != 1234 RUN Enter PIN:1234 Correct PIN, have some money! 5555 4321 Correct PIN, have some money! Invalid PIN, please re-enter: Invalid PIN, please re-enter:1234
40
Input Validation
41
import java.util.*; public class DisplayResult { public static void main(String[] args) { int mark; Scanner sc = new Scanner (System.in); System.out.println("What exam mark did you get?"); mark = sc.nextInt(); if (mark >= 40) { System.out.println("Congratulations, you passed"); } else { System.out.println("I‘m sorry, but you failed"); } System.out.println("Good luck with your other exams"); } CHECK MARK HERE
42
System.out.println("What exam mark did you get?"); mark = sc.nextInt( ); if (mark >= 40) { System.out.println("Congratulations, you passed"); } else { System.out.println("I‘m sorry, but you failed"); } System.out.println("Good luck with your other exams"); CHECK MARK HERE System.out.println(“Invalid mark: please re-enter“); mark = sc.nextInt( ); while ( ) { } mark > 100mark < 0 ||
43
Sample Program Run What exam mark did you get? 101 Invalid mark: please re-enter -10 Invalid mark: please re-enter 10 I'm sorry, but you failed Good luck with your other exams
44
The ‘do…while’ loop
45
// some code here // some code here // some code goes here { } while ( /*test goes here*/ ) do ;
46
import java.util.*; public class FindCost4 { public static void main(String[] args ) { double price, tax; Scanner sc = new Scanner(System.in); } } // code for rest of program here char reply; System.out.print (“Enter another product (y/n)?: ”); reply = sc.next().charAt(0); { } while ( ); ? reply == ‘y’reply == ‘Y’ ||
47
Sample Program Run *** Product Price Check *** Enter initial price: 50 Enter tax rate:10 Cost after tax = 55.0 Enter another product (y/n)?: y *** Product Price Check *** Enter initial price: 70 Enter tax rate:5 Cost after tax = 73.5 Enter another product (y/n)?: n
48
Menu Driven Programs
49
*** Product Price Check *** [1] Enter Price [2] Enter tax [3] Calculate Cost [4] Quit Enter choice [1-4]: 1 Enter initial price:12.5
50
*** Product Price Check *** [1] Enter Price [2] Enter tax [3] Calculate Cost [4] Quit Enter choice [1-4]: 1 Enter initial price:100
51
*** Product Price Check *** [1] Enter Price [2] Enter tax [3] Calculate Cost [4] Quit Enter choice [1-4]: 2 Enter tax rate:12.5
52
*** Product Price Check *** [1] Enter Price [2] Enter tax [3] Calculate Cost [4] Quit Enter choice [1-4]: 3 Cost = 112.5
53
*** Product Price Check *** [1] Enter Price [2] Enter tax [3] Calculate Cost [4] Quit Enter choice [1-4]: 4 Goodbye!
54
Menu Driven Timetable Program
55
*** Lab Times *** [1] TIME FOR GROUP A [2] TIME FOR GROUP B [3] TIME FOR GROUP C [4] QUIT Enter choice [1-4]: 2 1.OOp.m
56
*** Lab Times *** [1] TIME FOR GROUP A [2] TIME FOR GROUP B [3] TIME FOR GROUP C [4] QUIT Enter choice [1-4]: 5 Options 1-4 only!
57
*** Lab Times *** [1] TIME FOR GROUP A [2] TIME FOR GROUP B [3] TIME FOR GROUP C [4] QUIT Enter choice [1-4]: 1 10.OOa.m
58
*** Lab Times *** [1] TIME FOR GROUP A [2] TIME FOR GROUP B [3] TIME FOR GROUP C [4] QUIT Enter choice [1-4]: 3 11.00a.m
59
*** Lab Times *** [1] TIME FOR GROUP A [2] TIME FOR GROUP B [3] TIME FOR GROUP C [4] QUIT Enter choice [1-4]: 4 Goodbye
60
// code to declare variables System.out.println(“[1] TIME FOR GROUP A”); System.out.println(“[2] TIME FOR GROUP B”); System.out.println(“[3] TIME FOR GROUP C”); System.out.println(“[4] QUIT”); System.out.print(“Enter choice [1-4]: “); choice =sc.next().charAt(0); switch(choice) { } case ‘1’: case ‘2’: case ‘3’: default: System.out.println(“10.00a.m”); System.out.println(“1.00p.m”); System.out.println(“11.00a.m”); System.out.println(“Options 1-4 only!”); { } choice != ‘4’ case ‘4’:System.out.println(“Goodbye”); // CODE TO DISPLAY MENU // CODE TO ENTER CHOICE // CODE TO PROCESS CHOICE do while( ); char choice; break; break; break; break;
62
public class IterationQ3 { public static void main(String[] args) { for(int i=1; i<=10; i++) { if (i%2 == 0) { System.out.println(i); } } } } 2 4 6 8 10 1 i =23 45 67891011
63
Assignment
64
Aaron’s Shop Guitar (£100) - How many?:
65
Aaron’s Shop Guitar (£100) - How many?: 2
66
Aaron’s Shop Guitar (£100) - How many?: 2 Total cost required?
67
Aaron’s Shop Guitar (£100) - How many?: 2 Total cost required? n
68
Aaron’s Shop Guitar (£100) - How many?: 2 Total cost required? n Piano (£900) - How many?:
69
Aaron’s Shop Guitar (£100) - How many?: 2 Total cost required? n Piano (£900) - How many?: 1
70
Aaron’s Shop Guitar (£100) - How many?: 2 Total cost required? n Piano (£900) - How many?: 1 Total cost required?
71
Aaron’s Shop Guitar (£100) - How many?: 2 Total cost required? n Piano (£900) - How many?: 1 Total cost required? y
72
Aaron’s Shop Guitar (£100) - How many?: 2 Total cost required? n Piano (£900) - How many?: 1 Total cost required? y Total cost so far = £1100
73
Aaron’s Shop Guitar (£100) - How many?: 2 Total cost required? n Piano (£900) - How many?: 1 Total cost required? y Total cost so far = £1100 Mike (£45) - How many?:
74
Aaron’s Shop Guitar (£100) - How many?: 2 Total cost required? n Piano (£900) - How many?: 1 Total cost required? y Total cost so far = £1100 Mike (£45) - How many?: 0
75
Using a for loop, write a program that displays a "6 times" multiplication table; the output should look like this: 1 6 = 6 2 6 = 12 3 6 = 18 4 6 = 24 5 6 = 30 6 6 = 36 7 6 = 42 8 6 = 48 9 6 = 54 10 6 = 60 11 6 = 66 12 6 = 72
76
Using a for loop, write a program that displays a "6 times" multiplication table; the output should look like this: 1 6 = 6 2 6 = 12 3 6 = 18 4 6 = 24 5 6 = 30 6 6 = 36 7 6 = 42 8 6 = 48 9 6 = 54 10 6 = 60 11 6 = 66 12 6 = 72
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.