Survey structure

Loops

`- loop:` repeats a sub-survey for each item in a list — once per brand, once per channel, once per panel member. The loop variable carries the current iteration's code and label, and every question inside picks up an iteration suffix on export.

What - loop: is

A loop repeats a chunk of the survey for each item in a list. The classic case: ask the same battery of questions about each brand the respondent has used.

- 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               # full authored domain — required
    options.from: QBrand.selected    # narrow to what the respondent picked
    - qid: QRating
        type: number
        text: Rate {{ brand_loop.selectedLabel }} out of 10.
        min: 0
        max: 10

    - qid: QWhy
        type: open
        text: Why did you give that rating?
        if: QRating.value < 4

A respondent who picked Apple, Sony, and LG sees the rating + why pair three times — once per brand. The respondent who only picked Samsung sees it once. The export gets QRating_1, QRating_3, QRating_4 (one column per iteration’s code).

Properties

PropertyValuesNotes
idstringRequired. Unique across the survey. The id is also the loop variable name used inside the body — see The loop variable.
optionsinline coded list, or reusable list nameThe loop’s authored domain — the full set of possible iteration codes. Use this or group:.
groupreusable list namePull the authored domain from a named Reusable list — alternative to options:.
options.fromPython expression returning codesFilter that narrows the authored domain to a subset (e.g. QBrand.selected). Optional. Must be paired with options: or group: — see below.
orderkeyword or expressionControls iteration order. Keywords: fixed (default), random, randomEach, alphabetical, reverse, flip. Also accepts an expression that resolves to an ordered code list — most usefully QSource.all to iterate in the order the respondent saw the source question. See Iteration order below and the Order reference.

The full authored domain is required. Either options: or group: must be declared on every loop — even when options.from: narrows it. The validator rejects a loop with options.from: alone (options-from-domain-required error): “options.from is a mask/filter only. Declare options or group on the same loop so the full response domain remains explicit.”

Why: the authored domain is what the static cross-iteration accessors (QRating_1, QRating_2, …) bind to — every code in options: / group: produces a stable name, regardless of whether a particular respondent actually iterated through it. See Reading a specific iteration’s answer.

What can go inside a loop

ChildMeaning
- qid: ...A question. Every question inside a loop picks up an iteration suffix on export (QRating_2, QRating_3, …).
- page: ...A nested page. Each iteration gets its own screen.
- block: ...A named sub-group within the iteration body.
- loop: ...A nested loop — see Nested loops.
- if <expr>:Conditional sub-tree, evaluated per iteration.
- script: |Imperative Python, evaluated per iteration.
- note: |Author-only comment.

The loop variable

Every loop has a loop variable — a runtime proxy named after the loop’s id. Inside the body, this variable lets you read the current iteration. Most-used accessor is selectedLabel, for piping the iteration’s name into question text.

ExpressionKindReturnsMeaning
loop_id.valuepropertyCodeThe current iteration’s code (e.g. 2). Not a respondent answer — the iteration code itself.
loop_id.selectedLabelpropertyLabelThe current iteration’s label (e.g. "Samsung"). The most common one to pipe.
loop_id.allpropertyCodeSetAll iteration codes this loop will see for this respondent (after masking via options.from).
loop_id.allLabelspropertyLabelListLabels for all.
loop_id.sizepropertyintTotal iterations this respondent will see.
loop_id.at(n)methodCodeThe iteration code at 0-based position n — same contract as collection .at(index). loop_id.at(0) is the first code, loop_id.at(1) the second. Returns null for out-of-range or non-numeric n.
text: Rate {{ brand_loop.selectedLabel }}.
text: One of {{ brand_loop.size }} brands — {{ brand_loop.selectedLabel }}.

There’s no built-in “current position” accessor (no current iteration is N of M value). For pure piping, selectedLabel and size cover most cases; for cross-iteration logic, see Reading a specific iteration’s answer below.

Inside a loop — references resolve to the current iteration

Question references inside the loop body automatically scope to the current iteration. So a follow-up reading the previous answer just works:

- 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 for {{ brand_loop.selectedLabel }}?
        if: QRating.value < 4

Inside iteration for Apple, QRating.value is Apple’s rating. Inside the iteration for Sony, QRating.value is Sony’s rating. The runtime substitutes the iteration code automatically.

Reading a specific iteration’s answer

Outside the loop body — or inside, when you explicitly want a different iteration’s answer — reach the per-iteration question through the trailing-underscore form of its qid:

# Static — when the iteration code is known at authoring time
QRating_1.value      # Apple's rating (assuming Apple is code 1)

# Dynamic — when the iteration code is in a variable
QRating_[code].value

QRating_ (with the trailing underscore) is a runtime proxy that exposes every iteration of the looped question by code. The bare QRating.value form keeps reading the current iteration when used inside the loop body — the underscore form is the explicit per-iteration handle.

For nested loops, codes concatenate outer-to-inner — matching the export column naming:

# Static — market 1, brand 10
QRating_1_10.value

# Dynamic — same shape with brackets
QRating_[1][10].value

Static accessor names bind to the full authored domain (every code in options: / group:), not just the iterations this respondent ran. So QRating_5.value exists whether or not the respondent’s options.from: filter included code 5 — it just returns an unanswered slot when the iteration didn’t run.

When you want to iterate over only the codes that actually rendered for this respondent, use loop_id.all:

for c in brand_loop.all:           # only the codes that ran
    total = total + (QRating_[c].value or 0)

The most common use is aggregating after the loop completes:

- script: |
    total = 0
    for code in brand_loop.all:
        total = total + (QRating_[code].value or 0)
    hAvg.set(total / brand_loop.size)

Use this in - script: blocks and any if: outside the loop body.

When the qid name itself is a string you’ve computed at runtime (rather than a literal QRating_ in the script), reach for the q() helper:

q("QRating_", code).value     # equivalent to QRating_[code].value

Note that q("QRating", code) (without the trailing underscore) resolves to QRating1, QRating2, etc. — not the loop-iteration accessor. Supply the underscore yourself.

Examples

Loop with inline options

- loop:
    id: market_loop
    options:
        1: United States
        2: United Kingdom
        3: Germany
        4: Japan
    - qid: QSpend
        type: number
        text: Marketing spend in {{ market_loop.selectedLabel }} (USD millions).
        min: 0

Four iterations, fixed up front. Useful when the loop list is authored, not derived from a respondent answer.

Loop with a group (reusable list)

- lists:
    competitor_brands:
        1: Apple
        2: Samsung
        3: Sony

- loop:
    id: comp_loop
    group: competitor_brands
    - qid: QFamiliar
        type: gridSingle
        text: How familiar are you with {{ comp_loop.selectedLabel }}?
        scale:
            1: Not at all
            5: Very familiar

Iterations come from a named list defined once at the survey level — reusable across multiple loops without duplicating the brand list.

Loop with random iteration order

- 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
    order: random
    - qid: QImpression
        type: open
        text: Tell us your first impression of {{ brand_loop.selectedLabel }}.

Each respondent sees the brands in a random order — eliminates ordering bias on first impressions.

Loop that iterates in the same order the source question was shown

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

- loop:
    id: brand_loop
    options: brandList
    options.from: QBrand.selected
    order: QBrand.all
    - qid: QImpression
        type: open
        text: Tell us about {{ brand_loop.selectedLabel }}.

QBrand is shuffled per respondent. order: QBrand.all tells the loop to iterate the selected brands in the order they were shown on QBrand — not authored order, not click order. If QBrand was rendered Sony → Apple → LG → Samsung and the respondent picked Apple and LG, the loop runs Apple → LG. Keeps the rotation consistent across the whole battery.

See the Order reference for the full set of expressions order: accepts.

Iteration order

The loop’s order: controls the sequence it runs through its members. Three common patterns:

  • order: fixed (default) — iterate in authored / returned order.
  • order: random — shuffle per respondent, stable across their session.
  • order: QSource.all — iterate in the order the respondent saw QSource. Use this with options.from: QSource.selected for carry-forward loops where the display order matters.

order: only re-sequences — the loop’s membership still comes from options: / options.from:. See the Order reference for every form the expression accepts, including stored sequences (hOrder.rows) and script functions.

Conditional inside a loop

- lists:
    productList:
        1: Laptop
        2: Tablet
        3: Smartphone
        4: Smartwatch

- qid: QProducts
    type: multi
    text: Which of these do you currently own?
    options: productList

- loop:
    id: prod_loop
    options: productList
    options.from: QProducts.selected
    - qid: QOwnLength
        type: number
        text: How many years have you owned the {{ prod_loop.selectedLabel }}?
        min: 0

    - qid: QSatisfaction
        type: gridSingle
        text: How satisfied are you with the {{ prod_loop.selectedLabel }}?
        scale:
            1: Very dissatisfied
            5: Very satisfied

    - qid: QReplace
        type: single
        text: Would you replace your {{ prod_loop.selectedLabel }} this year?
        if: QSatisfaction.value <= 2
        options:
            1: Yes
            2: No

The replace question only shows for iterations where the respondent gave a low satisfaction rating — same if: logic applies independently to each iteration.

Nested loops

The classic case for nesting: the inner list genuinely varies per outer iteration. A shopper study asks which retailers you visited, then — per retailer — which departments you actually bought from, then rates each of those. Someone who bought Groceries + Household at Walmart but only Groceries at Costco should evaluate 3 (retailer, department) combinations, not 4.

- lists:
    retailerList:
        1: Walmart
        2: Target
        3: Costco
    deptList:
        1: Groceries
        2: Household
        3: Electronics
        4: Apparel

- qid: QRetailers
    type: multi
    text: Which of these retailers have you shopped at in the last 30 days?
    options: retailerList
    min: 1

- loop:
    id: retailer_loop
    options: retailerList
    options.from: QRetailers.selected

    # Per retailer, capture which departments the respondent actually
    # bought from. This answer is scoped to THIS retailer iteration,
    # so `QDepts.selected` reads the current retailer's answer below.
    - qid: QDepts
        type: multi
        text: At {{ retailer_loop.selectedLabel }}, which departments did you buy from?
        options: deptList
        min: 1

    # Inner loop iterates only the departments this respondent bought
    # from AT THIS RETAILER — a different subset for each outer iteration.
    - loop:
        id: dept_loop
        options: deptList
        options.from: QDepts.selected

        - qid: QExperience
            type: gridSingle
            text: How was the {{ dept_loop.selectedLabel }} department at {{ retailer_loop.selectedLabel }}?
            scale:
                1: Poor
                2: Fair
                3: Good
                4: Very good
                5: Excellent

What a respondent sees. Suppose someone picks Walmart + Costco, then at Walmart says Groceries + Household + Electronics (3 depts), and at Costco just Groceries (1 dept). They answer QExperience 4 times — once per (retailer, department) combination they actually shopped, never for a combination they didn’t. No “were you at Costco’s Electronics department” rating for someone who wasn’t.

Export columns. Iteration codes concatenate outer-to-inner, matching the loop nesting: QExperience_1_1r1, QExperience_1_2r1, QExperience_1_3r1, QExperience_3_1r1. The _1_1 = retailer 1, department 1; the r1 is the gridSingle row (see Column naming and Loop suffixes).

Piping both loop variables into text works exactly as shown — {{ retailer_loop.selectedLabel }} picks up the current outer iteration, {{ dept_loop.selectedLabel }} the current inner. Inside the inner loop, bare QDepts.selected still means “this respondent’s answer for the current outer iteration” — question references resolve to the innermost loop that contains that qid’s declaration.

Cross-iteration reads from outside. After both loops complete, use the trailing-underscore proxy with concatenated codes:

# Their experience of Costco (retailer 3) Groceries (dept 1)
QExperience_3_1.selected

# Dynamic form when both codes are variables
QExperience_[rCode][dCode].selected

See Reading a specific iteration’s answer for the full accessor surface.

Empty loops

When options.from resolves to an empty collection (e.g. the respondent selected nothing), the loop body is skipped entirely — no iterations, no rendered questions, no exported columns for that respondent.

Defensive guarding. If there's a chance the source collection is empty, either gate the loop's parent with an if: that handles the empty case, or add a fallback question outside the loop. A silently-empty loop produces no data but no error — easy to miss in QA.

Aggregating across iterations

After the loop completes, scripts can sum, average, or filter across iterations using qualified qid access:

- 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

- qid: hAvgRating
    type: number
    visibility: hide
    text: Average rating across all rated brands

- qid: hBestBrand
    type: single
    visibility: hide
    text: Code of the highest-rated brand
    options: brandList

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

# After the loop completes
- script: |
    # Average rating across all rated brands
    total = 0
    count = 0
    for c in brand_loop.all:
        if QRating_[c].answered:
            total = total + QRating_[c].value
            count = count + 1
    if count > 0:
        hAvgRating.set(total / count)

    # Brand with the highest rating
    best_code = None
    best_value = -1
    for c in brand_loop.all:
        v = QRating_[c].value or 0
        if v > best_value:
            best_value = v
            best_code = c
    hBestBrand.set(best_code)

See Python essentials for the supported language features and Patterns for more aggregation idioms.

Export naming

Every question inside a loop picks up an iteration suffix on export. With brand_loop iterating over codes 1, 3, 4:

QRating_1
QRating_3
QRating_4

For nested loops the suffixes concatenate outer-to-inner: QRating_<market_loop_code>_<brand_loop_code>.

The full pattern is in Loop suffixes.

See also

  • Pages — the screen unit
  • Blocks — sub-grouping that can live inside or wrap loops
  • Routing — if: per iteration, - if: flow blocks
  • Piping — {{ loop_id.selectedLabel }} in question text
  • Loop suffixes — export column naming
  • Reusable lists — named list groups for group: