Courses / Physics I
Computer Science

Algorithms, variables, and control flow

Physics I 172 words Free to read

Algorithms and Variables

An algorithm is a finite sequence of unambiguous steps that solves a problem, automating calculations from numerical integration to simulations.

A variable stores a value in RAM and can be updated anytime. Always initialise variables before use; uninitialised variables cause the most bugs in scientific code.

Memory TypeSpeedPersistence
RAMFastVolatile
StorageSlowerPermanent

The CPU executes instructions sequentially or in parallel, while RAM holds active data and secondary storage persists data after power-off.

An algorithm is an ordered path; a variable is a box that can be empty

Control Flow and Pitfalls

Control flow structures dictate the order of execution. A conditional executes code when a condition is true, while a loop repeats a block of code.

For example, Euler numerical integration updates position and velocity over time steps: x=x+v×dtx = x + v \times \text{dt}

Common pitfall: The computer executes exact instructions, never intended logic. Off-by-one errors in loops and boundary conditions are where intent and execution diverge most.

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

Computer Science