subjects

Dropdown Menu

Friday, 2 April 2021

Conditional Statements(If Statements)

 If statement: If statement evaluates the test condition and will execute statement-block only if the condition is True. If the condition is False, the statement-block is not executed and control is transferred to next statements. The group of statements which has to be executed when the condition is true are written in statements-block. Statement-block  may be a single statement or group of statements  with proper indent.


   
   Flow chart : 



Ex: Python program to check whether the given number is positive or not

num = float(input("Enter a number: "))                

if num > 0:

print("Positive number")

print("Simple if")

Output:

Enter a number 5

Positive number

Simple if


 If –else:

The if-else is an extension of the simple if statement. The if –else statement evaluates the condition, whether the condition is true then the If block will be executed otherwise the else block   will be executed, not both. The general form is


Syntax

                                   


                                    if  test condition:

                                                If block

                                    else:

                                                else block

                                    statement-x


num = float(input("Enter a number: "))

if num > 0:

    print("Positive Number ")

else:

    print("Negative Number ")


Output:

    Enter a number 5

    Positive Number