Break & continue Statement

 Break

"Break" function is used to break / stop the running loop or infinite loop when the given condition gets true.
Code👇
i= 0
while(True):
print(i+1, end=" ")
if(i==12):
break
i = i + 1
1 2 3 4 5 6 7 8 9 10 11 12 13

Dissection of the program

"i" = used for initializing the number
"While" = used for loop
"True" = given condition
"end= " "" = {end} is used for remove the new lines whereas, {" "} is used for the white spaces
"if" = used for giving condition
"print" = to print the statement 
"break" = used to break the loop at given condition (12)

Continue

"Continue" function is used to starts a loop from the given / specific location from the list or loop.
Code👇
i= 0
while(True):
if i+1<5:
i = i + 1
continue
print(i+1, end=" ")
if(i==20):
break
i = i + 1
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
    note: Here the loop starts with "5",  it ignores the previous given or initial number from "5". 

Comments

Popular Posts