Loops, Conditionals, and Flow Control with Colang 2.0

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

text
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

text
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.

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

This is functionally similar to:

text
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

Keyword: while, break, continue

Loops let your bot repeat actions until a condition is met, which is useful for tasks like collecting information or confirming details.

Example: Collecting Multiple Email Addresses

text
flow collect_emails
    $emails = []
    while True
        bot ask "Please provide an email address (or type 'done' to finish):"
        user said $input
        if $input == "done"
            break
        else
            $emails.append($input)
            bot say "Added {$input}."
    bot say "You provided these emails: {$emails}"

This flow keeps asking the user for email addresses until they type "done".

Control Flow Keywords

  • break: Exit a loop early.
  • continue: Skip to the next iteration of a loop.
  • return: Finish a flow and optionally return a value.
  • abort: Fail a flow immediately.
  • pass: Do nothing; useful as a placeholder.

Example: Validating User Input in a Loop

text
flow get_valid_age
    while True
        bot ask "Please enter your age:"
        user said $age
        if not is_integer($age)
            bot say "That doesn't look like a number."
            continue
        if $age < 0 or $age > 120
            bot say "Please enter a realistic age."
            continue
        break
    bot say "Thank you for providing your age."

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