Hi... I'm still a beginner, but I want to ask what are the differences between the "for" statment and the "while" statement. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Hi... I'm still a beginner, but I want to ask what are the differences between the "for" statment and the "while" statement.

29th Aug 2017, 5:52 PM
salemHawari
3 Answers
+ 11
for(initialization; condition; inc/dec){ //statements } Note: Best when we know the no of times a loop will run while(condition){ //statements } Used where we don't know how many times a loop will run
29th Aug 2017, 5:55 PM
P R
P R - avatar
+ 9
"For" executes an iteration loop - it iterates through a range, a string, a list/array or any other collection type. It starts at the iterator's first element and finishes at the last (unless told otherwise inside the loop). "While" executes a conditional loop - it executes *only* or *as long as* its condition is true/True. Some languages also have a do...while loop which executes the code first (so it is sure to fire up at least once) and checks for the validity of the argument at the end.
29th Aug 2017, 7:19 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
0
thanks for helping
29th Aug 2017, 5:56 PM
salemHawari