Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

difference between for loop and while loop

+10 votes
asked Jun 16 by Swaliha S (230 points)

5 Answers

+2 votes
answered Jun 17 by Mrunaal Earla (190 points)
for loop will be used when we know how many number of iterations we needed to do and
while is used when we don't know how many iterations do we require to do
and also
in some cases we can use both for / while loop anyone would be great.
+2 votes
answered Jun 18 by pugazh (180 points)

Featurefor Loopwhile Loop
Best used whenNumber of iterations is knownNumber of iterations is not known in advance
StructureInitialization, condition, and update are written togetherInitialization and update are usually written separately
ReadabilityMore compact for counting loopsMore flexible for condition-based loops
Common useTraversing arrays, repeating a fixed number of timesReading input until a condition changes, waiting for an event
0 votes
answered Jun 21 by GANTA NAGA TEJA (140 points)

 

FOR LOOP : runs a fixed number of times, usually when you already know how many times you want the code to repeat.

WHILE LOOP: runs until a condition becomes false, which is useful when you don’t know in advance how many times it should run.

0 votes
answered 6 days ago by Ankit Baheti (140 points)

for loop and while loop has one of the major difference is that while loop continues the code until the break element is executed on the other hand for loop can only only execute code only once example if we gonna create a number guessing game then the user need to have multiple attempts that in this case we can only use while loop loop which executes repeatedly until the number is guessed by the user but if we use for loop here it gives user only one chance to guess the number so while loop is majorly used that's the simple and major difference between while and for loop i think you understood it buddy !!!

0 votes
answered 23 hours ago by Godala VijayaKumari (160 points)
  •  Both for loop and while loop are entry controlled loops.
  • for loop is used when we know number of time the loop to be iterated.
  • for loop can also be represented without the initialization and updation part but syntax must be followed .
  • example 
  • int i=1;
  •    for( ; i<10; ){
           printf("%d",i);
           i++;
           }
  • while loop is used when we don't know the number of times the loop to be iterated. 
  • while loop will be terminated only when the condition gets failed . .
  • when we don't know for how many iterations required for condition to get failed the while loop will be used. 
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...