{"id":741,"date":"2021-04-29T12:17:28","date_gmt":"2021-04-29T06:47:28","guid":{"rendered":"https:\/\/cbsepython.in\/?p=741"},"modified":"2023-12-28T09:56:22","modified_gmt":"2023-12-28T04:26:22","slug":"while-loop-in-python","status":"publish","type":"post","link":"https:\/\/cbsepython.in\/while-loop-in-python\/","title":{"rendered":"While Loop In Python Notes for Class 11-12"},"content":{"rendered":"<h2>While Loop In Python:<\/h2>\n<p>A <code>while<\/code> loop in Python is used to execute a block of code repeatedly until a certain condition is met. Here is the basic syntax of a <code>while<\/code> loop:<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">while condition:\r\n    # code to be executed\r\n<\/pre>\n<p>The condition is checked at the beginning of each iteration. If it is <code>True<\/code>, the code inside the loop is executed. Afterward, the condition is checked again, and the loop continues until the condition becomes <code>False<\/code>.<\/p>\n<p>Here is an example of a <code>while<\/code> loop that prints the numbers from 1 to 5:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">count = 1\r\nwhile count &lt;= 5:\r\n    print(count)\r\n    count += 1\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>In this example, the <code>count<\/code> variable is initialized to 1. The condition <code>count &lt;= 5<\/code> is checked at the beginning of each iteration. If it is <code>True<\/code>, the current value of <code>count<\/code> is printed to the console and <code>count<\/code> is incremented by 1. After 5 iterations, the value of <code>count<\/code> is 6, which is not less than or equal to 5, so the loop ends.<\/p>\n<p>You can also use the <code>break<\/code> statement inside a <code>while<\/code> loop to terminate the loop prematurely. For example, the following code uses a <code>while<\/code> loop to repeatedly ask the user for input until they enter a non-empty string or type &#8220;quit&#8221;:<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">while True:\r\n    user_input = input(\"Enter something (or type 'quit' to exit): \")\r\n    if user_input == \"\":\r\n        print(\"You didn't enter anything!\")\r\n    elif user_input == \"quit\":\r\n        break\r\n    else:\r\n        print(\"You entered:\", user_input)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>In this example, the <code>while<\/code> loop runs indefinitely (<code>True<\/code> is always <code>True<\/code>). However, the <code>break<\/code> statement is used to exit the loop when the user types &#8220;quit&#8221;.<\/p>\n<p>&nbsp;<\/p>\n<p>You can use the <code>continue<\/code> statement to skip the rest of the current iteration and move on to the next one. For example, the following code uses a <code>while<\/code> loop to print the numbers from 1 to 10, skipping the even numbers:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">count = 1\r\nwhile count &lt;= 10:\r\n    if count % 2 == 0:\r\n        count += 1\r\n        continue\r\n    print(count)\r\n    count += 1\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>In this example, the <code>if<\/code> statement checks if the current value of <code>count<\/code> is even. If it is, the <code>continue<\/code> statement is executed, which skips the rest of the current iteration (including the <code>print<\/code> statement) and moves on to the next iteration.<\/p>\n<ul>\n<li>You can use the <code>else<\/code> clause to specify a block of code that should be executed when the loop terminates normally (i.e., when the condition becomes <code>False<\/code>). For example, the following code uses a <code>while<\/code> loop to find the smallest power of 2 that is greater than or equal to a given number:<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">num = 20\r\npower = 1\r\nwhile power &lt; num:\r\n    power *= 2\r\nelse:\r\n    print(\"The smallest power of 2 that is greater than or equal to\", num, \"is\", power)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>In this example, the <code>while<\/code> loop multiplies <code>power<\/code> by 2 on each iteration until it is greater than or equal to <code>num<\/code>. When the loop terminates (because the condition <code>power &lt; num<\/code> is no longer <code>True<\/code>), the <code>else<\/code> block is executed, which prints a message to the console.<\/p>\n<ul>\n<li>You can use the <code>while-else<\/code> construct to combine the <code>while<\/code> loop and <code>else<\/code> clause into a single statement. The <code>else<\/code> block is executed when the condition becomes <code>False<\/code>, just like in the previous example. However, if you use the <code>break<\/code> statement to exit the loop prematurely, the <code>else<\/code> block will not be executed. For example, the following code uses a <code>while-else<\/code> loop to print the numbers from 1 to 5:<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">count = 1\r\nwhile count &lt;= 5:\r\n    print(count)\r\n    count += 1\r\nelse:\r\n    print(\"Finished!\")\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>In this example, the <code>while<\/code> loop prints the numbers from 1 to 5 and then terminates normally (because the condition <code>count &lt;= 5<\/code> is no longer <code>True<\/code>). The <code>else<\/code> block is then executed, which prints a message to the console.<\/p>\n<h3><\/h3>\n<h3>Also Check: <a href=\"https:\/\/cbsepython.in\/while-loop-exercises-for-cbse-computer-science\/\">While loop exercises for CBSE Computer Science Students.<\/a><\/h3>\n","protected":false},"excerpt":{"rendered":"<p>While Loop In Python: A while loop in Python is used to execute a block of code repeatedly until a certain condition is met. Here is the basic syntax of a while loop: &nbsp; while condition: # code to be executed The condition is checked at the beginning of each iteration. If it is True, [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-741","post","type-post","status-publish","format-standard","hentry","category-cbse-computer-science-with-python-class-12"],"_links":{"self":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/741","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/comments?post=741"}],"version-history":[{"count":0,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/741\/revisions"}],"wp:attachment":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/media?parent=741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/categories?post=741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/tags?post=741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}