[{"data":1,"prerenderedAt":1893},["ShallowReactive",2],{"doc-\u002Flearn\u002Fpython-break-and-continue-statements":3},{"id":4,"title":5,"body":6,"description":1886,"extension":1887,"meta":1888,"navigation":164,"path":1889,"seo":1890,"stem":1891,"__hash__":1892},"content\u002Flearn\u002Fpython-break-and-continue-statements.md","Python break and continue Statements",{"type":7,"value":8,"toc":1849},"minimark",[9,13,25,39,50,55,191,200,204,207,230,243,247,252,255,269,274,278,281,415,420,460,464,467,490,503,507,512,514,532,537,541,544,595,598,622,626,640,648,652,655,667,672,683,687,698,701,834,837,883,887,893,899,908,979,982,1000,1007,1072,1075,1095,1109,1118,1122,1129,1133,1139,1190,1193,1206,1216,1220,1225,1276,1278,1298,1305,1309,1312,1375,1385,1390,1394,1397,1461,1467,1470,1500,1506,1568,1572,1576,1587,1591,1602,1608,1613,1661,1665,1668,1694,1697,1752,1761,1765,1769,1777,1781,1791,1795,1798,1802,1808,1812,1815,1819,1845],[10,11,5],"h1",{"id":12},"python-break-and-continue-statements",[14,15,16,20,21,24],"p",{},[17,18,19],"code",{},"break"," and ",[17,22,23],{},"continue"," are used inside loops to control what happens next.",[26,27,28,34],"ul",{},[29,30,31,33],"li",{},[17,32,19],{}," stops the loop completely",[29,35,36,38],{},[17,37,23],{}," skips the current loop step and moves to the next one",[14,40,41,42,45,46,49],{},"These statements work in both ",[17,43,44],{},"for"," loops and ",[17,47,48],{},"while"," loops. They are useful when you want more control over loop behavior without rewriting the whole loop.",[51,52,54],"h2",{"id":53},"quick-example","Quick example",[56,57,62],"pre",{"className":58,"code":59,"language":60,"meta":61,"style":61},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","for number in range(1, 6):\n    if number == 3:\n        continue\n    if number == 5:\n        break\n    print(number)\n\n# Output:\n# 1\n# 2\n# 4\n","python","",[17,63,64,100,118,124,138,144,159,166,173,179,185],{"__ignoreMap":61},[65,66,69,72,76,79,83,87,91,94,97],"span",{"class":67,"line":68},"line",1,[65,70,44],{"class":71},"sVHd0",[65,73,75],{"class":74},"su5hD"," number ",[65,77,78],{"class":71},"in",[65,80,82],{"class":81},"sptTA"," range",[65,84,86],{"class":85},"sP7_E","(",[65,88,90],{"class":89},"srdBf","1",[65,92,93],{"class":85},",",[65,95,96],{"class":89}," 6",[65,98,99],{"class":85},"):\n",[65,101,103,106,108,112,115],{"class":67,"line":102},2,[65,104,105],{"class":71},"    if",[65,107,75],{"class":74},[65,109,111],{"class":110},"smGrS","==",[65,113,114],{"class":89}," 3",[65,116,117],{"class":85},":\n",[65,119,121],{"class":67,"line":120},3,[65,122,123],{"class":71},"        continue\n",[65,125,127,129,131,133,136],{"class":67,"line":126},4,[65,128,105],{"class":71},[65,130,75],{"class":74},[65,132,111],{"class":110},[65,134,135],{"class":89}," 5",[65,137,117],{"class":85},[65,139,141],{"class":67,"line":140},5,[65,142,143],{"class":71},"        break\n",[65,145,147,150,152,156],{"class":67,"line":146},6,[65,148,149],{"class":81},"    print",[65,151,86],{"class":85},[65,153,155],{"class":154},"slqww","number",[65,157,158],{"class":85},")\n",[65,160,162],{"class":67,"line":161},7,[65,163,165],{"emptyLinePlaceholder":164},true,"\n",[65,167,169],{"class":67,"line":168},8,[65,170,172],{"class":171},"sutJx","# Output:\n",[65,174,176],{"class":67,"line":175},9,[65,177,178],{"class":171},"# 1\n",[65,180,182],{"class":67,"line":181},10,[65,183,184],{"class":171},"# 2\n",[65,186,188],{"class":67,"line":187},11,[65,189,190],{"class":171},"# 4\n",[14,192,193,194,196,197,199],{},"Use ",[17,195,23],{}," to skip the current loop step. Use ",[17,198,19],{}," to stop the loop completely.",[51,201,203],{"id":202},"what-this-page-teaches","What this page teaches",[14,205,206],{},"By the end of this page, you will understand that:",[26,208,209,214,219,227],{},[29,210,211,213],{},[17,212,19],{}," stops a loop early",[29,215,216,218],{},[17,217,23],{}," skips to the next loop iteration",[29,220,221,222,45,224,226],{},"Both can be used inside ",[17,223,44],{},[17,225,48],{}," loops",[29,228,229],{},"They help control loop flow without changing the whole loop",[14,231,232,233,20,238,242],{},"If you are still getting comfortable with loops, see ",[234,235,237],"a",{"href":236},"\u002Flearn\u002Fpython-for-loops-explained\u002F","Python for loops explained",[234,239,241],{"href":240},"\u002Flearn\u002Fpython-while-loops-explained\u002F","Python while loops explained",".",[51,244,246],{"id":245},"what-break-does","What break does",[14,248,249,251],{},[17,250,19],{}," exits the nearest loop immediately.",[14,253,254],{},"That means:",[26,256,257,260,266],{},[29,258,259],{},"the loop stops right away",[29,261,262,263,265],{},"any code after ",[17,264,19],{}," inside that loop does not run",[29,267,268],{},"Python continues with the first line after the loop",[14,270,193,271,273],{},[17,272,19],{}," when the job is already done. For example, if you are searching for the first match, there is no reason to keep looping after you find it.",[51,275,277],{"id":276},"simple-break-example","Simple break example",[14,279,280],{},"This example stops when the target value is found.",[56,282,284],{"className":58,"code":283,"language":60,"meta":61,"style":61},"numbers = [4, 7, 9, 12, 15]\n\nfor number in numbers:\n    if number == 12:\n        print(\"Found 12\")\n        break\n    print(\"Checked\", number)\n\nprint(\"Loop ended\")\n",[17,285,286,323,327,340,352,371,375,395,399],{"__ignoreMap":61},[65,287,288,291,294,297,300,302,305,307,310,312,315,317,320],{"class":67,"line":68},[65,289,290],{"class":74},"numbers ",[65,292,293],{"class":110},"=",[65,295,296],{"class":85}," [",[65,298,299],{"class":89},"4",[65,301,93],{"class":85},[65,303,304],{"class":89}," 7",[65,306,93],{"class":85},[65,308,309],{"class":89}," 9",[65,311,93],{"class":85},[65,313,314],{"class":89}," 12",[65,316,93],{"class":85},[65,318,319],{"class":89}," 15",[65,321,322],{"class":85},"]\n",[65,324,325],{"class":67,"line":102},[65,326,165],{"emptyLinePlaceholder":164},[65,328,329,331,333,335,338],{"class":67,"line":120},[65,330,44],{"class":71},[65,332,75],{"class":74},[65,334,78],{"class":71},[65,336,337],{"class":74}," numbers",[65,339,117],{"class":85},[65,341,342,344,346,348,350],{"class":67,"line":126},[65,343,105],{"class":71},[65,345,75],{"class":74},[65,347,111],{"class":110},[65,349,314],{"class":89},[65,351,117],{"class":85},[65,353,354,357,359,363,367,369],{"class":67,"line":140},[65,355,356],{"class":81},"        print",[65,358,86],{"class":85},[65,360,362],{"class":361},"sjJ54","\"",[65,364,366],{"class":365},"s_sjI","Found 12",[65,368,362],{"class":361},[65,370,158],{"class":85},[65,372,373],{"class":67,"line":146},[65,374,143],{"class":71},[65,376,377,379,381,383,386,388,390,393],{"class":67,"line":161},[65,378,149],{"class":81},[65,380,86],{"class":85},[65,382,362],{"class":361},[65,384,385],{"class":365},"Checked",[65,387,362],{"class":361},[65,389,93],{"class":85},[65,391,392],{"class":154}," number",[65,394,158],{"class":85},[65,396,397],{"class":67,"line":168},[65,398,165],{"emptyLinePlaceholder":164},[65,400,401,404,406,408,411,413],{"class":67,"line":175},[65,402,403],{"class":81},"print",[65,405,86],{"class":85},[65,407,362],{"class":361},[65,409,410],{"class":365},"Loop ended",[65,412,362],{"class":361},[65,414,158],{"class":85},[416,417,419],"h3",{"id":418},"output","Output",[56,421,423],{"className":58,"code":422,"language":60,"meta":61,"style":61},"Checked 4\nChecked 7\nChecked 9\nFound 12\nLoop ended\n",[17,424,425,433,440,447,455],{"__ignoreMap":61},[65,426,427,430],{"class":67,"line":68},[65,428,429],{"class":74},"Checked ",[65,431,432],{"class":89},"4\n",[65,434,435,437],{"class":67,"line":102},[65,436,429],{"class":74},[65,438,439],{"class":89},"7\n",[65,441,442,444],{"class":67,"line":120},[65,443,429],{"class":74},[65,445,446],{"class":89},"9\n",[65,448,449,452],{"class":67,"line":126},[65,450,451],{"class":74},"Found ",[65,453,454],{"class":89},"12\n",[65,456,457],{"class":67,"line":140},[65,458,459],{"class":74},"Loop ended\n",[416,461,463],{"id":462},"why-it-stops","Why it stops",[14,465,466],{},"The important line is:",[56,468,470],{"className":58,"code":469,"language":60,"meta":61,"style":61},"if number == 12:\n    break\n",[17,471,472,485],{"__ignoreMap":61},[65,473,474,477,479,481,483],{"class":67,"line":68},[65,475,476],{"class":71},"if",[65,478,75],{"class":74},[65,480,111],{"class":110},[65,482,314],{"class":89},[65,484,117],{"class":85},[65,486,487],{"class":67,"line":102},[65,488,489],{"class":71},"    break\n",[14,491,492,493,495,496,499,500,242],{},"As soon as ",[17,494,155],{}," becomes ",[17,497,498],{},"12",", the loop ends. It does not continue to ",[17,501,502],{},"15",[51,504,506],{"id":505},"what-continue-does","What continue does",[14,508,509,511],{},[17,510,23],{}," skips the rest of the current iteration.",[14,513,254],{},[26,515,516,524,527],{},[29,517,518,519,523],{},"the loop does ",[520,521,522],"strong",{},"not"," stop",[29,525,526],{},"Python jumps straight to the next iteration",[29,528,262,529,531],{},[17,530,23],{}," in that iteration does not run",[14,533,193,534,536],{},[17,535,23],{}," when you want to ignore some values but keep looping through the rest.",[51,538,540],{"id":539},"simple-continue-example","Simple continue example",[14,542,543],{},"This loop skips one value but keeps going.",[56,545,547],{"className":58,"code":546,"language":60,"meta":61,"style":61},"for number in range(1, 6):\n    if number == 3:\n        continue\n    print(number)\n",[17,548,549,569,581,585],{"__ignoreMap":61},[65,550,551,553,555,557,559,561,563,565,567],{"class":67,"line":68},[65,552,44],{"class":71},[65,554,75],{"class":74},[65,556,78],{"class":71},[65,558,82],{"class":81},[65,560,86],{"class":85},[65,562,90],{"class":89},[65,564,93],{"class":85},[65,566,96],{"class":89},[65,568,99],{"class":85},[65,570,571,573,575,577,579],{"class":67,"line":102},[65,572,105],{"class":71},[65,574,75],{"class":74},[65,576,111],{"class":110},[65,578,114],{"class":89},[65,580,117],{"class":85},[65,582,583],{"class":67,"line":120},[65,584,123],{"class":71},[65,586,587,589,591,593],{"class":67,"line":126},[65,588,149],{"class":81},[65,590,86],{"class":85},[65,592,155],{"class":154},[65,594,158],{"class":85},[416,596,419],{"id":597},"output-1",[56,599,601],{"className":58,"code":600,"language":60,"meta":61,"style":61},"1\n2\n4\n5\n",[17,602,603,608,613,617],{"__ignoreMap":61},[65,604,605],{"class":67,"line":68},[65,606,607],{"class":89},"1\n",[65,609,610],{"class":67,"line":102},[65,611,612],{"class":89},"2\n",[65,614,615],{"class":67,"line":120},[65,616,432],{"class":89},[65,618,619],{"class":67,"line":126},[65,620,621],{"class":89},"5\n",[416,623,625],{"id":624},"why-3-is-missing","Why 3 is missing",[14,627,628,629,632,633,635,636,639],{},"When ",[17,630,631],{},"number == 3",", Python runs ",[17,634,23],{},". That skips the ",[17,637,638],{},"print(number)"," line for that iteration.",[14,641,642,643,20,645,242],{},"The loop then moves on to ",[17,644,299],{},[17,646,647],{},"5",[51,649,651],{"id":650},"break-vs-continue","break vs continue",[14,653,654],{},"The difference is simple:",[26,656,657,662],{},[29,658,659,661],{},[17,660,19],{}," = stop the loop",[29,663,664,666],{},[17,665,23],{}," = skip one pass of the loop",[14,668,193,669,671],{},[17,670,19],{}," when:",[26,673,674,677,680],{},[29,675,676],{},"the task is finished",[29,678,679],{},"you found what you were looking for",[29,681,682],{},"there is no need to keep checking more values",[14,684,193,685,671],{},[17,686,23],{},[26,688,689,692,695],{},[29,690,691],{},"you want to ignore certain values",[29,693,694],{},"you want to filter items during a loop",[29,696,697],{},"the loop should keep going after skipping one case",[14,699,700],{},"Here is a side-by-side example:",[56,702,704],{"className":58,"code":703,"language":60,"meta":61,"style":61},"print(\"Using break:\")\nfor i in range(1, 6):\n    if i == 3:\n        break\n    print(i)\n\nprint(\"Using continue:\")\nfor i in range(1, 6):\n    if i == 3:\n        continue\n    print(i)\n",[17,705,706,721,742,754,758,769,773,788,808,820,824],{"__ignoreMap":61},[65,707,708,710,712,714,717,719],{"class":67,"line":68},[65,709,403],{"class":81},[65,711,86],{"class":85},[65,713,362],{"class":361},[65,715,716],{"class":365},"Using break:",[65,718,362],{"class":361},[65,720,158],{"class":85},[65,722,723,725,728,730,732,734,736,738,740],{"class":67,"line":102},[65,724,44],{"class":71},[65,726,727],{"class":74}," i ",[65,729,78],{"class":71},[65,731,82],{"class":81},[65,733,86],{"class":85},[65,735,90],{"class":89},[65,737,93],{"class":85},[65,739,96],{"class":89},[65,741,99],{"class":85},[65,743,744,746,748,750,752],{"class":67,"line":120},[65,745,105],{"class":71},[65,747,727],{"class":74},[65,749,111],{"class":110},[65,751,114],{"class":89},[65,753,117],{"class":85},[65,755,756],{"class":67,"line":126},[65,757,143],{"class":71},[65,759,760,762,764,767],{"class":67,"line":140},[65,761,149],{"class":81},[65,763,86],{"class":85},[65,765,766],{"class":154},"i",[65,768,158],{"class":85},[65,770,771],{"class":67,"line":146},[65,772,165],{"emptyLinePlaceholder":164},[65,774,775,777,779,781,784,786],{"class":67,"line":161},[65,776,403],{"class":81},[65,778,86],{"class":85},[65,780,362],{"class":361},[65,782,783],{"class":365},"Using continue:",[65,785,362],{"class":361},[65,787,158],{"class":85},[65,789,790,792,794,796,798,800,802,804,806],{"class":67,"line":168},[65,791,44],{"class":71},[65,793,727],{"class":74},[65,795,78],{"class":71},[65,797,82],{"class":81},[65,799,86],{"class":85},[65,801,90],{"class":89},[65,803,93],{"class":85},[65,805,96],{"class":89},[65,807,99],{"class":85},[65,809,810,812,814,816,818],{"class":67,"line":175},[65,811,105],{"class":71},[65,813,727],{"class":74},[65,815,111],{"class":110},[65,817,114],{"class":89},[65,819,117],{"class":85},[65,821,822],{"class":67,"line":181},[65,823,123],{"class":71},[65,825,826,828,830,832],{"class":67,"line":187},[65,827,149],{"class":81},[65,829,86],{"class":85},[65,831,766],{"class":154},[65,833,158],{"class":85},[416,835,419],{"id":836},"output-2",[56,838,840],{"className":58,"code":839,"language":60,"meta":61,"style":61},"Using break:\n1\n2\nUsing continue:\n1\n2\n4\n5\n",[17,841,842,851,855,859,867,871,875,879],{"__ignoreMap":61},[65,843,844,847,849],{"class":67,"line":68},[65,845,846],{"class":74},"Using ",[65,848,19],{"class":71},[65,850,117],{"class":85},[65,852,853],{"class":67,"line":102},[65,854,607],{"class":89},[65,856,857],{"class":67,"line":120},[65,858,612],{"class":89},[65,860,861,863,865],{"class":67,"line":126},[65,862,846],{"class":74},[65,864,23],{"class":71},[65,866,117],{"class":85},[65,868,869],{"class":67,"line":140},[65,870,607],{"class":89},[65,872,873],{"class":67,"line":146},[65,874,612],{"class":89},[65,876,877],{"class":67,"line":161},[65,878,432],{"class":89},[65,880,881],{"class":67,"line":168},[65,882,621],{"class":89},[51,884,886],{"id":885},"using-break-and-continue-in-while-loops","Using break and continue in while loops",[14,888,889,890,892],{},"Both statements also work in ",[17,891,48],{}," loops.",[14,894,895,896,898],{},"The main thing to watch is the loop variable. In a ",[17,897,48],{}," loop, you usually update the variable yourself. If that update does not happen, the loop may never end.",[14,900,901,902,904,905,907],{},"Here is a safe ",[17,903,48],{}," loop with ",[17,906,19],{},":",[56,909,911],{"className":58,"code":910,"language":60,"meta":61,"style":61},"count = 1\n\nwhile count \u003C= 5:\n    if count == 4:\n        break\n    print(count)\n    count += 1\n",[17,912,913,923,927,941,954,958,969],{"__ignoreMap":61},[65,914,915,918,920],{"class":67,"line":68},[65,916,917],{"class":74},"count ",[65,919,293],{"class":110},[65,921,922],{"class":89}," 1\n",[65,924,925],{"class":67,"line":102},[65,926,165],{"emptyLinePlaceholder":164},[65,928,929,931,934,937,939],{"class":67,"line":120},[65,930,48],{"class":71},[65,932,933],{"class":74}," count ",[65,935,936],{"class":110},"\u003C=",[65,938,135],{"class":89},[65,940,117],{"class":85},[65,942,943,945,947,949,952],{"class":67,"line":126},[65,944,105],{"class":71},[65,946,933],{"class":74},[65,948,111],{"class":110},[65,950,951],{"class":89}," 4",[65,953,117],{"class":85},[65,955,956],{"class":67,"line":140},[65,957,143],{"class":71},[65,959,960,962,964,967],{"class":67,"line":146},[65,961,149],{"class":81},[65,963,86],{"class":85},[65,965,966],{"class":154},"count",[65,968,158],{"class":85},[65,970,971,974,977],{"class":67,"line":161},[65,972,973],{"class":74},"    count ",[65,975,976],{"class":110},"+=",[65,978,922],{"class":89},[416,980,419],{"id":981},"output-3",[56,983,985],{"className":58,"code":984,"language":60,"meta":61,"style":61},"1\n2\n3\n",[17,986,987,991,995],{"__ignoreMap":61},[65,988,989],{"class":67,"line":68},[65,990,607],{"class":89},[65,992,993],{"class":67,"line":102},[65,994,612],{"class":89},[65,996,997],{"class":67,"line":120},[65,998,999],{"class":89},"3\n",[14,1001,1002,1003,904,1005,907],{},"And here is a ",[17,1004,48],{},[17,1006,23],{},[56,1008,1010],{"className":58,"code":1009,"language":60,"meta":61,"style":61},"count = 0\n\nwhile count \u003C 5:\n    count += 1\n    if count == 3:\n        continue\n    print(count)\n",[17,1011,1012,1021,1025,1038,1046,1058,1062],{"__ignoreMap":61},[65,1013,1014,1016,1018],{"class":67,"line":68},[65,1015,917],{"class":74},[65,1017,293],{"class":110},[65,1019,1020],{"class":89}," 0\n",[65,1022,1023],{"class":67,"line":102},[65,1024,165],{"emptyLinePlaceholder":164},[65,1026,1027,1029,1031,1034,1036],{"class":67,"line":120},[65,1028,48],{"class":71},[65,1030,933],{"class":74},[65,1032,1033],{"class":110},"\u003C",[65,1035,135],{"class":89},[65,1037,117],{"class":85},[65,1039,1040,1042,1044],{"class":67,"line":126},[65,1041,973],{"class":74},[65,1043,976],{"class":110},[65,1045,922],{"class":89},[65,1047,1048,1050,1052,1054,1056],{"class":67,"line":140},[65,1049,105],{"class":71},[65,1051,933],{"class":74},[65,1053,111],{"class":110},[65,1055,114],{"class":89},[65,1057,117],{"class":85},[65,1059,1060],{"class":67,"line":146},[65,1061,123],{"class":71},[65,1063,1064,1066,1068,1070],{"class":67,"line":161},[65,1065,149],{"class":81},[65,1067,86],{"class":85},[65,1069,966],{"class":154},[65,1071,158],{"class":85},[416,1073,419],{"id":1074},"output-4",[56,1076,1077],{"className":58,"code":600,"language":60,"meta":61,"style":61},[17,1078,1079,1083,1087,1091],{"__ignoreMap":61},[65,1080,1081],{"class":67,"line":68},[65,1082,607],{"class":89},[65,1084,1085],{"class":67,"line":102},[65,1086,612],{"class":89},[65,1088,1089],{"class":67,"line":120},[65,1090,432],{"class":89},[65,1092,1093],{"class":67,"line":126},[65,1094,621],{"class":89},[14,1096,1097,1098,1101,1102,1105,1106,1108],{},"In this example, ",[17,1099,1100],{},"count += 1"," happens ",[520,1103,1104],{},"before"," ",[17,1107,23],{},", so the loop still makes progress.",[14,1110,1111,1112,20,1116,242],{},"If you want to review loop conditions first, see ",[234,1113,1115],{"href":1114},"\u002Flearn\u002Fpython-if-statements-explained\u002F","Python if statements explained",[234,1117,241],{"href":240},[51,1119,1121],{"id":1120},"common-beginner-mistakes","Common beginner mistakes",[14,1123,1124,1125,20,1127,242],{},"These are some common problems with ",[17,1126,19],{},[17,1128,23],{},[416,1130,1132],{"id":1131},"using-break-when-continue-was-needed","Using break when continue was needed",[14,1134,1135,1136,1138],{},"If you use ",[17,1137,19],{},", the whole loop stops.",[56,1140,1142],{"className":58,"code":1141,"language":60,"meta":61,"style":61},"for i in range(1, 6):\n    if i == 3:\n        break\n    print(i)\n",[17,1143,1144,1164,1176,1180],{"__ignoreMap":61},[65,1145,1146,1148,1150,1152,1154,1156,1158,1160,1162],{"class":67,"line":68},[65,1147,44],{"class":71},[65,1149,727],{"class":74},[65,1151,78],{"class":71},[65,1153,82],{"class":81},[65,1155,86],{"class":85},[65,1157,90],{"class":89},[65,1159,93],{"class":85},[65,1161,96],{"class":89},[65,1163,99],{"class":85},[65,1165,1166,1168,1170,1172,1174],{"class":67,"line":102},[65,1167,105],{"class":71},[65,1169,727],{"class":74},[65,1171,111],{"class":110},[65,1173,114],{"class":89},[65,1175,117],{"class":85},[65,1177,1178],{"class":67,"line":120},[65,1179,143],{"class":71},[65,1181,1182,1184,1186,1188],{"class":67,"line":126},[65,1183,149],{"class":81},[65,1185,86],{"class":85},[65,1187,766],{"class":154},[65,1189,158],{"class":85},[14,1191,1192],{},"This prints:",[56,1194,1196],{"className":58,"code":1195,"language":60,"meta":61,"style":61},"1\n2\n",[17,1197,1198,1202],{"__ignoreMap":61},[65,1199,1200],{"class":67,"line":68},[65,1201,607],{"class":89},[65,1203,1204],{"class":67,"line":102},[65,1205,612],{"class":89},[14,1207,1208,1209,1212,1213,1215],{},"If you only wanted to skip ",[17,1210,1211],{},"3",", use ",[17,1214,23],{}," instead.",[416,1217,1219],{"id":1218},"using-continue-when-break-was-needed","Using continue when break was needed",[14,1221,1135,1222,1224],{},[17,1223,23],{},", the loop keeps going.",[56,1226,1228],{"className":58,"code":1227,"language":60,"meta":61,"style":61},"for i in range(1, 6):\n    if i == 3:\n        continue\n    print(i)\n",[17,1229,1230,1250,1262,1266],{"__ignoreMap":61},[65,1231,1232,1234,1236,1238,1240,1242,1244,1246,1248],{"class":67,"line":68},[65,1233,44],{"class":71},[65,1235,727],{"class":74},[65,1237,78],{"class":71},[65,1239,82],{"class":81},[65,1241,86],{"class":85},[65,1243,90],{"class":89},[65,1245,93],{"class":85},[65,1247,96],{"class":89},[65,1249,99],{"class":85},[65,1251,1252,1254,1256,1258,1260],{"class":67,"line":102},[65,1253,105],{"class":71},[65,1255,727],{"class":74},[65,1257,111],{"class":110},[65,1259,114],{"class":89},[65,1261,117],{"class":85},[65,1263,1264],{"class":67,"line":120},[65,1265,123],{"class":71},[65,1267,1268,1270,1272,1274],{"class":67,"line":126},[65,1269,149],{"class":81},[65,1271,86],{"class":85},[65,1273,766],{"class":154},[65,1275,158],{"class":85},[14,1277,1192],{},[56,1279,1280],{"className":58,"code":600,"language":60,"meta":61,"style":61},[17,1281,1282,1286,1290,1294],{"__ignoreMap":61},[65,1283,1284],{"class":67,"line":68},[65,1285,607],{"class":89},[65,1287,1288],{"class":67,"line":102},[65,1289,612],{"class":89},[65,1291,1292],{"class":67,"line":120},[65,1293,432],{"class":89},[65,1295,1296],{"class":67,"line":126},[65,1297,621],{"class":89},[14,1299,1300,1301,1212,1303,242],{},"If you wanted the loop to stop at ",[17,1302,1211],{},[17,1304,19],{},[416,1306,1308],{"id":1307},"placing-code-after-break-or-continue-and-expecting-it-to-run","Placing code after break or continue and expecting it to run",[14,1310,1311],{},"Code after these statements in the same iteration will not run.",[56,1313,1315],{"className":58,"code":1314,"language":60,"meta":61,"style":61},"for i in range(3):\n    if i == 1:\n        continue\n        print(\"This will never print\")\n    print(i)\n",[17,1316,1317,1333,1346,1350,1365],{"__ignoreMap":61},[65,1318,1319,1321,1323,1325,1327,1329,1331],{"class":67,"line":68},[65,1320,44],{"class":71},[65,1322,727],{"class":74},[65,1324,78],{"class":71},[65,1326,82],{"class":81},[65,1328,86],{"class":85},[65,1330,1211],{"class":89},[65,1332,99],{"class":85},[65,1334,1335,1337,1339,1341,1344],{"class":67,"line":102},[65,1336,105],{"class":71},[65,1338,727],{"class":74},[65,1340,111],{"class":110},[65,1342,1343],{"class":89}," 1",[65,1345,117],{"class":85},[65,1347,1348],{"class":67,"line":120},[65,1349,123],{"class":71},[65,1351,1352,1354,1356,1358,1361,1363],{"class":67,"line":126},[65,1353,356],{"class":81},[65,1355,86],{"class":85},[65,1357,362],{"class":361},[65,1359,1360],{"class":365},"This will never print",[65,1362,362],{"class":361},[65,1364,158],{"class":85},[65,1366,1367,1369,1371,1373],{"class":67,"line":140},[65,1368,149],{"class":81},[65,1370,86],{"class":85},[65,1372,766],{"class":154},[65,1374,158],{"class":85},[14,1376,1377,1378,1381,1382,1384],{},"The ",[17,1379,1380],{},"print(\"This will never print\")"," line is skipped because ",[17,1383,23],{}," already jumped to the next iteration.",[14,1386,1387,1388,242],{},"The same idea applies to ",[17,1389,19],{},[416,1391,1393],{"id":1392},"creating-an-infinite-while-loop-by-not-changing-the-condition-value","Creating an infinite while loop by not changing the condition value",[14,1395,1396],{},"This is a very common mistake.",[56,1398,1400],{"className":58,"code":1399,"language":60,"meta":61,"style":61},"count = 0\n\nwhile count \u003C 5:\n    if count == 2:\n        continue\n    print(count)\n    count += 1\n",[17,1401,1402,1410,1414,1426,1439,1443,1453],{"__ignoreMap":61},[65,1403,1404,1406,1408],{"class":67,"line":68},[65,1405,917],{"class":74},[65,1407,293],{"class":110},[65,1409,1020],{"class":89},[65,1411,1412],{"class":67,"line":102},[65,1413,165],{"emptyLinePlaceholder":164},[65,1415,1416,1418,1420,1422,1424],{"class":67,"line":120},[65,1417,48],{"class":71},[65,1419,933],{"class":74},[65,1421,1033],{"class":110},[65,1423,135],{"class":89},[65,1425,117],{"class":85},[65,1427,1428,1430,1432,1434,1437],{"class":67,"line":126},[65,1429,105],{"class":71},[65,1431,933],{"class":74},[65,1433,111],{"class":110},[65,1435,1436],{"class":89}," 2",[65,1438,117],{"class":85},[65,1440,1441],{"class":67,"line":140},[65,1442,123],{"class":71},[65,1444,1445,1447,1449,1451],{"class":67,"line":146},[65,1446,149],{"class":81},[65,1448,86],{"class":85},[65,1450,966],{"class":154},[65,1452,158],{"class":85},[65,1454,1455,1457,1459],{"class":67,"line":161},[65,1456,973],{"class":74},[65,1458,976],{"class":110},[65,1460,922],{"class":89},[14,1462,1463,1464,242],{},"This loop becomes infinite when ",[17,1465,1466],{},"count == 2",[14,1468,1469],{},"Why?",[26,1471,1472,1480,1485,1490,1497],{},[29,1473,1474,1476,1477],{},[17,1475,966],{}," is ",[17,1478,1479],{},"2",[29,1481,1482,1484],{},[17,1483,23],{}," runs",[29,1486,1487,1489],{},[17,1488,1100],{}," is skipped",[29,1491,1492,1494,1495],{},[17,1493,966],{}," stays ",[17,1496,1479],{},[29,1498,1499],{},"the loop condition stays true forever",[14,1501,1502,1503,1505],{},"A correct version updates the counter before ",[17,1504,23],{}," can happen:",[56,1507,1508],{"className":58,"code":1009,"language":60,"meta":61,"style":61},[17,1509,1510,1518,1522,1534,1542,1554,1558],{"__ignoreMap":61},[65,1511,1512,1514,1516],{"class":67,"line":68},[65,1513,917],{"class":74},[65,1515,293],{"class":110},[65,1517,1020],{"class":89},[65,1519,1520],{"class":67,"line":102},[65,1521,165],{"emptyLinePlaceholder":164},[65,1523,1524,1526,1528,1530,1532],{"class":67,"line":120},[65,1525,48],{"class":71},[65,1527,933],{"class":74},[65,1529,1033],{"class":110},[65,1531,135],{"class":89},[65,1533,117],{"class":85},[65,1535,1536,1538,1540],{"class":67,"line":126},[65,1537,973],{"class":74},[65,1539,976],{"class":110},[65,1541,922],{"class":89},[65,1543,1544,1546,1548,1550,1552],{"class":67,"line":140},[65,1545,105],{"class":71},[65,1547,933],{"class":74},[65,1549,111],{"class":110},[65,1551,114],{"class":89},[65,1553,117],{"class":85},[65,1555,1556],{"class":67,"line":146},[65,1557,123],{"class":71},[65,1559,1560,1562,1564,1566],{"class":67,"line":161},[65,1561,149],{"class":81},[65,1563,86],{"class":85},[65,1565,966],{"class":154},[65,1567,158],{"class":85},[51,1569,1571],{"id":1570},"when-to-use-each-statement","When to use each statement",[14,1573,193,1574,671],{},[17,1575,19],{},[26,1577,1578,1581,1584],{},[29,1579,1580],{},"you are searching for the first match",[29,1582,1583],{},"the loop should end as soon as something happens",[29,1585,1586],{},"continuing would waste time",[14,1588,193,1589,671],{},[17,1590,23],{},[26,1592,1593,1596,1599],{},[29,1594,1595],{},"you want to skip unwanted values",[29,1597,1598],{},"you are filtering items during a loop",[29,1600,1601],{},"the loop should keep running after one special case",[14,1603,1604,1605,1607],{},"Do not use them if they make the code harder to read. In some cases, a simple ",[17,1606,476],{}," statement is clearer.",[14,1609,1610,1611,907],{},"For example, this can be easier to read than using ",[17,1612,23],{},[56,1614,1616],{"className":58,"code":1615,"language":60,"meta":61,"style":61},"for number in range(1, 6):\n    if number != 3:\n        print(number)\n",[17,1617,1618,1638,1651],{"__ignoreMap":61},[65,1619,1620,1622,1624,1626,1628,1630,1632,1634,1636],{"class":67,"line":68},[65,1621,44],{"class":71},[65,1623,75],{"class":74},[65,1625,78],{"class":71},[65,1627,82],{"class":81},[65,1629,86],{"class":85},[65,1631,90],{"class":89},[65,1633,93],{"class":85},[65,1635,96],{"class":89},[65,1637,99],{"class":85},[65,1639,1640,1642,1644,1647,1649],{"class":67,"line":102},[65,1641,105],{"class":71},[65,1643,75],{"class":74},[65,1645,1646],{"class":110},"!=",[65,1648,114],{"class":89},[65,1650,117],{"class":85},[65,1652,1653,1655,1657,1659],{"class":67,"line":120},[65,1654,356],{"class":81},[65,1656,86],{"class":85},[65,1658,155],{"class":154},[65,1660,158],{"class":85},[51,1662,1664],{"id":1663},"common-causes-of-confusion","Common causes of confusion",[14,1666,1667],{},"Beginners often get stuck because of one of these:",[26,1669,1670,1673,1679,1685],{},[29,1671,1672],{},"confusing “stop the loop” with “skip this iteration”",[29,1674,1675,1676,1678],{},"not realizing that ",[17,1677,19],{}," only exits the nearest loop",[29,1680,1681,1682,1684],{},"forgetting that ",[17,1683,23],{}," skips the remaining code in the current iteration",[29,1686,1687,1688,1690,1691,1693],{},"using ",[17,1689,23],{}," in a ",[17,1692,48],{}," loop before updating the counter",[14,1695,1696],{},"If your loop is not behaving as expected, add simple debug prints:",[56,1698,1700],{"className":58,"code":1699,"language":60,"meta":61,"style":61},"print(i)\nprint('before continue', i)\nprint('before break', i)\n",[17,1701,1702,1712,1733],{"__ignoreMap":61},[65,1703,1704,1706,1708,1710],{"class":67,"line":68},[65,1705,403],{"class":81},[65,1707,86],{"class":85},[65,1709,766],{"class":154},[65,1711,158],{"class":85},[65,1713,1714,1716,1718,1721,1724,1726,1728,1731],{"class":67,"line":102},[65,1715,403],{"class":81},[65,1717,86],{"class":85},[65,1719,1720],{"class":361},"'",[65,1722,1723],{"class":365},"before continue",[65,1725,1720],{"class":361},[65,1727,93],{"class":85},[65,1729,1730],{"class":154}," i",[65,1732,158],{"class":85},[65,1734,1735,1737,1739,1741,1744,1746,1748,1750],{"class":67,"line":120},[65,1736,403],{"class":81},[65,1738,86],{"class":85},[65,1740,1720],{"class":361},[65,1742,1743],{"class":365},"before break",[65,1745,1720],{"class":361},[65,1747,93],{"class":85},[65,1749,1730],{"class":154},[65,1751,158],{"class":85},[14,1753,1754,1755,1757,1758,1760],{},"These help you see which values the loop is reaching and whether ",[17,1756,19],{}," or ",[17,1759,23],{}," runs before later code.",[51,1762,1764],{"id":1763},"faq","FAQ",[416,1766,1768],{"id":1767},"what-is-the-difference-between-break-and-continue-in-python","What is the difference between break and continue in Python?",[14,1770,1771,1773,1774,1776],{},[17,1772,19],{}," ends the loop completely. ",[17,1775,23],{}," skips the current iteration and moves to the next one.",[416,1778,1780],{"id":1779},"can-i-use-break-in-a-for-loop-and-a-while-loop","Can I use break in a for loop and a while loop?",[14,1782,1783,1784,1786,1787,45,1789,892],{},"Yes. ",[17,1785,19],{}," works in both ",[17,1788,44],{},[17,1790,48],{},[416,1792,1794],{"id":1793},"does-continue-stop-the-loop","Does continue stop the loop?",[14,1796,1797],{},"No. It only skips the rest of the current iteration.",[416,1799,1801],{"id":1800},"why-did-my-while-loop-become-infinite-after-using-continue","Why did my while loop become infinite after using continue?",[14,1803,1804,1805,1807],{},"The loop variable may not have been updated before ",[17,1806,23],{}," ran, so the condition never changed.",[416,1809,1811],{"id":1810},"does-break-exit-nested-loops","Does break exit nested loops?",[14,1813,1814],{},"No. It only exits the nearest loop that contains it.",[51,1816,1818],{"id":1817},"see-also","See also",[26,1820,1821,1825,1829,1833,1839],{},[29,1822,1823],{},[234,1824,237],{"href":236},[29,1826,1827],{},[234,1828,241],{"href":240},[29,1830,1831],{},[234,1832,1115],{"href":1114},[29,1834,1835],{},[234,1836,1838],{"href":1837},"\u002Flearn\u002Fpython-pass-statement-explained\u002F","Python pass statement explained",[29,1840,1841],{},[234,1842,1844],{"href":1843},"\u002Fhow-to\u002Fhow-to-loop-through-a-list-in-python\u002F","How to loop through a list in Python",[1846,1847,1848],"style",{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}",{"title":61,"searchDepth":102,"depth":102,"links":1850},[1851,1852,1853,1854,1858,1859,1863,1866,1870,1876,1877,1878,1885],{"id":53,"depth":102,"text":54},{"id":202,"depth":102,"text":203},{"id":245,"depth":102,"text":246},{"id":276,"depth":102,"text":277,"children":1855},[1856,1857],{"id":418,"depth":120,"text":419},{"id":462,"depth":120,"text":463},{"id":505,"depth":102,"text":506},{"id":539,"depth":102,"text":540,"children":1860},[1861,1862],{"id":597,"depth":120,"text":419},{"id":624,"depth":120,"text":625},{"id":650,"depth":102,"text":651,"children":1864},[1865],{"id":836,"depth":120,"text":419},{"id":885,"depth":102,"text":886,"children":1867},[1868,1869],{"id":981,"depth":120,"text":419},{"id":1074,"depth":120,"text":419},{"id":1120,"depth":102,"text":1121,"children":1871},[1872,1873,1874,1875],{"id":1131,"depth":120,"text":1132},{"id":1218,"depth":120,"text":1219},{"id":1307,"depth":120,"text":1308},{"id":1392,"depth":120,"text":1393},{"id":1570,"depth":102,"text":1571},{"id":1663,"depth":102,"text":1664},{"id":1763,"depth":102,"text":1764,"children":1879},[1880,1881,1882,1883,1884],{"id":1767,"depth":120,"text":1768},{"id":1779,"depth":120,"text":1780},{"id":1793,"depth":120,"text":1794},{"id":1800,"depth":120,"text":1801},{"id":1810,"depth":120,"text":1811},{"id":1817,"depth":102,"text":1818},"Master python break and continue statements in our comprehensive Python beginner guide.","md",{},"\u002Flearn\u002Fpython-break-and-continue-statements",{"title":5,"description":1886},"learn\u002Fpython-break-and-continue-statements","F17znJA2g6m_VSHb5fsJB1wP4sOizQCBQcyKDlFk-1A",1777585495030]