Conditional Statements/ Decision Making in Python

 

Conditional Statements in Python

What are Decision Making statements in Python?

What are branching statements in Python?

Conditional statements or decision making statements are statements used to evaluate a particular expression based on test condition result. The test condition result is true or false. If test condition becomes true then particular block is executed and if test condition becomes false then particular block get execute.

For example

If you want to evaluate student is pass or fail in examination based on percentage of student. Then you can write test condition on percentage as follows,

percentage >=40  “Student is Pass”   else “student is fail”. This kind of decision making problems are solved with the help of conditional statements.

Types of Conditional statements in Python

1.     if  statement:

The if statement is used to execute block of code if test condition becomes true.

General syntax of if statement:

if test_condition:

          statement

 In above syntax if keyword is used to to write if statement. Test condition is the test expression which is used to test the particular condition. Test condition follows by the colon. After colon indentation is followed to write code block for if statement.

a=110

if a==110:

    print("A is 110")

Output:

A is 110

 

Here value of a is initialized as 110. Test condition is applied on a to compare a with 110. i.e. if value of a is exactly equals to 110 then if block will get execute. If test condition becomes false then nothing will get execute. In above example test condition becomes true so if block executed and result displayed on screen.

 

2.     If-else Statement:

If else statement is used to evaluate a particular code block based on test condition result i.e. true or false. If test condition result is true then if code block executes and if test condition becomes false then else code block executes. In if else statement if and else keywords are used. For both if and else statement the indentation is followed to represent the code block.

General syntax of if else statement,

if test_expression:

     code block to be executed if test condition becomes true

else:

     code block to be executed if test condition becomes false

For example,

a=111

if a==110:

    print("A is 110")

else:

    print("A is not 110")

Output:

A is not 110

In above example value of a is 111. A is compare with 110 by using equality operator. Here test condition becomes false because value of a is 111 and it is compared with 110. It is not exactly equals to 110. So if code block is not executed. Else block executed and output is A is not 110.

3.     if-elif-else statement:

if-elif=else statement is used to test multiple conditions and based on test condition result if true the particular code block is executed.

General syntax

if testcondition1:
          code block is executed if testcondition1 is true
elif testcondition2:
          code block is executed if testcondition2 is true
elif testcondition3:
          code block is executed if testcondition3 is true
else:
          code block is executed if testcondition3 is false

Here testcondition1 is checked. If testcondition1 result is true, the code block is executed. If test condition1 becomes becomes false the control goes to test  the test condition2. If testcondition2 result is true then this code block is executed otherwise compiler goes to test the testcondition3. Here again if testcondition3 becomes false then the default statement i.e. else block get executed.

For example,

a=10

b=100

if (a>b):

    print("A is bigger")

elif a<b:

    print("A is smaller")

else:

    print("Both numbers are equals")

Output:

A is smaller

In above example valueof a and b are 10 and 100 respectively. A is compared with b to check a is greater then b or not. If test condition becomes true then if code block is executed. If test condition becomes false then compiler goes to next test condition which is written with elif to test a is smaller than b. Here test condition becomes true so code block written in elif block is executed. The if-elif-else statement is very useful in case of multiple conditions.  The elif is nothing but else-if and we can use multiple elif statements to test multiple test conditions.

4.     Nested if statement:

The nested if statement is a if statement in Python which is written in another if statement. Nested if statement is used to test more than one test and based on test condition execute the statement.

General syntax:

if  testcondition1:

          statement to be executed if condition1 is true

if testcondition2:

          statement to be executed if condition2 is true

else:

          statement to be executed if testcondition2 is false

else:

          statement to be executed if testcondition1 is false

In the syntax statement written in testcondition1 is executed if test condition1 is true. The compiler will check one another test condition written inside the other test condition i.e. testcondion1. If test condition2 is true compiler will evaluate statement written in test condition2. If test condition2 becomes false then code in side outer if get executed. If test condition1 becomes false then outer else code block get execute.

For example,

 a=7

if a>0:

    print("A positive number")

    if a%2==0:

        print("a is even number")

    else:

        print("a is odd number")

else:

    print("A is negative number")

Output:

A positive number

a is odd number

In this example first a is compare with 0. If a is greater than 0 this condition is true then compiler will execute outer if code block and then control goes to check inside if block. Here a is divided by to and remainder is calculated. If remainder is exactly equals to 0 then inside if block get execute, if this condition becomes false then inside else block get execute. But if initially test condition1 i.e. outer if condition becomes false then compiler will execute outer else code block.

In this example outer if test condition is true so a is positive is displayed and then second condition is checked, here test condition becomes false so inner else code block i.e. number is odd is displayed.

 

 

 

 

 

 

 

टिप्पणी पोस्ट करा

0 टिप्पण्या