Control Statements

 Control Statements

 

As in our real life, where we make some decisions based on few options available in front of us. Based on the selected option we will decide what we should do next . Similarly, in programming also we need to take some decisions and based on the option arrived at, we will execute the next block of code. Decision making statements decide the direction of the flow of program execution.

 

All the statement indented by the same number of character spaces or tab character, after a programming construct are considered to be part of a single block of code. so a control statement construct can have a block of statements or even a single statement.

 

if statement

 

 it is used to decide whether a certain statement or block of statements is to be executed or not. i.e. if a condition is true then a block of statement is executed otherwise not.

 

Syntax

 

if <condition>:

#Statement to execute if the condition is true

 

Here, the condition on evaluation will be either true of false. It will be a Statement using Comparison operator or Logical operator. if the value is true then it will execute the block of statements below it, otherwise not executed. We can put the condition within bracket() also.

 

if condition:

statement 1

statement 2

 

#Here if the condition is true, if block will consider only statement1 to be inside its block and statement2 to be outside due to the indentation.

 

Example: Python if Statement

 

x=int(input("Give a Number"))

y=int(input("Give another Number"))

if(x>y):

        print("x=",x,"is greater")

if(x<y):

        print("y=",y,"is greater")

 

Output

Give a Number10

Give another Number15

y= 15 is greater

if-else Statement

 

The simple if statement  tells us that if a condition is true it will execute a block of statement and if the condition is false it won't

but,if we want to do something else,when the condition is false ,then we have to use the else statement.we can use the else statement to execute a block of code when the condition is false.

 

Syntax:

 

if(condition):

#Executes this block if

#condition is true

else:

#Executes this block if

#condition is false

 

Example: Python if-else statement

 

#python program to illustrate if statement

x=int(input("Give a Number"))

y=int(input("Give another Number"))

if(x>y):

        print("x=",x,"is greater")

else:

        print("y=",y,"is greater")

 

Output

Give a Number10

Give another Number15

y= 15 is greater

 

Nested if Statement

 

A nested if is an if statement which is a part of another if block. i.e, we can place an if statement inside another if statement.

 

Syntax

if(condition1):

   #Executes when condition1 is true

    if(condition2):

        #Executes when condition2 is true

      #if Block2 is end here

#if Block1 is end here

 

There can be multiple ways of using this statement, based on our programming requirements, a complete nested if statement will look as below,

 

if(condition1):

     if(condition2):

          #statement block1

     else

          #statement block2

    else

     if(condition3):

          #statement block3

     else

          #statement block

 

Example : Python Nested if

#Python program to illustrate nested if statement

 

x=int(input("Give a Number"))

y=int(input("Give another Number"))

z=int(input("Give another one"))

if(x>y):

    if(x>z):

        print("x=",x,"is greatest")

    else:

        print("z=",z,"is greatest")

else:

    if(y>z):

        print("y=",y,"is greatest")

    else:

        print("z=",z,"is greatest")

 

Output

Give a Number5

Give another Number3

Give another one9

z= 9 is greatest

If-elif-else Statement or ladder if Statement

 

This is similar to we, deciding among from multiple options. The if statements are executed from the top to down. As soon as one of the conditions of an 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

 

Example: Python if else elif statements

 

#Python program to illustrate if-elif-else ladder

 

i=int(input("Give a Number in the range of 10 to 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 equal to any values present")

 

Output:

Give a Number in the range of 10 to 20     15

i is 15

>>> 

 

Short Hand if statement

If there is only a single statement to be executed inside the if block then shorthand if can be used. The statement can be put on the same line as the if statement.

 

Syntax:

If condition : statement

 

Example : Python if shorthand

#Python program to illustrate short hand if

i=10

if i<15:print("i is less than 15")

 

Output:

i is less than 15

 

Short Hand if-else statement

 

This can be used to write the if-else statements in a single line where there is only one statement to be executed in both if and else block.

Syntax:

<statement_when_True> if <condition> else< statement_when_False>

 

#Python program to illustrate short hand if-else

i=int(input("Give a Number"))

print(True) if i<15 else print(False)

 

(if value>=15) True

(if value<15) False

 

Output:

Give a Number65             

False

Give a Number10           

True

 

Chaining comparison operators in Python

 

Checking two or more conditions is very common in Programming Languages. Let’s say we want to check the below condition:

 

a<b<c

 

The most common syntax to do it is as follows:

 

if a<b and b<c:

          {…}

 

In Python , there is a better way to write this using Comparison operator Chaining. The chaining of operators can be written as follows:

 

if a<b<c:

    {…}

 

According to associativity and precedence in Python, all comparison operations in Python have some priority, Which is lower than that of any arithmetic , shifting or bitwise operation. Also unlike C, expressions like a<b<c have the interpretation that is conventional in mathematics. List of comparison operators in Python that can be used are

 

“>”|”<”|”==”|”>=”|”<=”|”!=”|”is”  “is not”|”in”|”not in”

 

Comparisons yield Boolean values: True or False. Comparisons can be chained arbitrarily. For example:

 

x<y<=z is equivalent to x<y and y<=z,

 

except that y is evaluated only once. But in both the cases z is not evaluated at all when x<y is found to be false.

 

Formally , if a,b,c ,x,y,z are expressions and op1 ,op2 , …, opN are comparison operators, then a op1 b op2 c .. y opN z is equivalent to a op1 b and b op2 c and .. y opN z, except that each expression is evaluated at most once. Also,

 

a op1 b op2 c

 

It doesn’t imply any kind of comparison between a and c, so

a<b>c

is perfectly legal.

 

Example

 

#python code to illustrate chaining comparison operators

>>> x=5

>>> print(1<x<10)

True

>>> print(10<x<20)

False

>>> print(x<10<x*10<100)

True

>>> print(10>x<=9)

True

>>> print(5==x>4)

True

>>> 

 

Another Example

#Python code to illustrate Chaining Comparison Operators

 

a,b,c,d,e,f=0,5,12,0,15,15

exp1=a<=b<c>d is not e is f

exp2=a is d>f is not c

print(exp1)

print(exp2)

 

Output

True

False

No comments:

Post a Comment