Courses / Physics I
Computer Science

Algorithms, variables, and control flow

Physics I 219 words Free to read

An algorithm is a finite sequence of unambiguous steps that solves a problem. In physics, algorithms automate calculations from numerical integration to Monte Carlo simulations.

Variables and assignment

mass = 5.0        # kilograms
velocity = 3.0    # m/s
kinetic_energy = 0.5 * mass * velocity**2

A variable stores a value in RAM (main memory) and can be updated at any time.

Control flow structures

Conditional — Execute code only when a condition is true:

if temperature > 100:
    print("Boiling!")
elif temperature > 0:
    print("Liquid")
else:
    print("Frozen")

Loop — Repeat a block of code:

# Euler method: simple numerical integration
x, v, dt = 0.0, 10.0, 0.01
for step in range(1000):
    x += v * dt
    v += -9.8 * dt   # gravity

Key hardware context

Tip: Always initialise variables before using them. An uninitialised variable is the most common source of bugs in scientific code.
Common pitfall: The computer does exactly what the code says, never what you meant. Off-by-one boundaries in loops and conditions are where the gap between intention and instruction bites hardest.

Practise this lesson

The explanation above is free to read. The graded practice for this lesson lives in the Tryals app.

11practice questions
2interactive scenes
Start Physics I free

Computer Science