Java Java intro
  1. Java intro
  2. Java basics
Java core
  1. Java variables
  2. Java conditionals
  3. Java loops
  4. Java arrays
  5. Java strings
Object-oriented Java
  1. Java OOP 1
  2. Java OOP 2
  3. Java packages
  4. Java interfaces
Java interactive
  1. Java user input
  2. Java exceptions
  3. Java events
Graphical Java
  1. Java GUI
  2. Java GUI layout
  3. Java graphics
Java extras
  1. Java applets
  2. Java sounds
  3. Java random numbers
Java wrap-up
  1. Java summary

Java loops

Imagine you have to write a program which performs a repetitive task such as printing 1 to 100. Coding 100 lines to do this would be mundane. There has to be an easier way, right? This is where loops come into the picture. Loops are specifically designed to perform repetitive tasks with one set of code. Loops save a lot of time.

This tutorial focuses on:

The for loop

The for loop is used to repeat a task a set number of times. It has three parts.

Syntax:
for(int a_variable = initial_value; a_variable < end_value; a_variable_increment){ code to be executed; }
Example:
for(int a = 1; a < 11; a++){ System.out.print(a + " "); }
Output:
1 2 3 4 5 6 7 8 9 10

Lets take apart the loop from the above example to see what each part does.

The above loop will execute the code between the braces 10 times because a begins at 1 and ends at 10. This counter is what runs the loop.

The while loop

The while loop works differently than the for loop. The for loop repeats a segment of code a specific number of times, while the while loop repeats a segment of code an unknown number of times. The code within a while loop will execute while the specified condition is true.

Syntax:
while(condition is true){ execute this code; }
Example:
int num = 0; while(num < 25){ num = num + 5; System.out.print(num + " "); }
Output:
5 10 15 20 25

In the above code, a variable named num is initialized with the value of 0. The condition for the while loop is that while num is less than 25, 5 should be added to num and it should be printed (together with a single space). Once the value of num is greater than 25, the loop will stop executing.

The do-while loop

The do-while loop is very similar to the while loop, but it does things in reverse order. The while loop - while a condition is true, perform a certain action, the do-while loop - perform a certain action while a condition is true. Also, the code within a do-while loop will always execute at least once, even if the specified condition is false. This is because the code is executed before the condition is tested.

Syntax:
do{ execute this code; } while (condition);
Example:
int num = 5; do{ num = num + 2; System.out.print(num + " "); } while (num < 25);
Output:
7 9 11 13 15 17 19 21 23 25

In the above code, a variable named num is initialized with the value of 5. The condition for the do-while loop is that 2 should be added to num and it should be printed (together with a single space) while num is less than 25. Once the value of num is greater than 25, the loop will stop executing.

Preventing endless loops

A necessary precaution when working with a loop is to make sure that the loop is not endless. To prevent endless loops, you need to ensure that the condition in a loop will eventually become false.

Endless for loop example:
//as long as a > 5 run the loop, but a will always //be greater than 5 in this loop! for(int a = 10; a > 5; a++){ System.out.println(a); }
Endless while loop example:
int num = 50; //as long as num > 10 run the loop, but num will always //be greater than 10 in this loop! while(num > 10){ num = num + 5; System.out.print(num); }
Endless do-while loop example:
int num = 50; do{ num = num + 5; System.out.print(num); } //as long as num > 40 run the loop, but num will always //be greater than 40 in this loop! while (num > 40);

Breaking out of a loop

You can completely break out of a loop when it is still running. This is achieved with the break keyword. Once a loop is exited, the first statement right after it will be executed. The break keyword provides an easy way to exit a loop if an error occurs, or if you found what you were looking for.

Example:
for(int a = 1; a < 10; a++){ System.out.print(a + " "); if(a == 5) {break;} } System.out.print("You have exited the loop");

In the above example, the for loop is set to iterate 9 times and print the current value of the variable a during each iteration. The if statement within the loop states that when the variable a is equal to 5, break out of the loop.

Output:
1 2 3 4 5 You have exited the loop

Continuing a loop

While you can break out of a loop completely with the break keyword, there is another keyword used when working with loops - the continue keyword. Using the continue keyword in a loop will stop the loop at some point and continue with the next iteration of the loop from the beginning of it.

Example:
for(int a = 1; a < 10; a++){ if(a == 5){continue;} System.out.print(a + " "); }

In the above example, the for loop is set to iterate 9 times and print the current value of the variable a during each iteration. The if statement within the loop states that when the variable a is equal to 5, stop the loop and continue with the next iteration of the loop from the beginning of it. For this reason, all the numbers except the number 5 are printed.

Output:
1 2 3 4 6 7 8 9

NOTE: For the continue keyword to work properly, the conditional statement needs to come first just like in the above example.

© Copyright 2013-2014 Landofcode.com
Terms of use | Privacy policy | Copyright information