1 / 76

Programming Methodology (1)

Programming Methodology (1). Iteration. 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;

cybele
Download Presentation

Programming Methodology (1)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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: REPEAT 5 times { } System.out.println("*****"); System.out.println("*****"); System.out.println("*****"); System.out.println("*****"); System.out.println("*****"); * * * * * * * * * * * * * * * * * * * * * * * * * System.out.println("*****");

  6. The ‘for’ loop

  7. int i; for ( ; ; ) { } i = 1 i <= 5 i++ start counter test counter change counter // instruction(s) to be repeated go here System.out.println(“*****”);

  8. Variable Scope

  9. public static void main (String [] args) { } int x; x = 10; if ( /* some test */) { } int y; y = x+1; System.out.print(x); System.out.print(y); System.out.print(y);

  10. i 6 5 4 3 2 1 int i; for ( ; ; ) { } i = 1 int i = 1 i <= 5 i++ System.out.println(“*****”); ***** ***** RUN ***** ***** *****

  11. Different ways of using the loop counter

  12. for ( ; ; ) { } int i = 1 i <= 5 i ++ System.out.println(“*****”); ***** ***** ***** *****

  13. for ( ; ; ) { } int i = 1 i <= 5 i = i + 2 System.out.println(“*****”); ***** ***** ***** *****

  14. for ( ; ; ) { } int i = 1 i <= 10 i = i + 2 System.out.println(“*****”); ***** ***** ***** *****

  15. i 11 9 7 5 3 1 for ( ; ; ) { } int i = 1 i <= 10 i = i + 2 System.out.println(“*****”); ***** ***** RUN ***** ***** *****

  16. i 10 2 3 4 5 6 7 8 9 1 0 for ( ; ; ) { } int i = 10 i >= 1 i-- start counter test counter change counter // instruction(s) to be repeated go here System.out.println( i ); 10 9 8 RUN 7 6 5 4 3 2 1

  17. The body of the loop

  18. for ( ; ; ) { } int i = 1 i <= 3 i ++ System.out.println(“First line”); *****

  19. for ( ; ; ) { } int i = 1 i <= 3 i ++ System.out.println(“First line”); System.out.println(“Second line”); ***** First line Second line First line Second line First line Second line

  20. for ( ; ; ) { } int i = 10 i >= 1 i -- System.out.println( i ); ***** 10 9 8 7 6 5 4 3 2 1

  21. for ( ; ; ) { } int i = 10 i >= 1 i -- if ( i > 5 ) { System.out.println( i ); } 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.print("*"); System.out.print("*"); System.out.print("*"); System.out.print("*"); System.out.println( ); }

  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. Allowing the user to fix the size of the square * * * * * * * * *

  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. Running the program 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( ); while ( ) { } */ test goes here */ pin != 1234 System.out.print(“Invalid PIN, please re-enter: ”); pin = sc.nextInt( ); System.out.println(“Correct PIN, have some money! ”); Enter PIN: 1234 5555 Correct PIN, have some money! Invalid PIN, please re-enter: RUN 4321 Invalid PIN, please re-enter: 1234 Correct PIN, have some money!

  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( ); || while ( ) { } mark < 0 mark > 100 CHECK MARK HERE System.out.println(“Invalid mark: please re-enter“); 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");

  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 do { } while ( /*test goes here*/ ) // some code here ; // some code goes here

  46. import java.util.*; public class FindCost4 { public static void main(String[] args ) { double price, tax; Scanner sc = new Scanner(System.in); char reply; do { } // code for rest of program here 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

More Related