Differentiate between for and while statements with example.
Answers
Answer:
The 'for' loop used only when we already knew the number of iterations. The 'while' loop used only when the number of iteration are not exactly known. If the condition is not put up in 'for' loop, then loop iterates infinite times. If the condition is not put up in 'while' loop, it provides compilation error.
its help you
plese follow me
Answer:
Explanation:
A for loop is generally used for iteration on every element of a particular iterable object like a list, set, tuple etc.
A while statement is generally used to loop a bunch of codes until a specific condition is met.
example:
# Program to find the sum of all numbers stored in a list
# List of numbers
numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
# variable to store the sum
sum = 0
# iterate over the list
for val in numbers:
sum = sum+val
print("The sum is", sum)
example:
count = 0
while (count < 9):
print 'The count is:', count
count = count + 1
print "Good bye!"...