Introduction

Flow control is a core concept in programming and is just as important in conversational AI. In Colang 2.0, flow control enables your bot to make decisions and repeat actions, resulting in more natural and dynamic conversations. In this lesson, you'll learn how to use conditionals, event branching, and loops to create advanced conversational flows.

Conditional Branching

Keyword: if, elif, else

Conditional branching lets your bot choose different actions based on the current state or user input. The syntax is similar to Python, making it easy to use for those familiar with traditional programming.

Example: Handling Subscription Status

flow check_subscription_status
    $is_subscribed = get_user_subscription_status
    if $is_subscribed == True
        bot say "Welcome back, premium user!"
    elif $is_subscribed == False
        bot say "You are currently on a free plan."
    else
        bot say "I couldn't determine your subscription status."

Here, the bot checks the user's subscription status and responds accordingly.

Event Branching

Keyword: when, or when, else

Event branching allows your bot to react to multiple possible events, starting them concurrently and responding to whichever happens first.

Example: Handling Multiple Events

flow handle_user_action
    when user requested help
        bot say "Sure, how can I assist you?"
    or when user asked for account info
        bot say "Here is your account information."
    else
        bot say "I'm not sure what you need. Can you clarify?"

This pattern is useful when the bot should be ready to handle several different user intents at the same time.

Using Match Instead of When

If you don't include an or when or else branch, you can use an await or match statement instead of the when construct.

flow wait_for_help_request
    when user requested help
        bot say "How can I assist you?"

This is functionally similar to:

flow wait_for_help_request
    match user requested help
    bot say "How can I assist you?"

But since there are no additional branches, match is a simpler alternative.

Loops
Control Flow Keywords
Summary

In this lesson, you learned how to use Colang 2.0's flow control features—conditionals, event branching, and loops—to create flexible and responsive conversational experiences. These tools allow your bot to make decisions and repeat actions, all of which are essential for building advanced chatbots.

Next, you'll get hands-on practice applying these concepts to real-world scenarios.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal