answersLogoWhite

0

What is the different between for and while loop?

Updated: 8/19/2019
User Avatar

Wiki User

13y ago

Best Answer

A while loop runs until a sentinel value is met, i.e. I have a empty array.

A for loop is a specialized while loop, where there is a sentinel initialization, a while check, then sentinel modification.

A for loop can always be written using a while, but the other way around doesn't always work.

i.e.

for (int k = 0; k < 100; k++).....

can be written as

int k = 0;

while (k < 100){ ...... k++;}

but whiles like this cannot be written with for loops without major modification.

boolean done = false;

while( ! done)

{

//Blah blah blah

if ( someCondition) done = true;

}

User Avatar

Wiki User

16y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

The coding syntax is the only difference between the two loops. Their features and functionalities are the same.

Ex While loop:

int x = 8;

while (x > 8) {

System.out.println("in the loop");

x = 10;

}

Ex for loop:

for (int i = 0; i<10; i++) {

System.out.println("i is " + i);

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the different between for and while loop?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the difference between while loop and for loop in oracle?

You mean PL/SQL? Well, they are different things, read the manual for details.


What is main different between do while and while?

If you want to execute a statement which is in while loop at least one time you can use do- while loop. this is why because initially first statements will be executed then condition will be checked. but in case of while first condition will be check then statements will be executed


What is a loop type?

A Loop is a programming language construct that instructs the processor to repeat a sequence of operations a number of times until a specific condition is reached. There are different types of loops. They are: * for loop * while loop * do while loop


What is the difference between a do while loop and a for loop in c?

the counter variable cannot be initialized in while loop before entering into the block.


How is the first loop in the circulatory system of an adult amphibian different from the second loop?

Tadpole has two chambered heart and a single-loop system while the adult has a three chambered heart and a double-loop system


Can while loop and for loop give different outputs?

Sure. Here is an example: for (i=0; i&lt;3; ++i) printf ("I am for loop, i=%d\n"); i=1; while (i&gt;0) { printf ("I am while loop, i=%d\n"); i &lt;&lt;= 1; }


What is the Difference between while and do while?

Both are programming commands. A do/while loop will execute at least once. A while loop may not execute at all.


Why you need a for loop when you have already while loop or do while loop?

We need a for loop because the while and do-while loops do not make use of a control variable. Although you can implement a counter inside a while or do-while loop, the use of a control variable is not as self-evident as it is in a for loop. Aside from the use of a control variable, a for loop is largely the same as a while loop. However, it is quite different to a do-while loop, which always executes at least one iteration of the loop before evaluating the conditional expression. In a for and while loop, the conditional expression is always evaluated before entering the loop, which may result in the loop not executing at all.


What is the difference between if and while loop?

Easy: if-else is not a loop; while, for and do-while are loops.if-else just run once, but do-while run many times.


Difference between for and while loop not in syntactically?

Well 'while' goes like this: while (condition) statement 'for': for (initialize; condition; after-each-loop) statement


Difference between while and do while loops in java?

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


List out the differences between while and dowhile statement?

The do ..while loop is executed at least once, whereas the while loop may not be executed even once.