GIRLS CODING CAMP
WEEK 2
NESTED IF
STATEMENTS
LOOPS
LOOPS IN
PYTHON
Understanding
for and while
loops with
examples
What is a Loop? 🤔
A loop allows repeated execution of
code.
It is a way to repeat a set of
instructions multiple times until a
certain condition is met.
It helps automate repetitive tasks,
without writing the same code again
and again.
TWO MAIN TYPES IN
PYTHON
for loop – Iterates over
sequences
while loop – Runs until a
condition is met
THE FOR LOOP
Syntax: Example:
for i in range(3):
for variable in range(n): print("Hello, World!")
# Code to execute
output:
Hello, World!
Hello, World!
Hello, World!
USING RANGE(START, STOP, STEP)
for i in range(2, 10, 2):
range(start, print(i)
stop, step) can
output:
control the 2
range. 4
6
8
THE WHILE LOOP
A while loop syntax:
runs until a
condition while condition:
becomes # Code to execute
False.
Example
x=3 output:
while x > 0: 3
print(x) 2
x -= 1 1
Infinite Loops
If a loop condition never becomes False, it runs forever.
while True:
print("This will never stop!")
Always ensure exit conditions !
The break Statement
for i in range(10): Output:
if i == 5: 0
break 1
print(i) 2
3
4