Tuesday, 28 February 2012

Looping (Short and Brief)

Looping is a very useful tool inside java programming, and it's a way of accomplishing more tasks in less time. A scenerio where looping would be the most appropriate to use looping would be if you were to design a program that will read in 100 numbers, and are required to keep a running total (adding all of those numbers together). Without looping, this would take very long. But, with looping, you can accomplish this in minimal lines. It would look something like this:

import java.util.Scanner;

class RunningTotal{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int numberOfNumbers = 100;
int total = 0;
for(int i = 0; i < numberOfNumbers; i++){
int number = input.nextInt();
total = total + number;
}
System.out.println("your total is: " + total);
}
}

And that would be it. The loop shown above is known as a simple for loop. The type of loop that a for loop is is a counter-controlled loop because it only loops based on numbers. So, in the code above, we constructed a for loop to loop through 100 times. Each time it loops through, it will read in a number from the user and add it on to the total. When it's done looping, it will then break out of the loop and print out your total. If you were to write this program without looping, it would take very long and it wouldn't be very code conservative. Code conservation refers to the fact that you should use minimal lines in order to accomplish the most tasks. Looping is one way you can be code conservative.

There are 3 types of looping: counter-controlled looping, timer-controlled looping, and condition-controlled looping.

Timer-controlled looping means that it will execute every so often. An example: add a number after n seconds. I will be teaching timer-controlled looping later on inside these tutorials/tips, because at this very moment, it's not considered to be basic.

Condition-controlled looping means that you will loop through until the condition is NOT met. This means that while the condition is being met, it will keep looping. An example of this type of looping would be the while loop. Let's say you had a boolean (for those of you that don't know what a boolean is, a boolean is a statement which holds true or false, those are the only possible values for a boolean), and you set it equal to true. So it would look something like this:

boolean flag = true;
while(flag == true){
//body
}

There's only one problem with the code above, it's known as an infinite loop. This means that it will keep looping on forever because flag is always equal to true. In order to stop this, while you're performing your tasks, you would have to add in a "flag = false" inside your loop in order for the loop to break out of itself. An example of this would be something like this:

boolean flag = true;
int number = 0;
while(flag == true){
number = number + 1;
if(number == 10){
flag = false;
}
}

So, in the program above, it will keep looping through adding one to the value of number. When the number equals 10, it sets flag equal to false, meaning that the loop will not execute anymore because the condition is not being met. The only way that loop executes is only if the boolean flag is equal to true. When the number equals 10, you are setting the flag equal to false, causing this loop not to execute anymore. When a loop breaks out of itself, it goes on to the code after the last body bracket, if there is any. (It basically just continues the rest of your program).

No comments:

Post a Comment