xxxxxxxxxx
while True:
//do something
if (""" break condition """):
break
xxxxxxxxxx
#there is no do while loop in python but you san simulate it
i = 1
while True:
print(i)
i = i + 1
if(i > 5):
break
xxxxxxxxxx
while <condition>:
<run code here>
# for example,
i = 0
while True:
i += 1
# i will begin to count up to infinity
while i == -1:
print("impossible!")
xxxxxxxxxx
# A while loop is basically a "if" statement that will repeat itself
# It will continue iterating over itself untill the condition is False
python_is_cool = True
first_time = True
while python_is_cool:
if first_time:
print("python is cool!")
else:
first_time = False
print("Done")
# The while loop can be terminated with a "break" statement.
# In such cases, the "else" part is ignored.
# Hence, a while loop's "else" part runs if no break occurs and the condition is False.
# Example to illustrate the use of else statement with the while loop:
counter = 0
while counter < 3:
print("Inside loop")
counter = counter + 1
else:
print("Inside else")
while loops python
xxxxxxxxxx
condition = True
while condition == True:
execute the following
This is basically saying the while a certain variable is equal to something, go ahead and do this.
You can also say when a certain variable is NOT equal to something do this, or when something is True or False, do...
xxxxxxxxxx
##################################
# THERE IS NO DO-WHILE IN PYTHON #
##################################
## BEACUSE YOU DON'T NEED IT
# Here is the alternative
while True: # INFINITE LOOP (not really) # This is like do
'''
stuff to do
'''
if not (condition): # this is like while (condition)
break
xxxxxxxxxx
while <Condition>:
<code>
#example
i = 10
while i == 10:
print("Hello World!")
xxxxxxxxxx
i = 1
while i<10:
print("hello world")
i+=1
#note the i++ syntax is not valid in python
xxxxxxxxxx
# while True loop
while True:
print("This will continue to print this forever")
# while variable loop
value = 5
while value = 5:
# Since this is true, this will continue on forever
print("Yes")