dgadiraju
10/27/2017 - 11:44 AM

loops-example.py

# Prints out 0,1,2,3,4 using while loop

count = 0
while True:
    print(count)
    count += 1
    if count >= 5:
        break
# Prints out only even numbers - 2,4,6,8,10 using for loop

for x in range(10):
    # Check if x is even
    if not x % 2 == 0:
        continue
    print(x)
#nested loop----printing prime numbers less than 100

i=2
while(i<100):
    j=2
    while(j<=(i/j)):
        if not (i%j): break
        j=j+1
    if(j> (i/j)): print(str(i)+" is prime")
    i=i+1

#difference between continue and pass--printing elements of list

a=[0,1,2,3]
for ele in a:
    if not ele:
        pass
    print(ele)
print("------")
for ele in a:
    if not ele:
        continue
    print(ele)