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

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

8 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 (220 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 Jun 23 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 Jun 28 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. 
0 votes
answered Jul 14 by rohan ag (1,350 points)
if you know number of iterations go with for loop else go with while loop
0 votes
answered Jul 15 by Swapna J.mukkundi (140 points)
when we know finite number of iteration then we use for loop and when we dont know iteration then we use while loop or infinite loop execution
0 votes
answered Jul 16 by huzaifaaliyuj-create (140 points)

Both for and while loops are control flow structures that repeatedly execute a block of code, but they differ in syntax, use cases, and control mechanisms.

For Loop is generally used when the number of iterations is known in advance or when iterating over a sequence (list, tuple, string, range, etc.). It automatically handles initialization, condition checking, and iteration updates internally.

While Loop is preferred when the number of iterations is unknown and depends on a condition being met. It requires manual initialization and update of loop variables, and the loop continues until the condition becomes false.

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.
...