1 2 3 4 5 int sum 0 int i 2 sum sum i while 6 7 8 9

This preview shows page 2 - 4 out of 5 pages.

1 2 3 4 5 int sum = 0; int i = 2; sum = sum + i; while ( ) { } 6 7 8 9 10 11 12 String words = "I must not "; int lineNumber = 1; while ( ) { } System.out.println(words + "swear."); System.out.println(words + "be messy."); System.out.println(lineNumber); Designing Loops – condition How many times (if any) should the loop body be executed? 1 2 3 4 5 int sum = 0; int i = 2; sum = sum + i; while ( i < 101 ) { } 6 7 8 9 10 11 12 String words = "I must not "; int lineNumber = 1; while ( lineNumber <= 100 ) { } System.out.println(words + "swear."); System.out.println(words + "be messy."); System.out.println(lineNumber); These two loops are currently infinite loops. They will execute forever! 11 Designing Loops - increment The increment will make the loop variable change so that the condition will eventually evaluate to false. 1 2 3 4 5 6 int sum = 0; int i = 2; sum = sum + i; while ( i < 101 ) { } 7 8 9 10 11 12 13 14 String words = "I must not "; int lineNumber = 1; while ( lineNumber <= 100 ) { } System.out.println(words + "swear."); System.out.println(words + "be messy."); System.out.println(lineNumber); i = i + 2; lineNumber = lineNumber + 1; //or lineNumber++; 12 while loop example 1 1 2 3 4 5 6 7 8 9 String words = "I must not "; int lineNumber = 1; System.out.println(words + "swear."); System.out.println(words + "be messy."); while ( lineNumber <= 100 ) { } lineNumber++; System.out.println(lineNumber); System.out.println("Done!"); 1 I must not swear. I must not be messy. 2 I must not swear. I must not be messy. 3 I must not swear. I must not be messy. 4 Done!
Image of page 2

Subscribe to view the full document.

CompSci 101 Lecture 12 3 13 while loop example 2 Write a loop which prints all the numbers which are exactly divisible by 9 between 681 and 712 (inc). 1 2 3 4 5 6 7 8 9 10 int firstNum = 681; int lastNum = 712; int i = firstNum; while ( i <= lastNum ) { if (i % 9 == 0) { System.out.print(i + " "); } i++; } System.out.println(); 684 693 702 711 while loop example 3 Complete the code which prints the sum of the positive whole numbers entered by the user. The loop stops when the user enters a negative number. 1 2 3 4 5 6 7 8 9 int sum = 0; System.out.print("Enter number: "); int num = Integer.parseInt( Keyboard.readInput()); while ( num >= 0 ) { sum = sum + num; System.out.print("Enter number: "); num = Integer.parseInt( Keyboard.readInput()); } System.out.println("Sum: " + sum); Enter number: 3 Enter number: 4 Enter number: -1 Sum: 7 15 example 3 alternative Complete the code which prints the sum of the positive whole numbers entered by the user. To stop the user enters a negative number.
Image of page 3
Image of page 4
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern