Artificial Intelligence

Module 2.10 : Conditional Statements

Introduction

In programming, decision making is one of the most important concepts. Programs often need to make decisions based on different conditions.

For example:

  • If a student scores above 40 marks, display “Pass”.
  • If a user enters the correct password, allow login.
  • If a customer’s purchase amount is greater than 5000, apply a discount.

In Python, such decision-making logic is performed using Conditional Statements.

Conditional statements allow a program to execute different blocks of code depending on whether a condition is True or False.

Conditional statements are heavily used in Artificial Intelligence, Machine Learning, Data Science, Web Development, Automation, and software development.


Learning Objectives

  • Understand Conditional Statements in Python.
  • Learn if statements.
  • Understand if-else statements.
  • Learn elif statements.
  • Use nested conditional statements.
  • Apply logical operators with conditions.
  • Build decision-making programs.

What are Conditional Statements?

Conditional statements are used to perform decision making in programming.

They allow a program to check conditions and execute specific code blocks depending on the result.

A condition usually returns:

  • True
  • False

Example:

age = 20

if age >= 18:
    print("Eligible to Vote")

Output:

Eligible to Vote

Since the condition is True, the code block executes.


Why Conditional Statements are Important

Conditional statements are essential because they allow software applications to make intelligent decisions.

Without conditional logic, programs would execute instructions blindly without reacting to data or user input.

Conditional statements are used in:

  • User Authentication
  • AI Decision Systems
  • Recommendation Systems
  • Banking Applications
  • Online Shopping Platforms
  • Machine Learning Predictions

1. The if Statement

The if statement is the simplest conditional statement in Python.

It executes a block of code only if the condition is True.

Syntax

if condition:
    statement

Example

marks = 80

if marks > 50:
    print("Pass")

Output:

Pass

2. The if-else Statement

The if-else statement allows the program to choose between two code blocks.

Syntax

if condition:
    statement
else:
    statement

Example

age = 15

if age >= 18:
    print("Adult")
else:
    print("Minor")

Output:

Minor

If the condition becomes False, the else block executes.


3. The elif Statement

The elif statement is used when multiple conditions must be checked.

Syntax

if condition1:
    statement

elif condition2:
    statement

else:
    statement

Example

marks = 85

if marks >= 90:
    print("Grade A")

elif marks >= 70:
    print("Grade B")

else:
    print("Grade C")

Output:

Grade B

4. Nested Conditional Statements

A nested conditional statement means placing one condition inside another condition.

Example

age = 25
citizen = True

if age >= 18:

    if citizen:
        print("Eligible to Vote")

    else:
        print("Not a Citizen")

else:
    print("Under Age")

Output:

Eligible to Vote

Using Logical Operators with Conditions

Logical operators are frequently used with conditional statements.

Python provides:

  • and
  • or
  • not

AND Operator Example

age = 25

if age > 18 and age < 60:
    print("Working Age")

Output:

Working Age

OR Operator Example

day = "Sunday"

if day == "Saturday" or day == "Sunday":
    print("Weekend")

Output:

Weekend

NOT Operator Example

logged_in = False

if not logged_in:
    print("Login Required")

Output:

Login Required

Conditional Statements with User Input

Conditional statements often work with user input.

Example

password = input("Enter Password: ")

if password == "python123":
    print("Login Successful")

else:
    print("Invalid Password")

Conditional Statements in Artificial Intelligence

Conditional logic is widely used in Artificial Intelligence systems.

Examples:

  • Spam Detection
  • Fraud Detection
  • Medical Diagnosis Systems
  • Recommendation Systems
  • Chatbot Responses

AI models often use conditions to evaluate predictions, confidence scores, and decision boundaries.


Real-World Examples

Discount System Example

amount = 6000

if amount >= 5000:
    print("10% Discount Applied")

else:
    print("No Discount")

Output:

10% Discount Applied

Weather Example

weather = "Rain"

if weather == "Rain":
    print("Take Umbrella")

else:
    print("Enjoy the Day")

Python Example

number = 10

if number % 2 == 0:
    print("Even Number")

else:
    print("Odd Number")

Output:

Even Number

Interview Questions

1. What are conditional statements?

Conditional statements allow programs to make decisions based on conditions.

2. Which keyword handles multiple conditions?

The elif keyword handles multiple conditions.

3. What is nested if?

Nested if means placing one conditional statement inside another.

4. Which logical operators are used with conditions?

and, or, and not.


Assignment

  1. Create a program to check whether a person is eligible to vote.
  2. Create a grade calculator using if-elif-else.
  3. Build an odd-even checker program.
  4. Create a login validation program.
  5. Use nested if conditions in a small example.

Quiz

Q1. Which statement executes code when a condition is True?

  • A. while
  • B. if
  • C. break
  • D. import

Answer: B. if

Q2. Which keyword checks multiple conditions?

  • A. elif
  • B. continue
  • C. def
  • D. return

Answer: A. elif

Q3. Which logical operator returns True when both conditions are True?

  • A. or
  • B. not
  • C. and
  • D. in

Answer: C. and


Summary

In this tutorial, you learned Conditional Statements in Python. You explored if, if-else, elif, nested conditions, and logical operators.

Conditional statements allow programs to make intelligent decisions and are widely used in Artificial Intelligence, software development, and automation.

Understanding conditional logic is essential for building real-world applications and decision-making systems.

Next Tutorial

Tutorial 16: Loops in Python

```

Leave a Reply

Your email address will not be published. Required fields are marked *