answersLogoWhite

0


Best Answer

The main difference comes into picture when you use continue with them i.e. for and while.

In a while loop if continue is used before the increment of a variable is done it converts into a infinite loop.

i=1;

while(i<10)

{

/* do stuff */

if(i==6);

continue;

i++;

}

The above piece of code will turn into an infinite loop.

for(i=1;i<10;i++)

{

/* do stuff */

if(i==6);

continue;

}

In the above for loop the value of will be incremented once it attains the value 6.

Therefore it will not turn into a infinite loop.

Cheers.

astvansh> 'for' statement takes the start condition, stop condition, and the increment. 'while' statement just takes the stop condition.

One scenario (which i can think of) where while 'needs' to be used rather than a for, would be when the counter needs to be incremented/decremented conditionally...

string[] arr = {"a", "b", "b", "c"};

int i = 0;

while( i< 10)

{

// do something constructive

if(arr[i] == "b")

{

i = i + 2;

}

else

{

i++;

}

}

Cheers,

Ajeesh

Another main difference between the two: a 'for' loop is called a determinate loop, meaning that we usually use it when we know ahead of time how many iterations we want. The 'while' loop is an 'indeterminate' loop, because we usually use it in situations where we do not know the number of times the loop may iterate.

User Avatar

Wiki User

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

Wiki User

14y ago

Typically a for loop is used when you know how many iterations the loop will go through.

A while loop is used when a boolean expression is used to determine when to end the loop.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Which is more good to use for loop or while loop?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Compare While with for Statements?

The while loop is the more general one because its loop condition is more flexible and can be more complicated than that of a for loop. The condition of a for loop is always the same and implicit in the construction. A for loop stops if there are no more elements in the collection to treat. For simple traversals or iterations over index ranges it is a good advice to use the for statement because it handles the iteration variable for you, so it is more secure than while where you have to handle the end of the iteration and the change of the iteration variable by yourself.


For loop -uses?

You can use a for loop whenever you can use a while loop; it's the same.


Is it possible to use a for loop in place of a while loop?

Yes. while loop consist of only condition statement to make for loop look as while loop we can use syntax shown below: for(;condition;) eg: for(;i&lt;=n;)


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.


Why you use for loop inside switch statement?

Because you have to repeat something. (Or you can use while-loop, too.)


What type of loop use a Boolean expression to control the number of times that it repeats a statement set of statements?

A counted loop. Typically we use a for loop for counted loops: // loop 10 times... for (int i=0; i&lt;10; ++i) { // ... } We can also use while and do-while loops to do the same thing, however a for loop provides all the information up front where it belongs and we can localise the control variable. With while and do-while loops, the control variable must be declared outside the loop, and the increment is usually specified at the end of the loop. This makes while and do-while loops harder to read because the information that controls the loop is separated: // loop 10 times... int i = 0; while (i&lt;10) { // ... ++i; } A do-while loop is similar to a while loop, but the control expression is placed at the end of the loop thus the loop always executes at least once. This also upsets the logic of a counted loop because the control variable is off-by-one. // loop 10 times... int i = 0; do { // ... ++i; } while (i&lt;11);


Is possible to use a for loop in place of a while loop?

Yes, it's easy:Old: while (expression)statementNew: for (; expression;)statement


How can you repeatedly execute a code in java script?

1) use for loop 2) do while loop


How do you use switches in c?

do WHILE loop defination


What is the difference between while dowhile?

While: If we can use while statement it will check the condition then proceed further loop statement.DoWhile: If we use dowhile, first execute loop statement then check the condition.


The Do-While loop is what type of loop?

The do loop is similar to the while loop, except that the expression is not evaluated until after the do loop's code is executed. Therefore the code in a do loop is guaranteed to execute at least once. The following shows a do loop in action: do { System.out.println("Inside do while loop"); } while(false); The System.out.println() statement will print once, even though the expression evaluates to false. Remember, the do loop will always run the code in the loop body at least once. Be sure to note the use of the semicolon at the end of the while expression.


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