IF, ELSE, and FOR
Chapter lab: Variables and control flow
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:
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch07-control-flow.zip && unzip -q ch07-control-flow.zip && rm ch07-control-flow.zipInvoke-WebRequest https://manykarim.github.io/robotframework-essentials-web/labs/ch07-control-flow.zip -OutFile ch07-control-flow.zip; Expand-Archive ch07-control-flow.zip -DestinationPath .; Remove-Item ch07-control-flow.zipOpen 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:
uv syncpython3 -m venv .venvsource .venv/bin/activatepip install -r requirements.txtpython -m venv .venv.venv\Scripts\Activate.ps1pip install -r requirements.txtKeep the environment active in every terminal where you run lesson commands.
poetry install --no-rootPython 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, andIN 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”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} 2Done 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} 2Task 2 — Run the suite green
Section titled “Task 2 — Run the suite green”Run it:
uv run robot checksrobot checkspoetry run robot checksDone 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.
How does a FOR loop end?
FOR, IF, WHILE, and TRY blocks all close with
END.Show the answer
Answer: with
END— FOR, IF, WHILE, and TRY blocks all close withEND.FOR ${n} IN RANGE 1 5iterates over…IN RANGE 1 5yields 1 through 4 — the end is exclusive, like Python's range.Show the answer
Answer: 1, 2, 3, 4 —
IN RANGE 1 5yields 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 Keywordfor one-liners.