UcenlyOpen in the app

Programming Essentials · BE5B33PRG · Topic 1 of 9

Conditionals and loops

Choose a branch and repeat work with controlled state.

Open the interactive lesson

Key ideas

Branches

if/elif/else checks conditions in order. Only the first true branch executes. Example: if x < 0: label = "negative".

For

for i in range(3): visits 0, 1, 2. Use for to traverse a known iterable.

While

while condition: repeats while true; update the state so it can stop. break exits and continue skips to the next iteration.

Boolean logic

and, or and not combine tests; nested branches and loop conditions can be simplified by checking edge cases.

In one line: Trace variables after each iteration, not only at the end.

Sources

Written by Ucenly. Based on the BE5B33PRG course materials: