curious

This forum is a place to ask questions about the python programming language.
Post Reply
User avatar
JP123
New User
Posts: 3
Joined: 17 Sep 2018, 10:15

curious

Post by JP123 » 12 Nov 2018, 21:02

Might be a dumb question but when using a while loop, if you put "continue" before you add a break does anything happen?
1 x

User avatar
Mr. MacKenty
Site Admin
Posts: 108
Joined: 28 Apr 2018, 17:06
Answers: 3

Re: curious

Post by Mr. MacKenty » 12 Nov 2018, 21:07

JP123 wrote:
12 Nov 2018, 21:02
Might be a dumb question but when using a while loop, if you put "continue" before you add a break does anything happen?
NO SUCH THING AS A DUMB QUESTION!

The code below works:

Code: Select all

counter = 0
while True:
    print("Hello JP123")
    counter = counter + 1
    if counter == 3:
        break
The code below executes an endless loop. If you run this code, just push control and C to stop the program.

Code: Select all

counter = 0
while True:
    print("Hello JP123")
    counter = counter + 1
    if counter == 3:
        continue
        break
Why? Well, this program runs from the top down. It hits the continue and never goes past continue :-) So the break is never called.
0 x

Post Reply