Python Forum
Use of 'continue' instruction in a 'while' loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use of 'continue' instruction in a 'while' loop
#1
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!
Reply
#2
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.
GJG likes this post
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020