Decision Making

Decision Making




If else in Python 3


In our real life there are certain time comes where we need to take a decision based on the situation, like same as the real world in programming world also there is decision making techniques available, we define them by if else statement.

Decision making statement in programming language basically controls the flow of program execution. There are the types available in python we have mentioned below:

·        if statement
·        if..else statements
·        nested if statements
·        if-elif ladder

Lets start with the first one

if statement :



if statement is very simple to understand, we use this to check whether a block of code is true or not, if true then code will execute and program will move forward. Otherwise not.

Lets see the syntax:


If condition:
#Execute if true


As we see, condition after evaluation will be either true or false. if statement accepts boolean values – if the value is true then it will execute the block of statements below it otherwise not. We can use condition with bracket ‘(‘ ‘)’ also.

As we know that python uses indentation to identify a block. So the block under an if statement will be identified as shown in the below example:

if condition:
   statement1
statement2

# here if condition true
# then block of statement inside

# will execute

Flowchart:

python tutorial,tensorflow tutorial,flask tutorial,python 3 tutorial,python django tutorial,pyqt tutorial,best python tutorial,programiz python,selenium python tutorial





Lets check this with a piece of code as an example

i=10
if  (i>8) :
  print(“condition is true”)

print(“exit from the if block”)



output :

condition is true

(“exit from the if block”) this statement gets printed if (i<12)




if..else statements:


as we know that by the if condition alone it self a code will execute if the condition is true, otherwise it will not execute, here comes the if else statement when the else part gets executed when the if parts found to be false.

Syntax :

if (condition):
    # executes this block if
    # condition is true
else:
    # executes this block if
    # condition is false 

Flow chart :


python tutorial,tensorflow tutorial,flask tutorial,python 3 tutorial,python django tutorial,pyqt tutorial,best python tutorial,programiz python,selenium python tutorial







#program to illustrate If else statement
  
i = 18;
if (i < 14):
    print ("i is smaller than 14")
    print ("i'm in if Block")
else:
    print ("i is greater than 14")
    print ("i'm in else Block")
print ("i'm not in if and not in else Block")


output :

i is greater than 14
i'm in else Block

i'm not in if and not in else Block




 nested-if :


A nested if is an if statement that is the target of another if statement. Nested if statements means an if statement inside another if statement lets see in a simple way, if statements within if statements. i.e, we can place an if statement inside another if statement. Now we will see some program for this.

Syntax:


if (condition1):
   # executes when condition1 is true
   if (condition2):
      # executes when condition2 is true
   # if Block is end here (by white space)
# if Block is end here (by white space)


Flow chart :
python tutorial,tensorflow tutorial,flask tutorial,python 3 tutorial,python django tutorial,pyqt tutorial,best python tutorial,programiz python,selenium python tutorial



# program to illustrate nested If statement
i = 20
if (i == 20):
    #  First if statement
    if (i < 25):
        print ("i is smaller than 25")
    # Nested - if statement
    # will only be executed if statement above
    # it is true
    if (i < 22):
        print ("i is smaller than 22 too")
    else:
        print ("i is greater than 25")


output :

i is smaller than 25

i is smaller than 22 too

if-elif-else ladder


Here, a user can decide among in a multiple options. The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

Syntax:-

if (condition):
    statement
elif (condition):
    statement
.
.
else:
    statement

Flow chart :

python tutorial,tensorflow tutorial,flask tutorial,python 3 tutorial,python django tutorial,pyqt tutorial,best python tutorial,programiz python,selenium python tutorial



# program to illustrate if-elif-else ladder
   
i = 20
if (i == 10):
    print ("i is 10")
elif (i == 15):
    print ("i is 15")
elif (i == 20):
    print ("i is 20")
else:
    print ("i is not present")


output : i is 20




In this article i have cleared all the techniques of if else statement.

Next article : End parameter in print() function


Recommended post : Statement, Indentation, Comment in python

                                       Python keywords part-1

                                 Python keywords part-2




















#ceh v10
#learn ethical hacking
#learn hacking online
#ethical hacking course fees
#ceh course
 #python data analysis
 #best python tutorial
 #python classes near me
 #learn python for beginners
 #python online training
 #django course
 #advanced python course
 #python course fees
 #introduction to data science in python
 #free python tutorial
 #learn python basics
 #python study
 #python and ai
 #python computer science
 #learn python step by step
 #best course to learn python
 #learn python with projects
 #python artificial intelligence tutorial





























































Post a Comment

0 Comments