<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rohitvishwakarma on Medium]]></title>
        <description><![CDATA[Stories by Rohitvishwakarma on Medium]]></description>
        <link>https://medium.com/@rohitvishwakarma4975?source=rss-477f658bfe42------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*a2R8dLXdanx4eGfG</url>
            <title>Stories by Rohitvishwakarma on Medium</title>
            <link>https://medium.com/@rohitvishwakarma4975?source=rss-477f658bfe42------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 22:21:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rohitvishwakarma4975/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Control Flow in Python: A Beginner’s Guide]]></title>
            <link>https://medium.com/@rohitvishwakarma4975/control-flow-in-python-a-beginners-guide-37440fe17815?source=rss-477f658bfe42------2</link>
            <guid isPermaLink="false">https://medium.com/p/37440fe17815</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[control-flow]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python-programming]]></category>
            <dc:creator><![CDATA[Rohitvishwakarma]]></dc:creator>
            <pubDate>Tue, 25 Aug 2026 05:06:06 GMT</pubDate>
            <atom:updated>2026-10-06T06:04:30.322Z</atom:updated>
            <content:encoded><![CDATA[<p>Python is known for its simple and readable syntax, making it one of the most popular programming languages for beginners and experienced developers alike. One of the fundamental concepts every Python programmer should understand is <strong>control flow</strong>.</p><p>Control flow determines the order in which statements in a program are executed. Instead of always running code from top to bottom, control flow allows a program to make decisions, repeat tasks, and skip or stop certain operations based on specific conditions.</p><p>In this blog, we’ll explore the main control flow statements in Python with simple examples.</p><h3>What Is Control Flow?</h3><p>Control flow refers to the order in which a program executes its instructions.</p><p>For example, consider a program that checks whether a person is old enough to drive:</p><pre>age = 20</pre><pre>if age &gt;= 18:<br>    print(&quot;You can drive.&quot;)</pre><p>Here, Python checks the condition age &gt;= 18. If the condition is true, the print() statement is executed.</p><p>Control flow in Python mainly includes:</p><ul><li>Conditional statements</li><li>Loops</li><li>Loop control statements</li><li>Exception handling</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/907/1*KxJocdenNTEcnE8VkGpXWQ.png" /></figure><h3>1. Conditional Statements</h3><p>Conditional statements allow a program to make decisions. Python provides if, elif, and else statements for this purpose.</p><h3>The if Statement</h3><p>The if statement executes a block of code only when a condition is true.</p><pre>temperature = 30</pre><pre>if temperature &gt; 25:<br>    print(&quot;It is a hot day.&quot;)</pre><p>Since the temperature is greater than 25, the message is displayed.</p><h3>The if-else Statement</h3><p>The else statement provides an alternative when the if condition is false.</p><pre>age = 16</pre><pre>if age &gt;= 18:<br>    print(&quot;You are an adult.&quot;)<br>else:<br>    print(&quot;You are a minor.&quot;)</pre><p>The program chooses one of the two blocks depending on the value of age.</p><h3>The if-elif-else Statement</h3><p>When there are multiple conditions, we can use elif, which means “else if.”</p><pre>marks = 75</pre><pre>if marks &gt;= 90:<br>    print(&quot;Grade A&quot;)<br>elif marks &gt;= 75:<br>    print(&quot;Grade B&quot;)<br>elif marks &gt;= 50:<br>    print(&quot;Grade C&quot;)<br>else:<br>    print(&quot;Fail&quot;)</pre><p>Python checks the conditions from top to bottom and executes the first condition that is true.</p><h3>2. Loops in Python</h3><p>Loops are used when we need to execute a block of code repeatedly.</p><p>Python mainly provides two types of loops:</p><ul><li>for loop</li><li>while loop</li></ul><h3>The for Loop</h3><p>A for loop is commonly used to iterate over a sequence such as a list, string, or range.</p><pre>for number in range(1, 6):<br>    print(number)</pre><p>Output:</p><pre>1<br>2<br>3<br>4<br>5</pre><p>The loop runs once for each value generated by range().</p><p>A for loop can also be used with lists:</p><pre>fruits = [&quot;Apple&quot;, &quot;Banana&quot;, &quot;Mango&quot;]</pre><pre>for fruit in fruits:<br>    print(fruit)</pre><h3>The while Loop</h3><p>A while loop repeatedly executes a block of code as long as its condition remains true.</p><pre>count = 1</pre><pre>while count &lt;= 5:<br>    print(count)<br>    count += 1</pre><p>The loop stops when count becomes greater than 5.</p><p>It is important to update the condition inside a while loop when necessary. Otherwise, the program may create an <strong>infinite loop</strong>.</p><h3>3. Loop Control Statements</h3><p>Python provides special statements that change the normal behavior of loops.</p><h3>break</h3><p>The break statement immediately terminates a loop.</p><pre>for number in range(1, 10):<br>    if number == 5:<br>        break<br>    print(number)</pre><p>Output:</p><pre>1<br>2<br>3<br>4</pre><p>When number becomes 5, the loop stops.</p><h3>continue</h3><p>The continue statement skips the current iteration and moves to the next one.</p><pre>for number in range(1, 6):<br>    if number == 3:<br>        continue<br>    print(number)</pre><p>Output:</p><pre>1<br>2<br>4<br>5</pre><p>The number 3 is skipped.</p><h3>pass</h3><p>The pass statement does nothing. It is useful as a placeholder when a statement is syntactically required but no action is needed yet.</p><pre>for number in range(5):<br>    pass</pre><p>It can also be useful while developing a program when you plan to add functionality later.</p><h3>4. Nested Control Flow</h3><p>Control flow statements can be placed inside other control flow statements. This is called <strong>nested control flow</strong>.</p><p>For example:</p><pre>for number in range(1, 6):<br>    if number % 2 == 0:<br>        print(number, &quot;is even&quot;)</pre><p>Here, an if statement is placed inside a for loop.</p><p>Nested structures are useful for solving more complex problems, although excessive nesting can make code harder to read.</p><h3>5. Exception Handling</h3><p>Another important part of controlling program execution is exception handling. Python provides try, except, else, and finally for handling errors without necessarily crashing the entire program.</p><pre>try:<br>    number = int(input(&quot;Enter a number: &quot;))<br>    print(10 / number)<br>except ValueError:<br>    print(&quot;Please enter a valid number.&quot;)<br>except ZeroDivisionError:<br>    print(&quot;Cannot divide by zero.&quot;)</pre><p>If the user enters invalid input or zero, the appropriate except block handles the problem.</p><h3>Why Is Control Flow Important?</h3><p>Control flow is essential because real-world programs need to make decisions and perform repeated tasks.</p><p>For example, control flow can help you:</p><ul><li>Validate user input</li><li>Process lists of data</li><li>Build menus</li><li>Create games</li><li>Automate repetitive tasks</li><li>Handle different program conditions</li><li>Respond to errors</li><li>Implement business logic</li></ul><p>Without control flow, programs would have very limited ability to respond intelligently to different situations.</p><h3>Best Practices for Python Control Flow</h3><p>When writing control flow statements, keep your code simple and readable.</p><h3>Use Meaningful Conditions</h3><p>Instead of complicated expressions, use conditions that clearly communicate your intention.</p><h3>Avoid Unnecessary Nesting</h3><p>Deeply nested if statements and loops can make code difficult to understand. Consider simplifying the logic when possible.</p><h3>Keep Loops Manageable</h3><p>Make sure while loops have a clear stopping condition to avoid accidental infinite loops.</p><h3>Use break and continue Carefully</h3><p>These statements are useful, but excessive use can make the flow of a program difficult to follow.</p><h3>Follow Python’s Indentation Rules</h3><p>Python uses indentation to define blocks of code.</p><pre>if age &gt;= 18:<br>    print(&quot;Eligible&quot;)</pre><p>Incorrect indentation can cause an error or change the meaning of your program.</p><h3>Conclusion</h3><p>Control flow is one of the most important concepts in Python programming. It allows programs to make decisions, repeat operations, skip instructions, stop loops, and handle unexpected situations.</p><p>By mastering if, elif, else, for, while, break, continue, and exception handling, beginners can build a strong foundation for writing useful Python programs.</p><p>The best way to learn control flow is through practice. Start with simple conditions and loops, then gradually solve more complex problems. Once you understand how Python controls the execution of your code, you’ll be ready to explore more advanced programming concepts.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=37440fe17815" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>