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

+9 votes
asked Jun 16 by Swaliha S (220 points)

4 Answers

+1 vote
answered Jun 17 by Mrunaal Earla (170 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.
+1 vote
answered Jun 18 by pugazh (160 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 6 days ago 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 4 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 !!!

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