Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

PB
Programming Basics
0/10 completed
Beginner
How computers workWhat is a program?Thinking in algorithmsData, variables and types
Intermediate
Decisions and loopsBreaking problems into functionsErrors and debugging
Advanced
Reading documentation and learning on your ownClean code basics
Pro
Thinking like a software engineer
Tracks / Programming Basics / Intermediate

Decisions and loops

Programs become useful when they can choose (conditions) and repeat (loops). These two ideas, plus sequence, can express any algorithm.

Conditions: if / else

Python · editable
JavaScript · editable

Notice the syntax differences:

  • Python uses a colon : and indentation to group code. Indentation isn't optional there!
  • JavaScript uses ( ) around the condition and { } braces around the block.

Comparison and logic

Meaning Python JavaScript
equal == ===
not equal != !==
greater / less > < >= <= same
and and &&
or or ||
not not !
Python · editable

Loops: repeat work

for: do something for each item

Python · editable
JavaScript · editable

while: repeat until a condition changes

Python · editable

⚠️ Infinite loops: if the condition never becomes false, the loop runs forever. Make sure something inside the loop moves you toward the end. (The editors here stop runaway code after a few seconds.)

break and continue

JavaScript · editable

Combining them: the accumulator pattern

Start with an empty result and build it up in a loop. You'll use this pattern constantly:

Python · editable

Classic exercise: FizzBuzz

Print the numbers 1 to 20, but print "Fizz" for multiples of 3, "Buzz" for multiples of 5, and "FizzBuzz" for multiples of both. It's a famous interview warm-up because it combines loops, conditions and the order of checks.

Python · editable

Hint: check "multiple of both" first. Why?

✎ Check your understanding

1.In Python, how is the body of an if block defined?

2.Which loop is best when you don't know in advance how many repetitions you need?

← Data, variables and types