Interview Questions 18 10
Interview Questions 18 10
Syntax:
Initialization
while condition:
# Block of while loop
#Update
For Example:
i = 0
while i < 5:
print(i)
i=i+1
Output:
0
1
2
3
4
Syntax:
Initialization
while condition:
# Block of while loop
#Update
For Loop:
Syntax:
#For Loop
Ans:
a. pass
for i in range(5):
if i == 3:
pass # Skip iteration when i is 3
print(i)
b. continue
for i in range(5):
if i == 3:
continue # Skip printing when i is 3
print(i)
c. break
for i in range(5):
if i == 3:
break # Exit loop when i is 3
print(i)
Ans:
You can iterate characters from a string using a while loop by maintaining an index
variable.
char=0
while char<len(name):
print(name[char])
char=char+1
# T
# h
# e
# K
# i
# r
# a
# n
# A
# c
# a
# d
# e
# m
# y