subjects

Dropdown Menu

Tuesday, 26 April 2022

Nested If

 To evaluate multiple conditions we use nested if statement. The general form is

Syntax

if test condition1:

        if test condition2:

                statement1

        else:

                statement2

else:

        statement3

From the above syntax if the condition1 is true control transfers to condition2 when it is true statement 1 executes, oterwise statement 2 will be executed.If the condition1 itself false the control will be transferred to else part and statement3 will be executed.





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