Features wise they are both the same. The difference lies in the way they are implemented.
while Loop
The while loop is good for scenarios where you don't know how many times a block or statement should repeat, but you want to continue looping as long as some condition is true. A while statement looks like this:
while (expression) {
// do stuff
}
In this case, as in all loops, the expression (test) must evaluate to a boolean result. The body of the while loop will only execute if the expression (sometimes called the "condition") results in a value of true. Once inside the loop, the loop body will repeat until the condition is no longer met because it evaluates to false.
Any variables used in the expression of a while loop must be declared before the expression is evaluated. In other words, you can't say
while (int x = 2) { } // This is not legal
The key point to remember about a while loop is that it might not run at all. If the test expression is false the first time the while expression is checked, the loop body will be skipped and the program will begin executing at the first statement after the while loop. Look at the following example:
int x = 8;
while (x > 8) {
System.out.println("in the loop");
x = 10;
}
System.out.println("past the loop");
Running this code produces
past the loop
Because the expression (x > 8) evaluates to false, none of the code within the while loop ever executes
The for loop is especially useful for flow control when you already know how many times you need to execute the statements in the loop's block. The for loop declaration has three main parts, besides the body of the loop:
• Declaration and initialization of variables
• The boolean expression (conditional test)
• The iteration expression
The three for declaration parts are separated by semicolons. The following two examples demonstrate the for loop. The first example shows the parts of a for loop in a pseudocode form, and the second shows a typical example of a for loop.
for (/*Initialization*/ ; /*Condition*/ ; /* Iteration */) {
/* loop body */
}
Ex:
for (int i = 0; i<10; i++) { System.out.println("i is " + i); }
You use loops in Java when you want a set of actions to be repeated until a particular condition is met or for a certain number of times.The different types of loops in Java are:For LoopsDo-While LoopsWhile Loops
Java has three kinds of loops 1. For Loop 2. While Loop 3. Do - While Loop Both For loop and While loop would iterate through a certain lines of code within the loop's limit as long as the loop condition is satisfied. A do while loop would execute the loop once even before checking the condition. So in a do while loop, even if the loop condition is not satisfied the loop would execute once. Example Declarations: for(int i = 0; i < n; i++) { ..... } while (i < n) { ... i++; } do { ... i++; } while (i < n) ;
There are 4 different looping statements in Java. They are:While loopDo-While loopFor loopFor Each loop
A nested loop is a (inner) loop that appears in the loop body of another (outer) loop. The inner or outer loop can be any type: while, do while, or for. For example, the inner loop can be a while loop while an outer loop can be a for loop.
The most important differences are: a. The while loop starts with a condition whereas the condition is the line of code in case of a do while loop b. The do while loop is guaranteed to run the loop body atleast once even if the condition is an impossible to satisfy but such a guarantee is not available with the normal while loop
1) use for loop 2) do while loop
All loops available in Java (for, while, do-while) have a loop termination condition that would get executed during every iteration of the loop. Without checking the loop condition the loop cannot be terminated and hence avoiding the loop condition check during iteration is not logic and Java does not do it.
The contents of a while loop will always execute if the statement between the while parentheses resolves to a boolean true. Hence, if you place "true" as the while statement, the loop will execute forever (until the loop is forcefully broken or the computer or process shuts down).
The do while loop is also called an exit condition loop in c, c++, and java.
ComparisonThe conditions for both the 'while' and 'for' loop are exactly the same, but in each flow structure the conditions are placed in different locations. A 'for' loop places the full condition within the 'for' loop whereas the 'while' loop places the counter variable outside of the loop.The 'for' loop is used when the length of the loop is known whereas the 'while' loop is usually used when the length of the loop is unknown.'for' loop exampleWithin the parentheses the complete condition is contained. The condition has 3 parts delimited by a colon ';'. The first part sets the counter 'int i' to 0, the second part tells the loop to stop when the counter is 5. The third part tells java to increment the counter by 1 value each time the loop iterates.for ( int i = 0; i < 5; i++)'while' loop exampleWith the 'while' loop the counter is initialized outside of the 'while' loop. Inside the parentheses of the loop the condition is set to stop looping when the counter reaches a value of 5. Inside of the 'while' loop block the counter is set to increment by 1 value each time the loop iterates.int i = 0;while ( i < 5 ) {i++}
The control structures used in java script are if-statement, for-loop, for-in loop, while loop,do-while loop, switch-statement, with-statement. try-catch-finally statements.
You may exit a nested loop in Java using a break with a label for the outer loop.