1 of 9

Conditional Statement�

2 of 9

What is Conditional Statement?

  • It is used to define the flow of control or execution of program.
  • Decision making statements in programming languages decides the direction of flow of program execution.
  • Decision making statements available in python are also known as

if..else statement

3 of 9

When to apply CS ?

  • Use case of Conditional Statement :�Conditional statements are used to execute different code blocks based on-�1. Conditions�2. Essential for decision-making�3. Validation �4. Controlling program flow.�� They ensure your program can adapt to varying inputs and scenarios effectively.

4 of 9

Types of Conditional Statement

5 of 9

Only If

  • An if condition executes a block of code only if a specified condition is true. If the condition is False, the block of code is skipped.� �Example:

temperature = 25� if temperature > 20:� print("It's a warm day.")� � �

6 of 9

2. If Else

  • An if-else statement allows for conditional execution of one block of code if a specified condition is true, and another block of code if the condition is false.
  • Example:

age = 18� if age >= 18:� print("You are eligible to vote.")� else:� print("You are not eligible to vote.")

7 of 9

If Elif… Else

  • An if-elif-else statement allows for multiple conditions to be checked sequentially; if the first condition is false, it checks the next condition (elif), and if none of the conditions are true, it executes the else block.
  • Example:

temperature = 15� if temperature > 30:� print("It's a hot day.")� elif temperature > 20:� print("It's a warm day.")� elif temperature > 10:� print("It's a cool day.")� else:� print("It's a cold day.")

8 of 9

Nested If Else

  • A nested if-else statement is an if-else statement inside another

if-else statement.��Example:

temperature = 25� is_sunny = True� if temperature > 20:� if is_sunny:� print("It's a warm and sunny day.")� else:� print("It's a warm but cloudy day.")� else:� if is_sunny:� print("It's a cold but sunny day.")� else:� print("It's a cold and cloudy day.")

9 of 9

Shorthand If Else

  • Shorthand if-else is a quick way to write an if-else statement in one line.��Example: message = "Warm" if temperature > 20 else "Cold"