What is the difference between an "if" loop and "while" loop! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is the difference between an "if" loop and "while" loop!

I started the lesson in while loops confident, but when I started learning I could not tell the difference for what if loops does, and while loops

30th Oct 2018, 9:27 PM
Johnny Meyer
Johnny Meyer - avatar
3 Answers
+ 3
The difference between for and while loops is only in syntax. In for loop you initialize your variable(s), that take part in condition, set the condition and define variable(s) changes after executing loop's body. Everything above is in parenthesis of for statement. In while loop you do exactly the same, but in different places. You can define those variables before loop, set condition in parenthesis of while statement and change those variables at the end of loop's body (or anywhere you need). Pointing out the difference, variables initialized via for statement won't exist outside the loop. But the same can be reached via while loop in some specific way
30th Oct 2018, 10:17 PM
microBIG
microBIG - avatar
+ 1
if statement doesn't create loop. It only checks condition and executes code block if it is true, only one time. while statement creates a loop. It also checks condition and executes code block after it if it is true, multiple times, until condition becomes false
30th Oct 2018, 9:33 PM
microBIG
microBIG - avatar
0
microBIG My Apologies for the misleading question I meant to ask the difference to a for loop and while loop
30th Oct 2018, 10:04 PM
Johnny Meyer
Johnny Meyer - avatar