Presentation is loading. Please wait.

Presentation is loading. Please wait.

Module 4 Loops.

Similar presentations


Presentation on theme: "Module 4 Loops."— Presentation transcript:

1 Module 4 Loops

2 Sometimes it’s nice to do something over and over again
Say we ask the user for an even number. They give us the number 5. Wouldn’t it be nice if we could ask over and over again until they gave us what we asked for? We can use a while loop to repeat a section of code WHILE a certain condition is true. Code it!

3 Sometimes it’s nice to do something over and over again
While this condition is true, we repeat the code inside the curly brackets over and over again.

4 While loops There are three parts to every loop: int n = 0; while(n <= 3) { System.out.println(n); n++; } 1) Loop variable initialization 2) Conditional Statement 3) Loop variable update

5 While loops n So what does this loop do? int n = 0; while(n <= 3) { System.out.println(n); n++; }

6 While loops n So what does this loop do? int n = 0; while(n <= 3) { System.out.println(n); n++; } Output:

7 While loops n 1 So what does this loop do? int n = 0; while(n <= 3) { System.out.println(n); n++; } Output:

8 While loops n 1 So what does this loop do? int n = 0; while(n <= 3) { System.out.println(n); n++; } Output: 1

9 While loops n 2 So what does this loop do? int n = 0; while(n <= 3) { System.out.println(n); n++; } Output: 1 2

10 While loops n 3 So what does this loop do? int n = 0; while(n <= 3) { System.out.println(n); n++; } Output: 1 2 3

11 So this loop prints out the numbers 0 through 3
While loops n 4 So what does this loop do? int n = 0; while(n <= 3) { System.out.println(n); n++; } Output: 1 2 3 So this loop prints out the numbers 0 through 3

12 Now it’s your turn! Use a while loop to print all the numbers from 1 to 100 to the screen. One number per line. Notice how it’s no more work for you to print 100 numbers to the screen than it was to print 4 numbers to the screen.

13 What is the output? Socrative
What is the output of the following code? This is what is known as an infinite loop. Since we start above the baseline and count up, our conditional statement will ALWAYS be true.

14 What about this one? Socrative
What is the output of the following code? This loop will not run at all. The conditional statement is false, so the loop gets skipped and the update statement never occurs.

15 Print positives Use a while loop to keep asking the user for numbers until they enter a zero. Print each number the user enters AS LONG AS IT IS POSITIVE (do not print any negative numbers or zero). So for example, if the user typed: You would print out: 5 3 1 Hint – start by printing out all the numbers the user enters. Once you get that working, then try and print out only the numbers you want.

16 Print positives

17 Average Now let’s write a program that finds the average of a bunch of numbers. Ask the user to enter numbers, entering zero when done Use a while loop to sum up the numbers and keep track of how many there are Divide the sum by the count to get the average Code it!

18 Careful or you run into integer division here!
If you don’t check, you might end up dividing by zero! Careful or you run into integer division here!

19 ReverseString Ask the user for a String.
Print out the string in reverse order. Do this by starting at the last letter of the String and use a while loop to “walk through” the String, index by index until you get to the beginning of the String. Hint: recall the String methods length() and charAt() Example: If the user enters “Hello There”, you would want to display “erehT olleH”

20 ReverseString

21 Do-While Loops Do-while loops are very similar to while loops.
The main difference is that a do-while loop ALWAYS executes at least once.

22 Remember what a While loop looks like?
There are three parts to every loop: int n = 0; while(n <= 3) { System.out.println(n); n++; } 1) Loop variable initialization 2) Conditional Statement 3) Loop variable update

23 Here’s what a Do-While loop looks like:
There are three parts to every loop: int n = 0; do { System.out.println(n); n++; } while(n <= 3); 1) Loop variable initialization 3) Loop variable update Notice the ; 2) Conditional Statement

24 Let’s walk through this do-while
n int n = 0; do { System.out.println(n); n++; } while(n <= 3); Output:

25 Let’s walk through this do-while
n int n = 0; do { System.out.println(n); n++; } while(n <= 3); 1 Output: 1

26 Let’s walk through this do-while
n int n = 0; do { System.out.println(n); n++; } while(n <= 3); 2 Output: 1 2

27 Let’s walk through this do-while
n int n = 0; do { System.out.println(n); n++; } while(n <= 3); 3 Output: 1 2 3

28 Let’s walk through this do-while
Notice how this works the same way as the while loop except that the order the code is executed has changed slightly. n int n = 0; do { System.out.println(n); n++; } while(n <= 3); 4 Output: 1 2 3

29 Now it’s your turn! Use a do-while loop to print all the numbers from 1 to 100 to the screen. One number per line.

30 Remember, do-while loops ALWAYS run at least once!
What is the output? Socrative Nothing 1 1 through 100 Something else Remember, do-while loops ALWAYS run at least once!

31 How about this one? Socrative
If you forget the update statement for any type of loop, you can run into an infinite looping situation.

32 Do-while loops make great game loops!
Scanner kb = new Scanner(System.in); char playAgain = ‘n’; do { //game code System.out.print(“Do you want to play again? “); playAgain = kb.nextLine().toLowerCase().charAt(0); } while(playAgain == ‘y’); If you start a game, you probably want to play it at least once. After playing, you can ask if the user wants to play again or not.

33 Your turn Take a look at your Rock/Paper/Scissors game
Use a do-while loop to allow the user to play over and over again.


Download ppt "Module 4 Loops."

Similar presentations


Ads by Google