Habenero Java

Click here for the previous tutorial.

In Java, we can make use of loops in order to repeat procedures. This can save a lot of work from the programmers, as more work is alloted to the computer instead. Consider, if we wanted to print our "Hello World" message a thousand times, we could type all of the println statements, along with the argument they are to display. Alternatively, programmers would find it advantageous to instead create a program that simply prints it once, but loops one thousand times. This may sound difficult, but the syntax is actually very similar to the if statement we have already used. Instead of if, use the keyword while, and you have technically already made a loop. The only thing you need to worry about now, is killing the loop, otherwise, we might run the program without a kill command until it crashes.

while(booleanTest){
    statement;
}

do NOT run this as is. Currently, we do not have a mode of killing the script. All that needs to be done, is make something inside that would eventually set the test to false. For example, a common way of doing this is to make a while loop that will cycle a predetermined amount of times. We can make this by setting the test to something like (intCounter <= 5), and then ending the while loop with intCounter++;.

int intCounter = 0;
while(intCounter <= 5){
    System.out.println("The counter is at " + intCounter);
    intCounter++;
}

This then will check if intCounter is less than or equal to five, if true, it will print "The counter is equal to " and then the value of the counter. Then it will add one. It will check this again, and run through the script of the loop as many times as the test is true. Because the check is for <= 5, as long as intCounter is five or less, it will run the script. It will do this for each number until five, because each time it only increases by one.

The while loop does work with more than just integers, however. For example, if we wanted the user to enter an integer, we may use a while loop to check validity; so long as it was invalid, the loop would repeat and ask again.

int intInput;
boolean validInt = false;
while(validInt == false){
    scannerName = Scanner(System.in);
    validInt = scannerName.hasNextInt();
}
intInput = scannerName.nextInt();

This will constantly ask the user for a valid integer response, until one is eventually given. Then it will stop, and assign that value to intInput, which you can use in your code.

There are two additionaly types of loops in Java, the for and the do while. The for loop works very similarly to the while, with only a slightly different form: the variable is innitialized within the loop arguments, and the counter is determined. Say you wanted a loop that always looped five times, we could use the while loop like before, but the prefered way of accomplishing this is actually the for loop.

for(int counter = 0; counter <= 20; counter++){
    System.out.println("Counter is at " + counter);
}

This will count from zero to twenty, and document it along the way. There are very minimal differences between this and its while loop counterpart. Because of the explicit representation of cycles mentioned inside the for loop, it is prefered if the amount of cycles is known before hand. For example, a counter like the one shown should use a for loop, but it would not make sense to try and fit a for loop into our integer validity check.

Finally, the last type of loop is the do while. The do while loop works exactly like a while loop, except that it will always run through the script at least once, and check if the condition is met at the end.

do{
    System.out.println("Please enter a valid integer: ");
    scannerName = new Scanner(System.in);
}while(scannerName.hasNextInt());
intInput = scannerName.nextInt();

Click here to coninue to the next tutorial.