Loops in JAVA
Submitted by moazkhan on Monday, February 16, 2015 - 17:27.
Loops in JAVA
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. For loops 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
- }
- for(j=0;j<10;j++)
- {
- }
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;
- }
- While(i<10)
- {
- }
- do{
- //statement to be executed
- }
- while(condition to be tested);
Add new comment
- 58 views