|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## 1. Break and Continue\n", |
| 8 | + "In Python, `break` and `continue` statements can alter the flow of a normal loop.<br>\n", |
| 9 | + "`Break` - exits the loop completely.<br>\n", |
| 10 | + "`Continue` - exits the current iteration of the loop and continues with the next iteration." |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "markdown", |
| 15 | + "metadata": {}, |
| 16 | + "source": [ |
| 17 | + "--------\n", |
| 18 | + "\n", |
| 19 | + "**What is the use of break and continue in Python?**\n", |
| 20 | + "\n", |
| 21 | + "In Python, `break` and `continue` statements can alter the flow of a normal loop.\n", |
| 22 | + "\n", |
| 23 | + "Loops iterate over a block of code until the test expression is false, but sometimes we wish to terminate the current iteration or even the whole loop without checking test expression.\n", |
| 24 | + "\n", |
| 25 | + "The `break` and `continue` statements are used in these cases." |
| 26 | + ] |
| 27 | + }, |
| 28 | + { |
| 29 | + "cell_type": "markdown", |
| 30 | + "metadata": {}, |
| 31 | + "source": [ |
| 32 | + "## 2. Break statement\n", |
| 33 | + "\n", |
| 34 | + "The `break` statement terminates the loop containing it. Control of the program flows to the statement immediately after the body of the loop.\n", |
| 35 | + "\n", |
| 36 | + "If the `break` statement is inside a nested loop (loop inside another loop), the `break` statement will terminate the innermost loop." |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "cell_type": "markdown", |
| 41 | + "metadata": {}, |
| 42 | + "source": [ |
| 43 | + "<img src=\"../img/break_1.png\" alt=\"fishy\" class=\"bg-primary mb-1\">\n", |
| 44 | + "<h4><center>Flowchart of break statement in Python</center></h4>\n", |
| 45 | + "\n", |
| 46 | + "-----------" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "markdown", |
| 51 | + "metadata": {}, |
| 52 | + "source": [ |
| 53 | + "<img src=\"../img/break_2.png\" alt=\"fishy\" class=\"bg-primary mb-1\">\n", |
| 54 | + "<h4><center>Working of break statement in Python</center></h4>\n", |
| 55 | + "\n", |
| 56 | + "-----------" |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "code", |
| 61 | + "execution_count": 3, |
| 62 | + "metadata": {}, |
| 63 | + "outputs": [ |
| 64 | + { |
| 65 | + "name": "stdout", |
| 66 | + "output_type": "stream", |
| 67 | + "text": [ |
| 68 | + "s\n", |
| 69 | + "t\n", |
| 70 | + "r\n", |
| 71 | + "The end\n" |
| 72 | + ] |
| 73 | + } |
| 74 | + ], |
| 75 | + "source": [ |
| 76 | + "#Example of a break statement in Python\n", |
| 77 | + "for val in \"string\":\n", |
| 78 | + " if val == \"i\":\n", |
| 79 | + " break\n", |
| 80 | + " print(val)\n", |
| 81 | + "\n", |
| 82 | + "print(\"The end\")" |
| 83 | + ] |
| 84 | + }, |
| 85 | + { |
| 86 | + "cell_type": "markdown", |
| 87 | + "metadata": {}, |
| 88 | + "source": [ |
| 89 | + "----------\n", |
| 90 | + "In this program, we iterate through the \"string\" sequence. We check if the letter is i, upon which we break from the loop. Hence, we see in our output that all the letters up till i gets printed. After that, the loop terminates." |
| 91 | + ] |
| 92 | + }, |
| 93 | + { |
| 94 | + "cell_type": "markdown", |
| 95 | + "metadata": {}, |
| 96 | + "source": [ |
| 97 | + "## 3. Continue statement\n", |
| 98 | + "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." |
| 99 | + ] |
| 100 | + }, |
| 101 | + { |
| 102 | + "cell_type": "markdown", |
| 103 | + "metadata": {}, |
| 104 | + "source": [ |
| 105 | + "<img src=\"../img/continue_1.png\" alt=\"fishy\" class=\"bg-primary mb-1\">\n", |
| 106 | + "<h4><center>Flowchart of continue statement in Python</center></h4>\n", |
| 107 | + "\n", |
| 108 | + "-----------" |
| 109 | + ] |
| 110 | + }, |
| 111 | + { |
| 112 | + "cell_type": "markdown", |
| 113 | + "metadata": {}, |
| 114 | + "source": [ |
| 115 | + "<img src=\"../img/continue_2.png\" alt=\"fishy\" class=\"bg-primary mb-1\">\n", |
| 116 | + "<h4><center>Working of continue statement in Python</center></h4>\n", |
| 117 | + "\n", |
| 118 | + "-----------" |
| 119 | + ] |
| 120 | + }, |
| 121 | + { |
| 122 | + "cell_type": "code", |
| 123 | + "execution_count": 4, |
| 124 | + "metadata": {}, |
| 125 | + "outputs": [ |
| 126 | + { |
| 127 | + "name": "stdout", |
| 128 | + "output_type": "stream", |
| 129 | + "text": [ |
| 130 | + "s\n", |
| 131 | + "t\n", |
| 132 | + "r\n", |
| 133 | + "n\n", |
| 134 | + "g\n", |
| 135 | + "The end\n" |
| 136 | + ] |
| 137 | + } |
| 138 | + ], |
| 139 | + "source": [ |
| 140 | + "# Program to show the use of continue statement inside loops\n", |
| 141 | + "\n", |
| 142 | + "for val in \"string\":\n", |
| 143 | + " if val == \"i\":\n", |
| 144 | + " continue\n", |
| 145 | + " print(val)\n", |
| 146 | + "\n", |
| 147 | + "print(\"The end\")" |
| 148 | + ] |
| 149 | + }, |
| 150 | + { |
| 151 | + "cell_type": "markdown", |
| 152 | + "metadata": {}, |
| 153 | + "source": [ |
| 154 | + "-------\n", |
| 155 | + "\n", |
| 156 | + "This program is the same as the above example except the `break` statement has been replaced with `continue`.\n", |
| 157 | + "\n", |
| 158 | + "We continue with the loop, if the string is i, not executing the rest of the block. Hence, we see in our output that all the letters except i gets printed." |
| 159 | + ] |
| 160 | + }, |
| 161 | + { |
| 162 | + "cell_type": "code", |
| 163 | + "execution_count": null, |
| 164 | + "metadata": {}, |
| 165 | + "outputs": [], |
| 166 | + "source": [] |
| 167 | + } |
| 168 | + ], |
| 169 | + "metadata": { |
| 170 | + "kernelspec": { |
| 171 | + "display_name": "Python 3", |
| 172 | + "language": "python", |
| 173 | + "name": "python3" |
| 174 | + }, |
| 175 | + "language_info": { |
| 176 | + "codemirror_mode": { |
| 177 | + "name": "ipython", |
| 178 | + "version": 3 |
| 179 | + }, |
| 180 | + "file_extension": ".py", |
| 181 | + "mimetype": "text/x-python", |
| 182 | + "name": "python", |
| 183 | + "nbconvert_exporter": "python", |
| 184 | + "pygments_lexer": "ipython3", |
| 185 | + "version": "3.8.5" |
| 186 | + } |
| 187 | + }, |
| 188 | + "nbformat": 4, |
| 189 | + "nbformat_minor": 4 |
| 190 | +} |
0 commit comments