Last answered:

20 Aug 2024

Posted on:

20 Aug 2024

0

while loops and incrementing!

I am completely unable to comprehend the difference between for and while loop, and when to use these.Furthermore, I didn't understand the concept behind  incrementing. Do we always have to add, for instance, x + 2 in incrementing. Kindly help!
1 answers ( 0 marked as helpful)
Instructor
Posted on:

20 Aug 2024

0
Hi Muhammad!
Thanks for reaching out!
Consider the following principle: Use a for loop when you know the exact number of iterations, and a while loop when you don't. For instance, if you want to iterate through each item in a list or repeat an action a specific number of times, a for loop is a good option. And if you don’t know beforehand how many times the loop will run, like when checking a condition continuously, then while loop is the right option.
Incrementing means increasing the value of a variable. You can add any number, for instance, x = x + 2 increases x by 2. 
Hope this helps.
Best,
Ivan

Submit an answer