Python Forum

Full Version: Use of 'continue' instruction in a 'while' loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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!
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.