0
0
Agentic AIml~5 mins

Branching and conditional logic in Agentic AI

Choose your learning style9 modes available
Introduction

Branching and conditional logic help AI decide what to do next based on different situations. It makes AI smart by letting it choose actions depending on conditions.

When an AI needs to choose different responses based on user input.
When a model should take different steps depending on data values.
When controlling the flow of decisions in an AI agent.
When filtering data to apply different processing rules.
When handling errors or unexpected situations in AI tasks.
Syntax
Agentic AI
if condition:
    do_something()
elif another_condition:
    do_something_else()
else:
    do_default_action()

Use if to check a condition.

elif checks another condition if the first is false.

else runs if all conditions are false.

Examples
This checks if temperature is above 30 and prints a message accordingly.
Agentic AI
if temperature > 30:
    print('It is hot')
else:
    print('It is not hot')
This assigns a grade based on the score using multiple conditions.
Agentic AI
if score >= 90:
    grade = 'A'
elif score >= 80:
    grade = 'B'
else:
    grade = 'C'
Sample Model

This simple AI function decides what to do based on a sensor value using branching logic.

Agentic AI
def ai_decision(sensor_value):
    if sensor_value > 70:
        return 'Activate cooling system'
    elif sensor_value > 40:
        return 'Monitor temperature'
    else:
        return 'System normal'

# Test the function with different sensor values
print(ai_decision(75))
print(ai_decision(50))
print(ai_decision(30))
OutputSuccess
Important Notes

Indentation is important to show which code belongs to each condition.

Conditions are checked in order; once one is true, the rest are skipped.

Use simple comparisons like ==, >, < for conditions.

Summary

Branching lets AI choose actions based on conditions.

Use if, elif, and else to handle different cases.

Proper indentation and order of conditions matter for correct decisions.