if, if-else and Nested if-else control statements with suitable example

 

Control Statement:

Control Statement is also known as control flow. Control Statement is the order that instructions are executed in a program. A control statement is a statement that determines the control flow of a set of instructions. There are three fundamental forms of control that programming languages provide, sequential control, selection control and iterative control.

            Sequential control is an implicit form of control in which statements/instruction are executed in the order that they are written. Selection control is provided by a control statement that selectively executes statement/instructions, while iterative control is provided by an iterative control statement that repeatedly instructions. Each is based on a given condition. Collectively set of instructions and the control statements controlling their execution is called as control structure or statement.








if StatementThis is simplest control statement to solve the decision problem. If is a keyword. It is used to implement the decision control instruction. If statement is closed with colon. If condition whatever, it is true then this statement is executed. If the condition is found false, then statement not executed, otherwise skip creation parts of the program.

The Python if statement is same as it is with other programming languages. It executes a set of statements conditionally, based on the value of a logical expression.

Syntax:

            if expression:

                        statement 1

                        statement 2

 Flow chart:


For Example: Write a program to demonstrate use of if statement

no = int(input("Enter Number less than 20: ")) # Here no is variable

if no<20:    #Here is if statement

   print("You entered right number ")

 Output: 





if-else Statement: If-else is also known as bi-direction control statement. The if statement can execute only true part of statement. When you need to check true and false part of program, that time you used if-else statement. If the condition is true then a single statement or a block of statement executed (true part), otherwise (false part) another part is executed.  We sure get result from your program, it may be true or false.

Syntax:

                        if condition :

                               Statement1               #True part

            else:

                  Statement 2             # False Part

           

Flowchart:




For Example: Write a program to input any number and find out given number is even or odd.

num = int(input("Enter any Number: "))

if (num % 2) == 0:

   print("Given number is Even ")

else:

   print("Given Number is  Odd ")

Output:


Nested if-else Statement: A nested if-else statement contains one or more if-else statement. In other words, if-else statement within another if-else statement is called nested if-else statement. There are many times when selection among more than two sets of statements is needed. In this case ‘nested if-else’ statement is used.

Syntax:

            if condition:

    if condition:

        statement 1  

    else:

        statement 2   

else:

    statement 3  

Flowchart:









 Figure: Basic flowchart of nested-if-else

For Example: Write a program to demonstrate the use of nested if-else statement.

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

if num >= 0:

    if num == 0:

        print("Given number is Zero")

    else:

        print("Given number is Positive number")

else:

    print("Given number is Negative number")

Output:






Comments

Popular posts from this blog

While loop, for loop , break and Continue Statements with example of python programming

operator of python

Precedence of operators and converstion in python programming with subtable example