Code Pumpkin

if….else statement | Python Flow Control

October 29, 2018
Posted by Dipen Adroja

Flow control is one of the important aspects of any programming language. In this article, we will learn to make decisions in a Python program using different forms of if….else statement. When we want to execute our code block if and only if a certain condition is matched, we use decision-making statements.

Python if Statement Flowchart

IF-statement

Python if Statement syntax

if test expression:

       statement(s)

Here, the program evaluates the test expression and will execute statement(s) only if the text expression is True, otherwise the statement(s) is not executed. The body of the if statement is indicated by the indentation. Body starts with an indentation and the next unindented line marks the end of the if block.

Example:


name = "Pumpkin"

if name != "":
   print("Hello "+ name);

Output:

Hello Pumpkin

Python if…else Statement flowchart

If-Else-Statement

Python if….else statement syntax

if test expression:
    Body of if
else:
    Body of else

Here, the program evaluates the test expression and will execute Body of if only if the text expression is True, otherwise the body of else will be executed.

Example:


def greet():
	if name != "":
	   print("Hello "+ name)
	else:
	   print("Hello world!!")	

name = "Pumpkin"
greet()
name = ""
greet()

Output:

Hello Pumpkin

Hello world!!

Python if…elif….else Statement flowchart

if_elif_else_statement

Python if…elif….else Statement Syntax

if test expression:
    Body of if

elif test expression1:

    Body of elif
else:
    Body of else

Here, the program evaluates the test expression and will execute Body of if only if the text expression is True

If test expression evaluates to false it will try to evaluate test expression1 and will execute `Body of elif if` the test expression1 evaluates to True else it will execute the body of else.

Example:


def greet():
	if name != "" and name != "Dipen":
	    print("Hello "+ name)
	elif name == "Dipen":
		print("Hey dude!!!")
	else:
	   print("Hello world!!")	

name = "Pumpkin"
greet()
name = "Dipen"
greet()
name = ""
greet()

Output:

Hello Pumpkin

Hey dude!!!

Hello world!!

We can also have if….elif….else statement inside another if….elif….else statement. We can have any number of nesting of these statements.

That's it for if, if…else, if….elif….else statements in python. In the next article, we will see for loop in python. 

That's all for this topic. If you guys have any suggestions or queries, feel free to drop a comment. We would be happy to add that in our post. You can also contribute your articles by creating contributor account here.

Happy Learning¬†ūüôā



About the Author


Dipen Adroja
Coder, Blogger, Wanderer, Philosopher, Curious pumpkin



Tags: ,


Comments and Queries

If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code class="java"> 
String foo = "bar";
</code></pre>
For more information on supported HTML tags in disqus comment, click here.
Total Posts : 122
follow us in feedly
Contribute Your Articles

Subscribe Us

Like Us On Facebook