Skip to content

Latest commit

 

History

History
117 lines (102 loc) · 5.81 KB

Prachi_Python_Conditonals.md

File metadata and controls

117 lines (102 loc) · 5.81 KB

Conditionals:

What are Conditional statements?

Conditional statements have a boolean(True or False) condition . If the condition evaluates to True , all the indented statements are executed. If the condition evaluates to False , then , all the indented statements are ignored and next line is executed.

Need of Conditional statements:

We need conditional statements in our program to change the direction of program according to the conditions.

Types of conditional statements:

  • if statement
  • if-else statement
  • Chained conditional statemets

Let's take a look at all conditional statements:

1.If statements

When we have a condition or boolen expression to check , we use if condition.

Syntax:

        if boolean condition:
statement(s)

Example:
 if a>b:
        print(a)
        
  • Here, colon(':') is used to seperate the if statement with if body
  • 'a>b' is the boolean condition
  • If this boolean condition evaluates to True , then the statements inside the if block will be executed
  • 'print(a)' here , is the statement inside the if block , which will be executed only if the boolean condition evalutes to True
  • If the condition evalutes to false the statement(s) inside the if block will be ignored , and rest of the statements will be executed.

Let's take an example from our daily life:

Example: If it is rainiing, use an umbrella otherwise don't use an umbrella.

Explanation: Here , the condition is "if it is raining" , if this condition evaluates to true , then we use an umbrella ,otherwise, it is not raining so we don't need the umbrella.

Now, let us take a look at the flowchart:

Thus, this flowchart explain us the flow of the program using 'if' conditional statement.

2.if-else statements These if-else statements gives us an opportunity to have a seperate block of code to be executed if the boolean condition return false, i.e, we can write a set of code which we want to be executed when condition is true , in the if block , and other set of codes in the else block.

Syntax:

        if boolean condition:
statement(s)
else: statements(s)

Example:If we want to print the greater number among two numbers:
   if a>b:
        print(a)
   else:
        print(b)
        
Here, we first check if a>b , if True ,a is printed . Else , statements under else code is executed and , b is printed.
  • Here, colon(':') is used to seperate the if statement with if body and the else statement withe the else body.
  • 'a>b' is the boolean condition
  • If this boolean condition evaluates to True , then the statements inside the if block will be executed
  • 'print(a)' here , is the statement inside the if block , which will be executed only if the boolean condition evalutes to True
  • If the condition evalutes to false, the statement(s) under else bock will be executed.
  • Let's take an example from our daily life:

    Example: Only the students with CGPA greater than 8.5 will be allowed to enter , others , go back home.

    Explanation: Here , the condition is "the CGPA of the student is greater than 8.5" , if this condition evaluates to true , then then student is allowed in , else he is requested to go back home.

    Now, let us take a look at the flowchart:

    Thus, this flowchart explain us the flow of the program using 'if-else' conditional statement.

3.Chained conditional statements

Sometimes we need more than two branches. When we have codes for more than two possibilites we require chained conditional statements. These have an elif statement , where we can mention the second set of possibilties that we have in our mind.

Syntax:

        if boolean condition:
statement(s)
elif:
statements(s)
else: statements(s)

Example:If we want to print the greater number among two numbers:
 if a>b:
        print(a)
 elif b>a:
        print(b)
 else:
        print("equal")
        
Here, we first check if a>b , if True ,a is printed . Else , elif condition is checked, if b>a, if True , b is printed.Else statements under else code is executed and , "equal" is printed.
  • It has an if statement at first , if that evaluate to true , then it goes into if block.
  • If it evaluates to false, it goes to elif, i.e, it checks the condition of the elif block.
  • If the condition in the elif bock evaluates to true , then the elif block is executed.
  • If the condition again evaluates to false , then the code in the else block is executed.
  • Now, let us take a look at the flowchart:

    Thus, this flowchart explain us the flow of the program using 'Chained conditional'statement.