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

+8 votes
asked 3 days ago by Swaliha S (210 points)

2 Answers

0 votes
answered 2 days ago by Mrunaal Earla (150 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.
0 votes
answered 1 day ago by pugazh (140 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
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.
...