answersLogoWhite

0

What is difference between for loop and while loop?

Updated: 8/10/2023
User Avatar

Raghuveertank

Lvl 1
13y ago

Best Answer

One first basic rule is that you should use the for loop when the number of iterations is known. For instance:

  • The loop iterates over the elements of a collection.
  • The loop iterates over a range of values which is known in advance at execution time.
  • The object provides a way to iterate over its elements with a for statements (e.g files).

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.

The while loop can take every boolean expression as condition and permits therefore more complex end conditions. It is also the better choice if the iteration variable does not change evently by the same step or if there are more than one iteration variable. Even if you can handle more than one iteration variable in a for statement, the collections from which to choose the values must have the same number of elements.

Note: In C 'for' and 'while' can be easily replaced by each other:

while (exp) stmt

for (;exp;) stmt

for (exp1;exp2;exp3) stmt

{ exp1; while (exp2) { stmt exp3}}

User Avatar

Wiki User

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

Wiki User

13y ago

What kind of loop and a while loop. Please be more specific if you want any chance of the question answered.

answ2. But in general, a For loop will have a numeric value attached, such as For 1 to 10, ..

Whereas a While loop will usually be dependent on some condition, such as While t > 99, Do.. ; or While clock is LT 10.00, Do ..

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

actually there is no major difference between the two loops. They are both extremely similar in the way they work. The only difference is the way they are coded - syntax wise.

Ex:

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

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

A for loop repeats a set number of times A while loop repeats until a certain value changes'for' statement takes the start condition, stop condition, and the increment.

While loop

'while' statement just takes the stop condition.

if we are using the continue condition before increment or decrement in while loop

the program will go along because after continue statement it can't read any thing

but in the case of for loop it is changed because when the for loop used the program takes all the condition firstly and working on it as initialization and condition and increment or decrement and then generating the result.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

A while loop repeats a block while a condition is true. A for loop is syntactic sugar for a certain kind of while loop. That is, a for loop:

for (INIT; COND; STEP) {
STATEMENTS

}


Where INIT is an initial statement (e.g., int i = 0), COND is the loop condition (i < 10), and STEP is the step statement (++i), can be replaced with a while loop:

INIT;
while (COND) {
STATEMENTS;
STEP;

}


Every for loop can be implemented as a while loop, but for loops tend to look nicer for iterating over ranges and containers.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

actually there is no major difference between the two loops. They are both extremely similar in the way they work. The only difference is the way they are coded - syntax wise.

Ex:

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

…

}

int i = 0;

While(i<10) {

…

i++;

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Both the for loop and the while loop execute the loop block until a particular condition is satisfied. The for loop is a compressed form of while loop because the loop counter and loop condition are both declared in one line instead of different lines.

ex:

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

...

}

int i = 0;

while (i < 10){

...

i++;

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is difference between for loop and while loop?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What the difference between pretest loop and posttest loops?

The difference is that pre means before and post means after in Latin so it's tested before or after. :)


Compare Do-While with While Statements?

A Do-While loop looks like this: do { loop body } while (condition); and a While loop looks like this: while (condition) { loop body } The main difference is that the loop body is always run once in the Do-While loop, then the condition is checked to see if the loop should keep running. In a While loop, the condition is checked first, and it will not run the loop body at all if the condition is false.


What is the difference between a loop and a whorl fingerprint?

That a loop is curved and a whorl is shaped like a wave.


What are the types of loops?

There are three forms of loop commonly used in C/C++, the for loop, the while loop and the do-while loop. The for loop is most commonly used whenever an action is going to be performed a set amount of times. For example, to sum every element in an array: for(i = 0; i &lt; arraySize; i++) { sum = sum + array[i]; } The while loop and do-while loop are commonly used to loop until a condition is met. The difference between the two is that the do-while loop goes through one iteration before checking its condition, while the while loop checks its condition before any execution of the loop. Example do-while loop: do { randomNumber = rand() % 10; }while(randomNumber != 6); Example while loop: cout &gt; number; while(number &lt; 0) { cout &gt; number; }


What is difference between for loop and do-while loop?

The do loop is similar to the forloop, 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.

Related questions

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.


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.


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


Is a while loop or a for loop faster?

No difference.


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 the difference between do and while loops in c?

the main difference b/w do and while loops is that do loop will run atleast once even if condition is not satisfied but while loop will not execute even once if condition is not satisfied . this is bcoz in do loop condition is checked after one execution but in while condition is prechecked.


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


Open loop and close loop system in injection moluld machine?

loop checking is perform before cable termination..the difference between a close loop and open loop is,tha close loop has a feedback while the open loop has not.


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.


What the difference between pretest loop and posttest loops?

The difference is that pre means before and post means after in Latin so it's tested before or after. :)


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

A while loop executes code inside the while block continuously until the said condition is not true. A for loop contains three parts. The first part is carried out prior to the for loop, the middle part is executed by the for loop until it is no longer true, and the final part is performed at the end of each go round of the loop.