• While loop with else using in python?

    While loop with else using in python?

    Silver Member Asked on June 5, 2018 in Python.
    Add Comment
  • 1 Answer(s)
    Best answer

    Same as that of for loop, we can have an optional else block with while loop as well.
    The else part is executed if the condition in the while loop evaluates to False. The while loop can be terminated with a break statement.
    In such case, the else part is ignored. Hence, a while loop’s else part runs if no break occurs and the condition is false.


    # Example to illustrate
    # the use of else statement
    # with the while loop
    counter = 0
    while counter < 3:
    print("Inside loop")
    counter = counter + 1
    print("Inside else")


    Inside loop
    Inside loop
    Inside loop
    Inside else
    Silver Member Answered on June 5, 2018.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.