Python continue statement

Created By: chatGPT

In Python, the continue statement is used within loops to skip the current iteration and move on to the next iteration of the loop. This is particularly useful when you want to bypass certain conditions without terminating the entire loop. For instance, if you're processing a list of numbers and want to ignore negative values, the continue statement allows you to quickly skip those values.
numbers = [1, -2, 3, -4, 5]

for num in numbers:
    if num < 0:
        continue  # Skip negative numbers
    print(num)  # This will only print positive numbers
In the above example, the loop iterates over a list of integers. Whenever the continue statement is encountered, it immediately jumps to the top of the loop for the next value, thus skipping the print operation for negative numbers. This results in only positive numbers being printed.
output:
1
3
5
This behavior can be very useful in scenarios where you need to filter out specific values from a dataset without stopping the loop entirely. You can use continue in both for and while loops to achieve similar control flow.
i = 0

while i < 5:
    i += 1
    if i % 2 == 0:
        continue  # Skip even numbers
    print(i)  # This will print only odd numbers
In this while loop example, even numbers are skipped, and only odd numbers are printed as a result. The loop increments i by 1 in each iteration. When i is even, the loop encounters the continue statement and skips the print statement.
output:
1
3
5
Introduction And SetupVariablesData TypesIntFloatStrBoolListTupleSetDictMethodsString MethodsList MethodsDict MethodsSet MethodsControl FlowIf StatementElif StatementElse StatementFor LoopWhile LoopBreak StatementContinue StatementFunctionsDef StatementReturn StatementLambda FunctionsScopeParametersArgumentsDefault ParametersVariable Length ArgumentsOperatorsArithmetic OperatorsComparison OperatorsLogical OperatorsAssignment OperatorsIdentity OperatorsMembership OperatorsData StructuresListsTuplesSetsDictionariesCommentsSingle Line CommentsMulti Line CommentsSyntaxIndentationDocstringsEvent HandlingError HandlingTry StatementExcept StatementFinally StatementRaise StatementAssert StatementContext ManagersWith Statement