darrell bethea may 19 2011
play

Darrell Bethea May 19, 2011 1 Assignments: Homework 0 grades up - PowerPoint PPT Presentation

Darrell Bethea May 19, 2011 1 Assignments: Homework 0 grades up Program 1 due yesterday Program 2 due Monday Program 3 assigned today Updated yesterdays slides 2 3 Loops Body Initializing Statements


  1. Darrell Bethea May 19, 2011 1

  2.  Assignments: ◦ Homework 0 grades up ◦ Program 1 due yesterday ◦ Program 2 due Monday ◦ Program 3 assigned today  Updated yesterday’s slides 2

  3. 3

  4.  Loops ◦ Body ◦ Initializing Statements ◦ Ending a loop ◦ Bugs 4

  5. count = 1; while (count <= num) { System.out.print(count + “, “); count++; }  Repeated code  Write pseudocode and turn repeated statements into loops 5

  6.  Get user input  sum = sum + input Repeated  Get user input statements in  sum = sum + input pseudocode  Get user input become your  sum = sum + input loop  Average sum 6

  7.  Get user input  sum = sum + input

  8. sum = sum + input  Variables in your loop must be initialized (set to a value) before the loop  What is initialization of sum? 8

  9. sum = sum + input  Variables in your loop must be initialized (set to a value) before the loop  What is initialization of sum?  What if we wanted the product? ◦ sum = sum * input 8

  10.  If you know number of loop iterations?  Count-controlled loops  for(count = 0; count < iterations; count++)  User controlled ending  Ask-before-iterating  Sentinel value  Booleans 9

  11. for (count = 0; count < iterations; count++) { System.out.print(“I have iterated”); System.out.println((count + 1) + “ times”); }

  12. do { //do stu fg in your code here System.out.print( “Continue? yes/no”); answer = keyboard.next(); } while (answer.equalsIgnoreCase(“yes”)); 11

  13.  Signal end of input System.out.print(“enter a negative number to end the loop”); next = keyboard.nextInt(); sum = 0; while ( next >= 0 ) { � sum = sum + next; � System.out.print(“Enter a number: ”); � next = keyboard.nextInt(); } 12

  14. int next, sum = 0; boolean numbersLeft = true; Scanner keyboard = new Scanner(System.in); while (numbersLeft) { next = keyboard.nextInt(); if (next < 0) { numbersLeft = false; } else { sum = sum + next; } } System.out.print(“the sum is “ + sum); 13

  15. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: Value of count : Value of count2 : 13

  16. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: Value of count : 0 Value of count2 : 13

  17. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: Value of count : 0 Value of count2 : 13

  18. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: Value of count : 0 0 Value of count2 : 13

  19. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: Value of count : 0 0 Value of count2 : 13

  20. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: Value of count : 1 0 Value of count2 : 13

  21. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: Value of count : 1 0 Value of count2 : 13

  22. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: Value of count : 1 0 Value of count2 : 13

  23. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: Value of count : 1 0 Value of count2 : 13

  24. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: 0 Value of count : 1 0 Value of count2 : 13

  25. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: 0 Value of count : 1 1 Value of count2 : 13

  26. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: 0 Value of count : 1 1 Value of count2 : 13

  27. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: 0 Value of count : 2 1 Value of count2 : 13

  28. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: 0 Value of count : 2 1 Value of count2 : 13

  29. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: 0 Value of count : 2 0 Value of count2 : 13

  30. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: 0 Value of count : 2 0 Value of count2 : 13

  31. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: 0 0 Value of count : 2 0 Value of count2 : 13

  32. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: 0 0 Value of count : 2 1 Value of count2 : 13

  33. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: 0 0 Value of count : 2 1 Value of count2 : 13

  34. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: 0 0 Value of count : 2 1 1 Value of count2 : 13

  35. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: 0 0 Value of count : 2 1 2 Value of count2 : 13

  36. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: 0 0 Value of count : 2 1 2 Value of count2 : 13

  37. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: 0 0 Value of count : 3 1 2 Value of count2 : 13

  38. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: 0 0 Value of count : 3 1 2 Value of count2 : 13

  39. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: 0 0 Value of count : 3 1 0 Value of count2 : 13

  40. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: 0 0 Value of count : 3 1 0 Value of count2 : 13

  41. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: 0 0 Value of count : 3 1 0 Value of count2 : 0 13

  42. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: 0 0 Value of count : 3 1 1 Value of count2 : 0 13

  43. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: 0 0 Value of count : 3 1 1 Value of count2 : 0 13

  44. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: 0 0 Value of count : 3 1 1 Value of count2 : 0 1 13

  45. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: 0 0 Value of count : 3 1 2 Value of count2 : 0 1 13

  46. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: 0 0 Value of count : 3 1 2 Value of count2 : 0 1 13

  47. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: 0 0 Value of count : 3 1 2 Value of count2 : 0 1 2 13

  48. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: 0 0 Value of count : 3 1 3 Value of count2 : 0 1 2 13

  49. int count, count2; for (count = 0; count <= 3; count++) { for (count2 = 0; count2 < count; count2++) { System.out.println(count2); } } Output: 0 0 Value of count : 3 1 3 Value of count2 : 0 1 2 13

Recommend


More recommend