Skip to content

Commit f410c20

Browse files
committed
Flow control loop examples for and while loops
1 parent 86806b9 commit f410c20

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

flowControlLoopExample.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#Flow control
2+
# 2) Iterative Statement
3+
# loops
4+
#
5+
# for loop
6+
s = input("Enter a string")
7+
count = 0
8+
index = 0
9+
for i in s:
10+
print("the index of {} is".format(i),end=" ")
11+
print(index)
12+
index += 1
13+
count += 1
14+
print(count)
15+
16+
l =[10,20,30]
17+
for x in l:
18+
print(x)
19+
20+
for y in range(1,10):
21+
print(y)
22+
23+
# while loop
24+
# iteration know in advance then we should go for loop
25+
# if we dont know then we can use while loop
26+
# while condition:
27+
# body
28+
29+
# Example 1 print 1 to 10 numbers
30+
x = 1
31+
while x<= 5:
32+
print(x)
33+
x+=1
34+
35+
#example 2
36+
37+
name =""
38+
pwd = ""
39+
while (name != 'naveen') and (pwd != 1234):
40+
name= input("enter name:")
41+
pwd = eval(input("enter password"))
42+
43+
print("Hello",name,"Good morning")
44+
45+
# Example 3 infinite loop
46+
# i = 0
47+
# while True:
48+
# print(i)
49+
# i+=1
50+
51+
#nested loops
52+
#loop inside another loop
53+
54+
for i in range(4):
55+
for j in range(4):
56+
print(1,j)
57+
58+
#example
59+
n = eval(input("enter number of rows:"))
60+
for i in range(1,n+1): # i represents rows number
61+
for j in range(1,i+1): # j represents the number of *
62+
print("*",end= " ")
63+
print()

0 commit comments

Comments
 (0)