{"id":7128,"date":"2024-03-19T06:42:51","date_gmt":"2024-03-19T06:42:51","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=7128"},"modified":"2024-03-19T06:42:51","modified_gmt":"2024-03-19T06:42:51","slug":"python-control-flow","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/python-control-flow\/","title":{"rendered":"Python Control Flow"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#what-is-a-decision-making-statement\">What is a Decision-Making Statement<\/a><\/li><li ><a href=\"#what-is-the-if-statement\">What is the if statement?<\/a><\/li><li ><a href=\"#what-is-the-match-statement\">What is the match statement?<\/a><\/li><li ><a href=\"#what-is-a-loop-or-iteration-statement\">What is a Loop or iteration statement?<\/a><\/li><li ><a href=\"#what-is-the-for-loop\">What is the for Loop?<\/a><\/li><li ><a href=\"#what-is-the-while-loop\">What is the while Loop?<\/a><\/li><li ><a href=\"#conclusion\">Conclusion<\/a><\/li><li ><a href=\"#python-control-flow-fa-qs\">Python  Control Flow- FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<p>In programming, it is known to use various tools to make our <a href=\"https:\/\/www.skillvertex.com\/blog\/computer-science-engineering-salary\/\" data-type=\"post\" data-id=\"1598\">computer program<\/a>s more intelligent and adaptable.  Hence, if there are more conditions to check, use &#8216;<strong>elif<\/strong>&#8216; (else if), and &#8216;<strong>else<\/strong>&#8216; provides a default action if none of the conditions are true. Then, there are loops (&#8216;for&#8217; and &#8216;while&#8217;) that help the robot repeat tasks. <\/p>\n\n\n\n<p>Read this article to learn more about <a href=\"https:\/\/www.skillvertex.com\/blog\/python-interview-questions\/\" data-type=\"post\" data-id=\"3614\">Python<\/a> Control Flow.  Therefore, there are two types of control flow statements in any <a href=\"https:\/\/www.skillvertex.com\/blog\/which-programming-language-is-best-for-getting-job\/\" data-type=\"post\" data-id=\"34\">programming language<\/a>.<\/p>\n\n\n\n<p> &#8216;<strong>For&#8217;<\/strong> loops are like telling the robot to do something a certain number of times, and &#8216;<strong>while&#8217;<\/strong> loops are for tasks that keep going until a certain condition is met. We also use functions to store sets of instructions that we can reuse whenever we want. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-a-decision-making-statement\">What is a Decision-Making Statement<\/h2>\n\n\n\n<p>Decision-making statements are considered instructions that are used in Python Programs which will allow them to decide the execution of the Program. These statements will be executed due to the <a href=\"https:\/\/www.skillvertex.com\/blog\/bool-in-c\/\" data-type=\"post\" data-id=\"1813\">Boolean expression<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-if-statement\">What is the if statement?<\/h2>\n\n\n\n<p>Python will give &#8216;<strong>&#8216;<a href=\"https:\/\/www.skillvertex.com\/blog\/c-ifelse-statement\/\" data-type=\"post\" data-id=\"2270\">if&#8230;elif&#8217;<\/a><\/strong><a href=\"https:\/\/www.skillvertex.com\/blog\/c-ifelse-statement\/\" data-type=\"post\" data-id=\"2270\">&#8216; <\/a>control statements according to the decision-making. Let us look into the example provided below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Get a number from the user\nnumber = int(input(\"Enter a number: \"))\n\n# Check the number and print a message based on its value\nif number &gt; 0:\n    print(\"The number is positive.\")\nelif number == 0:\n    print(\"The number is zero.\")\nelse:\n    print(\"The number is negative.\")\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter a number: -3\nThe number isnegative.\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-match-statement\">What is the match statement?<\/h2>\n\n\n\n<p><a href=\"https:\/\/www.skillvertex.com\/blog\/mern-stack-vs-python\/\" data-type=\"post\" data-id=\"4258\">Python<\/a> will use a match-case statement and this is executed according to the decision-making process. The example provided below will help you to understand the match statement.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\ndef check_number(number):\n    match number:\n        case 0:\n            return \"The number is zero.\"\n        case _ if number &gt; 0:\n            return \"The number is positive.\"\n        case _:\n            return \"The number is negative.\"\n\n# Get a number from the user\nuser_input = int(input(\"Enter a number: \"))\nresult = check_number(user_input)\nprint(result)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter a number: 0\nThe number is zero.\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-a-loop-or-iteration-statement\">What is a Loop or iteration statement?<\/h2>\n\n\n\n<p>It is known that each process needs a group of instructions that will be run continuously.  This terminology in programming is called a <a href=\"https:\/\/www.skillvertex.com\/blog\/c-for-loop\/\" data-type=\"post\" data-id=\"2045\">loop<\/a>. Whereas, if the flow is changed the direction towards the earlier step, it will make up a loop.<\/p>\n\n\n\n<p>Hence, In computer programs, we use loops, like the &#8216;<strong>for<\/strong>&#8216; or &#8216;<strong>while<\/strong>&#8216; loop, to repeat actions until a specific condition is met. For example, if you know exactly how many times you want to repeat something, you&#8217;d use a <strong>&#8216;for&#8217; loop<\/strong>. If you&#8217;re unsure how many times, but you have a condition to stop, you&#8217;d use a &#8216;while&#8217; loop.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-for-loop\">What is the for Loop?<\/h2>\n\n\n\n<p>The example that will make use of The<a href=\"https:\/\/www.skillvertex.com\/blog\/c-loops\/\" data-type=\"post\" data-id=\"2286\"> &#8221;<\/a><strong><a href=\"https:\/\/www.skillvertex.com\/blog\/c-loops\/\" data-type=\"post\" data-id=\"2286\">for loop<\/a>&#8221;<\/strong> is provided below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example of a for loop\nfor i in range(5):\n    print(\"Iteration\", i+1)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Iteration 1\nIteration 2\nIteration 3\nIteration 4\nIteration 5\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-while-loop\">What is the while Loop?<\/h2>\n\n\n\n<p>The example provided below will illustrate the &#8221;<strong>while Loop<\/strong>&#8221;.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example of a while loop\ncount = 0\nwhile count &lt; 5:\n    print(\"Iteration\", count+1)\n    count += 1\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Iteration 1\nIteration 2\nIteration 3\nIteration 4\nIteration 5\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>In conclusion, Python&#8217;s control flow mechanisms, like conditional statements and loops, provide the tools necessary to make programs more dynamic and responsive. Conditional statements, such as <strong>&#8216;if&#8217;,<\/strong> &#8216;<strong>elif&#8217;<\/strong>, and <strong>&#8216;else<\/strong>&#8216;, enable decision-making based on different conditions.<\/p>\n\n\n\n<p> Loops, like &#8216;for&#8217; and &#8216;while&#8217;, allow for the repetition of tasks until specific conditions are met, adding flexibility to program execution.  Several examples are illustrated for a better understanding of if-else, loop, and while statement.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-control-flow-fa-qs\">Python  Control Flow- FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1707892244048\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is the control flow in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In the control flow, the interpreter will run the operations and functions, and later it will encounter them.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1707892254837\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2.  What are the 3 types of control structures in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Sequential control structure, selection control structure, and iteration control structure.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1707892265256\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is an example of a control flow?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. If the value  <code>x<\/code> is set to 1, it prints &#8220;The variable x is equal to 1.&#8221; If <code>x<\/code> is any other number, it prints &#8220;The variable x is not equal to 1.&#8221; The if and else parts help the program decide what to do based on the value of <code>x<\/code>.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>In programming, it is known to use various tools to make our computer programs more intelligent and adaptable. Hence, if there are more conditions to check, use &#8216;elif&#8216; (else if), and &#8216;else&#8216; provides a default action if none of the conditions are true. Then, there are loops (&#8216;for&#8217; and &#8216;while&#8217;) that help the robot repeat &#8230; <a title=\"Python Control Flow\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/python-control-flow\/\" aria-label=\"More on Python Control Flow\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":7132,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[907,906,57,866],"class_list":["post-7128","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-if-else-statement","tag-loop","tag-python","tag-python-tutorial","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=7128"}],"version-history":[{"count":11,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7128\/revisions"}],"predecessor-version":[{"id":8260,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7128\/revisions\/8260"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/7132"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=7128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=7128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=7128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}