Python Forum
Use of 'continue' instruction in a 'while' loop - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: Code Review (https://python-forum.io/forum-46.html)
+--- Thread: Use of 'continue' instruction in a 'while' loop (/thread-31706.html)



Use of 'continue' instruction in a 'while' loop - GJG - Dec-29-2020

Hi folks! I'm a beginner and at the moment I'm taking a course of loop control in python. One of examples of how to implement continue instruction in a loop was this:
largest_number = -99999999
counter = 0

number = int(input("Enter a number or type -1 to end program: "))

while number != -1:
    if number == -1:
        continue
    counter += 1

    if number > largest_number:
        largest_number = number
    number = int(input("Enter a number or type -1 to end program: "))

if counter:
    print("The largest number is", largest_number)
else:
    print("You haven't entered any number.")
What I don't understand is why is it needed there at all? same for the if number == -1: condition? what function does it perform? and Isn't 'while' condition enough to carry out the task?
while number != -1:
    counter += 1
seems to be working fine without them..?
Thank you!


RE: Use of 'continue' instruction in a 'while' loop - Larz60+ - Dec-29-2020

that code will never execute, you're right, it's not needed and is worthless.
the condition of the while clause is number not = -1
so if number == -1 can never be satisfied.