+ 3

continue Statement

I don't understand what the continue statement really does. At first, to understand the function of a while statement, I took this example: i = 1 While i <= 5: print (i) i = i+ 1 This code will run multiple times as long as i <= 5 . But why in the output of the following code, these numbers are displayed even if we do not add: 'print(i)' i = 0 while True: i = i +1 if i == 2: print("Skipping 2") continue if i == 5: print("Breaking") break print(i) print("Finished") What is the meaning of: "while True?" And then , The execution will continue until i = 5, it is the fact that continue stops the current iteration which is : i = i + 1 if i == 2: print("Skipping 2") And continues with the next one which is: if i == 5: print("Breaking") the question here is : if we didn't add the break statement, the execution won't stop , this means; the program would indefinitely print the result of i = i + 1; which means that continu will not stop the first iteration once and for all. what is the exact function of continue Statement? How does it work? I am so confused, I hope you understand what I meant.

8th Jul 2019, 2:07 PM
KHADIJA AI
KHADIJA AI - avatar
9 Réponses
+ 7
while True: it is a construction of an infinite loop because it is no termination expression used. so user has to make sure that in certain circumstances a break statement allows to leave the loop.
8th Jul 2019, 3:23 PM
Lothar
Lothar - avatar
+ 6
You are right - on the first view it will be the same with or without using ‘continue‘. With the first if statement you check if i == 2. if it evaluates True, it does not make sense also to check for if i == 5. so if you have continue in the first if statement the program control will go direct to the head of the while loop. If you do not use continue, the next condition will checked and then go to the head of the loop.
8th Jul 2019, 4:18 PM
Lothar
Lothar - avatar
+ 5
I get it now, thank you. But let comeback to this question: What is the meaning of: "while True?"
8th Jul 2019, 2:38 PM
KHADIJA AI
KHADIJA AI - avatar
+ 5
That was a funny instance ^^ I do understand now :) Thanks for all of you.
9th Jul 2019, 4:02 PM
KHADIJA AI
KHADIJA AI - avatar
+ 4
thanks so much, i understand now. Only one thing left, In the following code, I deleted 'continue', in consequence nothing has changed, the output was the same as the first one. So why to use it? or there are some other uses for the continue statement? i = 0 while True: i = i +1 if i == 2: print("Skipping 2") if i == 5: print("Breaking") break print(i)
8th Jul 2019, 3:43 PM
KHADIJA AI
KHADIJA AI - avatar
+ 3
using continue wont stop it from printing it all in a switch, continue is use mostly with if statement to jump through a certain requirement of the coder if(i==2) continue; this means that when i equals 2 dont carry out any operation just go ahead to the next iteration, while break will take you out of the loop entirely
8th Jul 2019, 2:12 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 3
Sometimes the examples are a bit awkward and fail to demonstrate, what a language element is really about. Just understand the basic idea. You have a collection of things (an iterable) and do something with every item of the collection (iterate over it). And whenever for some reason you want to skip one of the items, you use continue and directly move on to the next item. Let's say you are sorting a pile of pink socks, you want to make pairs. Half way through you find a yellow sock. What do you do? You ignore it and just *continue* with the next pink sock. But what if you suddenly realize that the socks are smelly? Then you use *break* - because it doesn't make sense to sort socks that accidentally haven't been washed yet. ;-)
8th Jul 2019, 3:54 PM
HonFu
HonFu - avatar
+ 2
Let's say you have a list, that has numbers and strings in it. Now you only want to calculate the sum of the numbers. And if there's something else in the list, let's say a tuple, the whole procedure should be aborted. for item in that_list: sum_ = 0 if type(item) is str: continue elif type(item) is int: sum_ += item else: break Do you get an idea how it will work out?
8th Jul 2019, 2:18 PM
HonFu
HonFu - avatar
+ 1
When you test for any condition, it will be evaluated as either True or False. 2>1 becomes True, 4=='4' becomes False. Sometimes you want to let a loop run permanently, so you need a condition that always checks out as True. You could write: while 1==1: while True != False: while 100>99: Or anything like that. Or, since this all turns out as True anyway, you just write that directly: while True: ...
8th Jul 2019, 3:24 PM
HonFu
HonFu - avatar