Logic & flow

Routing

How to show different questions to different respondents. Two placement contexts — `if:` as a property on a question or page, and `- if:` as a standalone flow block that wraps a sub-tree. Both forms accept the same Python-style condition expression.

Two ifs, one expression language

There are two places you’ll write conditions in YAML:

FormWhere it goesWhat it gates
if: (property)A property on a question, page, block, or loopThat single object — show it or skip it
- if <expr>: (flow block)A standalone block in the flowThe list of questions / pages inside it

Both forms evaluate the same kind of expression — Python-style boolean logic over question accessors. The choice between them is about what’s being gated, not what you can write inside.

# As a property — one question is gated
- qid: QFollowup
    type: open
    text: Tell us why.
    if: QSat.value < 3

# As a flow block — five questions are gated together
- if QSat.value < 3:
    - qid: QFollowup
        type: open
        text: Tell us why.
    - qid: QChannel
        type: single
        text: How did you reach support?
        options:
            1: Phone
            2: Email
            3: Chat

The expression QSat.value < 3 is identical in both cases.

When to use which

NeedUse
Hide a single questionif: on the question
Hide a whole pageif: on the page
Wrap a chapter (multi-page block) under one conditionif: on the block
Fork the survey: when condition X, ask these questions; otherwise ask those- if: flow block per branch
Same condition on many siblings — DRY- if: flow block wrapping them

A useful rule: if the condition gates exactly one named object (a question, page, or block), put if: on that object as a property. If the condition gates a list of unrelated children under no shared named object, use the - if: flow block.

The expression vocabulary

What you write to the right of if: is a Python-subset expression that resolves to a truthy / falsey value at runtime. Common ingredients:

ExpressionReads
QFoo.valueSingle-value answer (single code, open text, number number).
QFoo.selectedThe selected codes on multi, rank, gridSingle.
QFoo.selected.any([1, 2])True if any of those codes are selected.
QFoo.selected.sizeCount of selected codes.
QFoo.row(1).valueA row’s value on openList / numberList.
QFoo.row(1).selectedA row’s selected scale codes on gridSingle / gridMulti.
QFoo.answeredTrue if anything is answered.
QFoo.matchedOn lookup, true if the typed value matched a record.

Combine with and / or / not, comparison operators (==, !=, <, <=, >, >=), and arithmetic. Wrap with parentheses for precedence.

if: QSat.value <= 2 and QChannel.selected.any([1, 2])
if: QAge.value >= 18 and not QGuardian.answered
if: QSpend.row(1).value > 100 or QSpend.row(2).value > 100

See Q.* accessors for the full read surface.

Inline if: — examples

Show a follow-up only when satisfaction is low

- qid: QSat
    type: gridSingle
    text: How satisfied were you?
    options: brandList
    scale: satisfactionScale

- qid: QFollowup
    type: open
    text: What's the main reason you weren't satisfied?
    if: QSat.row(1).selected.any([1, 2])

The follow-up appears only when the respondent rated brand 1 as Very dissatisfied (1) or Dissatisfied (2).

Hide a whole page from non-customers

- page:
    id: brand_loyalty
    title: Your relationship with the brand
    if: QCustomer.value == 1
    questions:
        - qid: QYears
            type: number
            text: How many years have you been a customer?
            min: 0
        - qid: QRecommend
            type: single
            text: Would you recommend us?
            options:
                1: Yes
                2: No

QCustomer.value == 1 — only customers see the loyalty page. Non-customers skip the entire screen.

Gate a whole chapter

- block:
    id: driver_chapter
    title: Driving habits
    if: QDriver.selected == 1
    - page:
        id: driver_basics
        ...
    - page:
        id: driver_vehicle
        ...

The block-level if: cascades through every page inside the block. Cleaner than putting the same if: on each child.

- if: flow block — examples

Use the flow block when you want to bundle a list of children that share one condition, especially when those children are at the survey root or inside a parent that doesn’t have its own identity.

Branch on a screening answer

- qid: QSegment
    type: single
    text: Which best describes you?
    options:
        1: Casual user
        2: Power user

- if QSegment.value == 1:
    - qid: QCasualHabits
        type: multi
        text: Which features do you use occasionally?
        ...
    - qid: QCasualLikes
        type: open
        text: What would make you use the product more often?

- elif QSegment.value == 2:
    - qid: QPowerWorkflow
        type: open
        text: Walk us through your typical workflow.
    - qid: QPowerMissing
        type: open
        text: What advanced feature is missing?

Two mutually-exclusive branches via - if: / - elif:. Casual users see two questions; power users see two different ones. Pair with - else: if you want a fall-through for any other value.

Reuse a condition across unrelated questions

- qid: QHasKids
    type: single
    text: Do you have children at home?
    options:
        1: Yes
        2: No

- if QHasKids.value == 1:
    - qid: QKidsAge
        type: number
        text: How old is your youngest child?
    - qid: QKidsActivities
        type: multi
        text: Which activities does your family do regularly?
        options: familyActivities

Without the - if:, you’d have to repeat if: QHasKids.value == 1 on each question. The flow block declares the condition once.

- elif: and - else: for chained branches

For mutually-exclusive branches, the flow grammar supports - elif: and - else: blocks chained after an - if:. Same shape as Python’s if/elif/else, but as YAML flow blocks.

- if QSegment.value == 1:
    - qid: QCasual
        type: open
        text: What features do you use most often?

- elif QSegment.value == 2:
    - qid: QPower
        type: open
        text: Walk us through your typical workflow.

- else:
    - qid: QGeneral
        type: open
        text: Tell us a bit about how you use the product.

The runtime evaluates the chain top-to-bottom and runs the first branch whose condition is true. If none match, the - else: branch (if present) runs. Each - elif: / - else: must come immediately after the preceding branch — separating them with other content breaks the chain and turns each - if: into a standalone block.

Use the chain when the branches are genuinely exclusive (“which of these segments is the respondent in?”). Use multiple standalone - if: blocks when the conditions can independently be true.

The third if — Python if inside scripts

There’s a third if you’ll meet, distinct from the two above: Python’s if statement as it appears inside a - script: or validation: block.

- script: |
    # Python statement, not a flow block.
    if QAge.value < 18:
        hSegment.set("teen")
    elif QAge.value < 65:
        hSegment.set("adult")
    else:
        hSegment.set("senior")

The script block runs Python; its if/elif/else are language control flow, not flow blocks. They use Python syntax (no leading dash, with elif and else available).

The three forms summarised:

FormLives inSyntaxHas elif / else?
if: propertyYAML — on a question / page / block / loopOne-line expression, no leading dashn/a — it’s a property
- if <expr>: flow blockYAML — at the flow levelBlock with childrenYes — - elif: / - else: blocks chained directly after
if x: ... Python statementInside a - script: or validation: blockStandard PythonYes — full if/elif/else

See Scripts for the imperative form and Validation for the per-question check form.

Common patterns

Screening — terminate on a non-qualifying answer

- qid: QAge
    type: number
    text: How old are you?
    min: 0

- if QAge.value < 18:
    - script: |
        endSurvey()

The endSurvey() runtime helper terminates the interview. Wrapping it in a - if: makes the screening rule readable as a unit.

Branching on combined conditions

- if QAge.value >= 25 and QSpend.value > 100:
    - qid: QPremiumOptions
        type: multi
        text: Which premium options interest you?
        options: premiumList

Combine accessors with and / or / not for richer routing.

Conditional follow-ups inside a loop

- lists:
    brandList:
        1: Apple
        2: Samsung
        3: Sony
        4: LG

- qid: QBrand
    type: multi
    text: Which brands have you used in the last year?
    options: brandList

- loop:
    id: brand_loop
    options: brandList
    options.from: QBrand.selected
    - qid: QRating
        type: number
        text: Rate {{ brand_loop.selectedLabel }}.
        min: 0
        max: 10

    - qid: QWhy
        type: open
        text: What's behind that rating?
        if: QRating.value < 4

Inside a loop body, if: references resolve to the current iteration’s answer automatically (no qualified-iteration syntax needed). See Loops.

See also