Skip to content

IF, ELSE, and FOR

Chapter lab: Variables and control flowch07-control-flow.zip · 3 KB

This lesson continues in the same lab as the lesson before it. If you don’t have it yet, download it here.

Download ch07-control-flow.zip

Or fetch and unpack it from a terminal in VS Code:

Terminal window
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch07-control-flow.zip && unzip -q ch07-control-flow.zip && rm ch07-control-flow.zip

Open the folder in VS Code with code rfe-ch07-control-flow (or File → Open Folder…) and accept the recommended RobotCode and Python extensions. Then set it up:

Terminal window
uv sync
Terminal window
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

Keep the environment active in every terminal where you run lesson commands.

Terminal window
poetry install --no-root

Python 3.13 or newer is required; uv installs it when it is missing.

Open in browserNo setup? Try the lab right here: its files, an editor and a terminal, running in your browser.

What's inside
  • Directoryrfe-ch07-control-flow/
    • Directory.vscode/
      • extensions.json
    • Directorychecks/
      • Directorycatalog/
        • catalog.robot
      • Directorysmoke/
        • smoke.robot
    • .gitignore
    • .python-version
    • LAB.md
    • pyproject.toml
    • README.md
    • requirements.txt
    • uv.lock

Robot Framework has native control structures, each closed with END:

  • IF / ELSE IF / ELSE — branch on a condition. A one-line form, IF ${x} > 0 Log positive, handles simple cases.
  • FOR — loop over a range or a collection: IN, IN RANGE, IN ENUMERATE, and IN ZIP.

IN RANGE 1 5 counts 1, 2, 3, 4 — the end is exclusive, like Python.

Task 1 — Write a FOR loop with an IF branch

Section titled “Task 1 — Write a FOR loop with an IF branch”
Edit

Goal: in checks/flow.robot, count how many numbers in a range clear a threshold, using a FOR loop with an IF branch inside.

Here is the same pattern over different data — counting how many temperature readings are above freezing. Adapt the loop and condition to your numbers.

Worked example — adapt it, don’t copy it

*** Test Cases ***
Count Warm Readings
${warm}= Set Variable ${0}
FOR ${temp} IN -3 5 12 -1
IF ${temp} > 0
${warm}= Evaluate ${warm} + 1
END
END
Should Be Equal As Integers ${warm} 2

Done when

checks/flow.robot uses a FOR loop and an IF branch, and the count comes out right.

Hint 1

Loop the numbers 1–4 with FOR ${n} IN RANGE 1 5 (the end is exclusive), starting a counter at ${0} before the loop.

Hint 2

Inside the loop, IF ${n} > 2 increment the counter with Evaluate ${count} + 1, and close both blocks with END.

Hint 3

Two numbers (3 and 4) clear the threshold, so assert the count is 2.

Solution
*** Test Cases ***
For And If
${count}= Set Variable ${0}
FOR ${n} IN RANGE 1 5
IF ${n} > 2
${count}= Evaluate ${count} + 1
END
END
Should Be Equal As Integers ${count} 2
Run

Run it:

Terminal window
uv run robot checks
Terminal window
robot checks
Terminal window
poetry run robot checks

Done when

the test For And If passes.

Check your understanding

Quick recall. Your first answer stays in this browser; the quiz never blocks your progress.

  1. How does a FOR loop end?

    Show the answer

    Answer: with END — FOR, IF, WHILE, and TRY blocks all close with END.

  2. FOR ${n} IN RANGE 1 5 iterates over…

    Show the answer

    Answer: 1, 2, 3, 4 — IN RANGE 1 5 yields 1 through 4 — the end is exclusive, like Python's range.

Key terms

FOR ... IN RANGEconcept
Loop over a range or list; close with END. Variants: IN, IN RANGE, IN ENUMERATE, IN ZIP.
IF / ELSE IF / ELSEconcept
Native branching, closed with END; inline IF cond Keyword for one-liners.

All terms in the glossary →