110 likes | 156 Views
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.
E N D
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. • Loops allow us to implement algorithms.
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
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”); }
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
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
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
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++; }
While Loop Syntax Ex: Robot bob = new Robot(); while(bob.frontIsClear()){ bob.move(); } while(bob.nextToABeeper()){ bob.pickBeeper(); }
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.
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