***Welcome to ashrafedu.blogspot.com ***This website is maintained by ASHRAF***
***Use tabs for Content***

POSTS

    Break and continue

    Break:

    The break statement terminates the loop containing it and control of the program flows to the statement immediately after the body of the loop. If break statement is inside a nested loop, break will terminate the innermost loop.












    break in for loop

    for var in sequence:

    # code inside for loop

    If condition:

    break (if break condition satisfies it jumps to outside loop)

    # code inside for loop

    # code outside for loop

    break in while loop

    while test expression

    # code inside while loop

    If condition:

    break (if break condition satisfies it jumps to outside loop)

    # code inside while loop

    # code outside while loop

    Example:

    for num in [11, 9, 88, 10, 90, 3, 19]:

    print(num)

    if(num==88):

    print("The number 88 is found")

    print("Terminating the loop")

    break

    Continue:

    The continue statement is used to skip the rest of the code inside a loop for the current iteration only. Loop does not terminate but continues on with the next iteration.












    continue in for loop:

    for var in sequence:

    # code inside for loop

    If condition:

    continue (if break condition satisfies it jumps to outside loop)

    # code inside for loop

    # code outside for loop

    continue in while loop;

    while test expression

    # code inside while loop

    If condition:

    continue(if break condition satisfies it jumps to outside loop)

    # code inside while loop

    # code outside while loop

    Example:

    # program to display only odd numbers

    for num in [20, 11, 9, 66, 4, 89, 44]:

    # Skipping the iteration when number is even

    if num%2 == 0:

    continue

    # This statement will be skipped for all even numbers

    print(num)