Skip to content
Black November: every course and Teams plan at the lowest price of the year with code BLACKFRIDAY, through November 30. Shop the deal

Control Structures in Robot Framework: FOR, IF and WHILE

11-11-2026 Roy de Kleijn
Control Structures in Robot Framework: FOR, IF and WHILE

Sometimes a test needs to repeat a step or make a decision. Robot Framework has loops and conditionals for exactly that, and the useful skill is knowing when to use them and when to hide them inside a keyword instead.

FOR loops

A FOR loop repeats a block for each item in a list, closed with END:

FOR    ${item}    IN    @{BASKET}
    Add To Cart    ${item}
END

There are variants for looping over a range or over several lists at once, but the shape is always the same: a header, an indented body, and END.

IF and ELSE

IF runs a block only when a condition holds, with optional ELSE IF and ELSE:

IF    ${count} > 0
    Log    The basket has items
ELSE
    Log    The basket is empty
END

There is also a WHILE loop for repeating until a condition changes, and BREAK and CONTINUE for controlling a loop from the inside.

Use them sparingly

This matters more than the syntax. Robot Framework works best when tests read as clear, linear steps. Heavy logic in a test makes it harder to read and to trust. A good habit is to keep loops and conditionals small, and to push anything complex down into a user keyword with a readable name. The test then says what it does, and the mechanics stay out of sight.

Knowing where to draw that line is a judgement that a structured course helps you build. Our Robot Framework Certified Professional course covers control structures and good test design together, and prepares you for the RFCP exam.

Frequently asked questions

How do I write a FOR loop? Open with FOR ${item} IN @{list}, indent the body, and close with END. There are variants for ranges and for looping over several lists.

Does Robot Framework have IF and WHILE? Yes. IF with optional ELSE IF and ELSE handles conditionals, and WHILE repeats until a condition changes. Both close with END.

Should tests contain a lot of logic? Prefer not. Keep loops and conditionals light, and move anything complex into a well-named keyword so the test stays readable.


Next: web testing with the Browser library, or revisit keyword-driven testing explained.

Share:
Roy de Kleijn

Test Automation Expert

With over 15 years of experience in software testing and test automation, Roy helps teams improve their testing processes.

Get Robot Framework certified

Go from the basics to exam-ready at your own pace with the online RFCP course.