Sign in to save

Bookmark this page so you can find it later.

Sign in to save

Bookmark this page so you can find it later.

Control flow is how a program decides what to do next instead of simply running every line from top to bottom. The three most common tools are if statements, for loops, and while loops. Choosing the right one makes code easier to read, debug, and change. These tools appear in almost every program, from games and apps to data analysis and simulations.

An if statement is best when the program needs to make a decision once based on a condition. A for loop is best when the number of repetitions is known or when you are moving through a sequence such as a list, string, or range of numbers. A while loop is best when repetition should continue until a condition changes, especially when you do not know the number of repeats in advance. Good programmers choose the structure that matches the logic of the problem, not just the first one that works.

Key Facts

  • Use if when code should run only when a condition is true.
  • Use for when you know how many times to repeat or when iterating through a collection.
  • Use while when repetition depends on a condition that may change during execution.
  • A loop condition must eventually become false, or the loop may run forever.
  • for i in range(n) runs n times in Python, with i taking values 0 through n - 1.
  • Boolean conditions evaluate to True or False, such as x > 5 or name == 'Ada'.

Vocabulary

Control flow
Control flow is the order in which a program's statements are executed.
Condition
A condition is an expression that evaluates to True or False.
If statement
An if statement runs a block of code only when its condition is true.
For loop
A for loop repeats a block of code for each item in a sequence or for a fixed number of steps.
While loop
A while loop repeats a block of code as long as its condition remains true.

Common Mistakes to Avoid

  • Using if when repeated action is needed is wrong because if checks a condition only once. Use a loop when the same block must run multiple times.
  • Using while when the repeat count is known can make code harder to read. A for loop is usually clearer for fixed counts or list traversal.
  • Forgetting to update the variable in a while loop can create an infinite loop. Make sure something inside the loop moves the condition toward False.
  • Confusing = with == in conditions is wrong in many languages because = assigns a value while == compares two values. Use the comparison operator when testing equality.

Practice Questions

  1. 1 A program must print the numbers 1 through 20. Which control flow tool should it use, and how many times will the loop body run?
  2. 2 A password program keeps asking the user to enter a password until the correct password is typed. Should this use if, for, or while, and why?
  3. 3 You are writing code that gives a student a bonus message only if their score is at least 90, then continues with the rest of the program. Which control flow tool is most appropriate, and why is a loop unnecessary?