CampusLearn
← CS 101 Introduction to Programming

Control flow: if statements and loops

Notes

Control flow decides which instructions run and how often. IF STATEMENTS An if statement runs a block only when a condition is true. Add else if for extra branches and else for the fallback. LOOPS - for loops repeat a known number of times (counting, iterating a list). - while loops repeat until a condition becomes false. KEY RULES - Always make sure a while loop can end, or the program hangs. - Use break to exit a loop early and continue to skip to the next round. - Nesting loops inside loops multiplies the work: two loops over n items is n squared steps. Practice: write a loop that prints the multiplication table from 1 to 10.

Quiz

1. Which loop is the better choice when you know exactly how many repetitions you need?

2. What does the continue statement do inside a loop?

Sign in to submit and securely grade your quiz.