Presentation is loading. Please wait.

Presentation is loading. Please wait.

Overview of Java Loops By: Reid Hunter. What Is A Loop? A loop is a series of commands that will continue to repeat over and over again until a condition.

Similar presentations


Presentation on theme: "Overview of Java Loops By: Reid Hunter. What Is A Loop? A loop is a series of commands that will continue to repeat over and over again until a condition."— Presentation transcript:

1 Overview of Java Loops By: Reid Hunter

2 What Is A Loop? A loop is a series of commands that will continue to repeat over and over again until a condition is met. For example, let's say you have the names of people in an array, and you wanted print a list of all of the names. You could setup a loop that would print the first persons name, and then move on to the next and print her name, etc (the continuing to repeat itself part.) The condition would be that it stops once all of the names have been used.

3 Simple Loop VS. Nested Loop A simple loop is the use of just one loop. Would be used if you only need to do something a certain number of times. A nested loop is the use of one loop inside of another loop. You would use this if you need to run something a certain number of times while running something else a certain number of times

4 Different Types of Java Loops “For Loops” “While Loops” “Do-While Loops”

5 Best Uses For Loop : When you need a simple loop to list something such as a println a certain number of times or extracting a certain group of numbers While Loop : when statements which the condition that was present on the first line is true Do-While Loop : when condition present on the last line is true

6 For Loops One of the easiest types of loops to establish and use in code. Usually uses a counter with a precise starting point and a precise ending point. iterate through a certain lines of code within the loop's limit as long as the loop condition is satisfied.

7 For Loops Cont’d Example: for(int i = 0; i <= 5; i++) // i starts@ 0 and stops@ 5 {System.out.print(i);} // prints every possible i value Output: 012345 Nested Loop Example: for (int i=1; i<=5; i++) // { System.out.println(); // moves down 1 line per number in loop for (int j=1; j<=i; j++) {System.out.print(j);} // progresses 1-5 as loop first loop goes 1 line down for ea value } Output: 1 12 123 1234 12345

8 While Loop Best used when statements which the condition that was present on the first line is true Slightly more complicated to use compared to for loop differs from the for loop in that it only contains a condition which gets tested at the beginning of the loop. if the condition evaluates to false, it will not be executed at all. called a pre-tested loop because the test is made before executing the sequence of statements contained within.

9 While Loop Examples int i = 1; while (i <= 5) { System.out.println ("count : "+ i); i++; } Output: count : 1 count : 2 count : 3 count : 4 count : 5 I int x=10; I I while (x>=1) I I { I System.out.print(x+ " "); I I x--; I I } I I Output: 10 9 8 7 6 5 4 3 2 1

10 Do-While Loops Most complicated out of the three types Best used when condition present on the last line is true executes the “do” part of loop at least once even before checking the condition. even if the loop condition is not satisfied the loop would execute once. useful for certain types of situations that need to run at least once.

11 Do-While Examples int i = 1; do { System.out.println ("count : " + i); i++; } while (i <= 5) Iint x=1; I I do { I I System.out.println(+x+" "); I Ix++; I I } I while (x<=10); I I Output: 1 2 3 4 5 6 7 8 9 10

12 Useful Websites for those Still Confused http://www.javacoffeebreak.com/articles/loopyjav a/index.htmlhttp://www.javacoffeebreak.com/articles/loopyjav a/index.html http://www.tutorialspoint.com/java/java_loop_con trol.htm http://java.sun.com/docs/books/tutorial/java/nuts andbolts/for.htmlhttp://java.sun.com/docs/books/tutorial/java/nuts andbolts/for.html http://java.sun.com/docs/books/tutorial/java/nuts andbolts/while.html


Download ppt "Overview of Java Loops By: Reid Hunter. What Is A Loop? A loop is a series of commands that will continue to repeat over and over again until a condition."

Similar presentations


Ads by Google