Computer Science, asked by nayaknitya503, 19 days ago

Explain use of while v/s for loop with suitable example. Subject : - Python ​

Answers

Answered by Sohanakamran
1

Use of while loop vs do loop with suitable examples in Python programming:

Python programming is a type of computer language it helps the user to interpret with the computer.

  • The set of statements is executed in a while loop till the condition of the statement is true. But for loop runs a list of objects, it also runs if the statement is false once.
  • while loop has indefinite the number of iterations but for loop has a definite number of iterations.
  • while loop cannot be iterated by the generator but for loop can be iterated by generators.
  • for loop is faster than while loop in python programming.
  • If the statement is true in the while loop then only the loop ends but if the statement is false in for loops it continues with the next statement and ends when the statement is true.

  • example of while loop:

m = 1

while i < 9:

print(m)

m += 1

  • example of for loop:

colors = ["red", "blue", "pink", "white"]

for x in colors:

print(x)

Similar questions