1 / 11

Looping

Looping. I bet you can’t code just one. Why loops?. Many times in code just like in life we need to do things repeatedly. Loops save us from having to right out repetitious code. Loops allow us to do things repeatedly for a specific set amount or until some condition changes.

valadez
Download Presentation

Looping

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. Looping I bet you can’t code just one.

  2. Why loops? • Many times in code just like in life we need to do things repeatedly. • Loops save us from having to right out repetitious code. • Loops allow us to do things repeatedly for a specific set amount or until some condition changes. • Loops allow us to implement algorithms.

  3. For Loops • When you want to execute code for a specified number of times, you use a for loop. • Some examples of this in life in real life are: • Peeling 10 potatoes • Walking 10 miles • Writing 10 letters • There are three key components to a for loop: • Initialization • Terminating Condition • Increment/Decrement or Change

  4. For Loop Syntax for(Initialization; Terminating Condition; Change){ code } Ex: for(int x = 1; x <= 10; x++){ System.out.println(“This will print ten times”); }

  5. For Loop Syntax • Here is another example of a for loop and the output it would produce: Ex: for(int x = 20; x >= 4; x-=2){ System.out.print(x + “, “); } Output: 20, 18, 16, 14, 12, 10, 8, 6, 4, • Notice that the loop variable x is initialized to 20, the loop continues while x remains greater than 4 and x is decremented by 2 each time

  6. For Loop Syntax • We can rewrite that code in the following way: Ex: intnum = 20; for(int x = 1; x <= 9; x++){ System.out.print(num + “, “); num -= 2; } Output: 20, 18, 16, 14, 12, 10, 8, 6, 4, • Notice that the loop code is set up to execute 9 times, num is initialized to 20 before the loop starts, and each time through num is decremented by 2

  7. While Loop • When you want to execute code until a condition changes, you use a while loop. • Some examples of this in life in real life are: • Peeling potatoes until the potato bucket is empty • Walking until you arrive at school. • Writing until you have finished writing you book. • There is one main component to a while loop: • Terminating Condition • But you still have the other two components play a factor as well: • Initialization • Increment/Decrement or Change

  8. While Loop Syntax Initialization; while(Terminating Condition){ code Change } Ex: int x = 1; while(x <= 10){ System.out.println(“This will print ten times”); x++; }

  9. While Loop Syntax Ex: Robot bob = new Robot(); while(bob.frontIsClear()){ bob.move(); } while(bob.nextToABeeper()){ bob.pickBeeper(); }

  10. Nesting Loops while(bob.frontIsClear()){ bob.move(); while(bob.nextToABeeper()){ bob.pickBeeper(); } } • Take note that in the previous slide bob moved forward until he hit the wall then he picked up all the beepers at his location. Here bob will pick up all the beepers on the way to the wall.

  11. Nesting Loops for(int x = 1; x <= 2; x++){ for(int n = 3; n <= 9; n += 3){ System.out.println(x + “->” + n); } } Output: 1->3 1->6 1->9 2->3 2->6 2->9

More Related