Learn to Code via Tutorials on Repl.it!

← Back to all posts
while vs.if
Joseph114 (1)

What is the difference between "while" and "if"?

While

I made two variabiles with user input:

n1 = input("Write down any number: ")
repeat_n1 = input("Input the same number another time: ")

Then I added a "while" statement:

while n1 != repeat_n1:
	print("The two numbers are not the same")
	repeat_n1 = input("Input the same number another time: ")
if n1 == repeat_n1:
	print("OK")

If the program reaches a state that the "while" statement contains, the program executes everything contained in the "while" statement repeatedly

If

if n1 != repeat_n1:
	print("The two numbers are not the same")
	repeat_n1 = input("Input the same number another time: ")
if n1 != repeat_n1:
	print("Ugh... This is a waste of time")
	quit()

The "if" statement does the same thing as the "while" statement, butthe program only executes the code contained in the "if" statement once.