In this tutorial you will learn:
1) Why do we need loops in JAVA?
2) For loop in JAVA
3) While loop in JAVA
4) Do while loop in JAVA
5) Continue and Break Statement in JAVA
Why do we need loops?
Loops allows you to execute a statement or block of statements repeatedly. For loop is used when the number of iterations are already known. The for loops keep executing until the specific condition is met and when the certain condition is met the execution terminates.
Unlike C programming there are 2 type of for loops in JAVA which differ in increment method.
Three parts of control of for loop
- Initialization, Test, Iteration ( Increment/decrement)
Execution starts from the initialization statement that is executed only once. Test condition is evaluated after each statement. If test condition is true, body of for loop gets executed. Iteration expression is evaluated before the test condition is evaluated again.
Syntax of for loop type1:
This is the ordinary loop just like used in C programming.
- for(initial value; condition to be satisfied; increment/decrement)
- //statements to be executed
Example of for loop type1:
This loop will start from 0 and keep on executing to j equal to 9. J has to be declared as int. The output of this loop will be numbers from 0 to 9.
Syntax for for loop 2:
This loop is used for the 1D array, it automatically finds the size of array and keep on executing the statements. Iterations are equal to the size of the array.
Hence it will print out all the entries of the array.
While loop in JAVA
While loop executes as long as the test expression between parentheses is true. The statements of the loop may not execute at all in a while construct. Make sure the statements within the body of the while loop eventually result in making the test condition as false
- while(condition to be tested)
- //here we write the statements which are executed until the condition is false;
Do while loop in JAVA
Similar to while loop except that loop block is executed at least once, even if the test condition is false from the start.This is because the test condition is executed at the end of the loop block.
Syntax is given as:
- //statement to be executed
- while(condition to be tested);
Continue and Break Statement in JAVA
Continue is used if you want to skip all or part of body of the loop for a loop iteration. Continue stop executing the loop and takes you to the next iteration of the loop in which it finds itself. Note that if is not a loop but a decision. Continue statement can appear anywhere in the body of the loop. You can have more than one continue in the body of a loop.
Labeled break Statement in case of nested loops, it enables you to stop executing the inner loop and jump to the statement following the end of any enclosing statement block or loop that is identified by the label in the labeled statement It happens regardless of how many levels there are. Used to break out of a deeply nested set of loops in an easy manner. The continue and break statements enables us easy reading and debugging.
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.