A loop is a set of instructions that will keep repeating what you asked it to do until it's reached its goal or a specific requirement is met. It will do what you will ask it to do while the requirement is true until it is false, which is the skeleton of a loop. Doing something that is true until it’s not, like I'm playing video games while I’m allowed to until I'm not , which is playing above 1 hour. This is a skeleton of a loop, but there are 3 types of loops that work in different sub-scenarios.
This is a for loop. A for loop will repeat the specified code until a condition is met. This loop is best used IF you know how many times you want a block of code to be used/executed.
1. Start with the statement for(), inside the brackets is where all the magic happens.
2. DIAGRAM A: This will determine what number the loop will start counting from. In our key example here we will be starting our count from 1.
3. DIAGRAM B: This is a checking/exit condition. The loop will check it then compare if your previously initialized variable meets those conditions, if it does, the loop enables the code to execute again, if not the code stops. In our key example that if the variable i is less than 5 the code can execute again.
4. DIAGRAM C: This is an increment condition. This is what will happen if the condition before is true. For example, since i=1, i will go up by one via i++. This is the final leg of your code. Add what code you want to execute during each loop. In our key example, it will just print the variable i, or the number it’s currently on.
OUTPUT: 0 2 4 6 8 10 : This is printing only even values from 1-10.
This is a while loop. A while loop takes a boolean condition, it will keep doing what code it has as LONG as the statement is true. This is helpful if you don’t know how long a specific code needs to be executed.
1. Start with the statement while(), inside the brackets is where all the magic happens.
DIAGRAM A: This is the condition you add, if the condition is true the code will execute if not, the code will go to the next line. The key example shows that the code will execute while it is less than 10.
3. Simply put what code you want to execute while the condition is true. In our key example, it shows that the variable i will be increased by 1 each time the condition is true.
It is usually recommended to make the condition false somehow within the code inside. For example the while loop is executed while i is less than 10, but make sure to have
a statement inside your code to increase by 1 each time so it eventually hits that condition.
IF NOT: THIS CAN CAUSE AN INFINITE LOOP!!!!
OUTPUT: 0 2 4 6 8 10 :
The sales are above average!
The sales are above average!
The sales are NOW NOT above average!
This program will determine if the sales are above average (above 10), it will print when it's above average, then print when the sales above average.
This is ado/while loop. A do/while loop is a variation of the while loop. It will execute the code once first and then look at the condition, if it’s true it’ll do the code again, if not, it will go to the next line.
1. Create the statement indicated above.
2. Within the curly brackets input the code you want.
3. Beside the second bracket, make your while() code, this code will execute if the condition you put in those brackets are true, AFTER the first execution.
4. Put in your condition there, in our example it will keep doing the code while the variable i is 1. BUT this only happened before the code was executed once.
This is a for each loop. A for each loop travels through a collection of items one by one. It isn’t monitored/activated through a condition, on its own it goes through each element individually, each time executing the code. Usually this is used in an array, where it goes through each element, and then executes the code for each element it goes through, it will stop automatically when every element is scanned. This is good, since IT NEVER GOES OUT OF BOUNDS.
Create a collection of items, this is usually an array, arrays can be found in the other subpages.
2. Create the statement for(). This is where your statements will lie.
3. DIAGRAM A: This is where the variable name goes, the variable type has to match what the array’s variable type is. If it is a collection of ints you declare an int. The variable itself will be the element it’s currently accessing. In our example, our variable is a String i, so when we start the loop, the variable = Dachshund, then the second time around a GoldenDoodle.
4. DIAGRAM B: This is the name of your collection/array. In our example, since our array is called “cars”, we put “cars” there.
STEP 5: In the curly brackets just add the code you want to execute each time, for our code, each time it accesses an element it will print it.
OUTPUT:
1. Ratatouille
2. Wall-E
3. Spirited Away
4. The Boy And The Heron
This program will list the movies in a numerical order. For each variable it accesses, it will use th count variable to print a number and the movie, after it will just increase count each time.