answersLogoWhite

0

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

14y ago

What else can I help you with?

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 difference between a wire loop and a wire needle?

ewan ko


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 < 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 > number; while(number < 0) { cout > number; }

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.

Trending Questions
What should be done after a stress cone is installed on a high voltage conductor termination? What are 3 types of opposition to current flow that impedance represents? Should you pour a concrete sidewalk without crushed stone? Can any one of you please suggest which materials are to be prepared for basic mechanical engineering? What are the common errors in shell scripting associated with for loop? What are the various turning operations that can be performed and the shapes and types of tooling that can be used (such as solid speed tooling? What is the difference between P and BR tag? How much voltage does a battery produce? How do you change font size default in tvs msp 250 star printer? Who is the real frank barnes engineer? When a multi dimensional array is passed to a function how are formal argument declaration written? Name different ways of joining metals? How can you tell if a pipe fitting is cast or ductile iron? What can be used to reduce the amount of voltage drop across the branches of a parallel circuit to be less than source voltage? Some technologies like vertical farming have a number of negative effects Which is a negative outcome of this technology? Why won't lotro load? What is meant by open circuit voltage and arc voltage? What is the importance of water supply system? What are the effect of over tightening the compression fitting? If resistor X has the same length and is made of the same material as resistor Y but has twice the diameter. Resistor X has the blank resistance of resistor Y?