# Algorithms, variables, and control flow

Physics I · Computer Science · https://tryals.app/learn/physics-i/algorithms-variables-and-control-flow

## 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 Type | Speed | Persistence |
| --- | --- | --- |
| **RAM** | Fast | Volatile |
| **Storage** | Slower | Permanent |

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

## 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 \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.

## Practice questions

3 of this lesson's 11 practice questions, with answers. The full set is in the app.

### 1. A `while` loop and a `for` loop can compute the same result. Why does the choice between them matter?

A. When the number of iterations is not known in advance
B. When the loop body contains more than one statement
C. Never: the two are interchangeable in every situation
D. When performance matters, since `for` loops are compiled more efficiently

**Answer:** A. When the number of iterations is not known in advance

**Why:** The two express different intentions. Convergence loops have no known trip count, and forcing them into a `for` hides the real termination condition.

Page: https://tryals.app/practice/physics-i/algorithms-variables-and-control-flow/a-while-loop-and-a-for-loop-can-compute-the-same-result-why-does-the

### 2. A loop written `for i in range(1, 10)` runs **nine** times, not ten. Why is the convention half-open rather than an off-by-one waiting to happen?

A. Because loops are indexed from zero and cannot include the last element
B. Because including both ends would make the loop run one time too many for the compiler
C. Because the length is stop minus start, and ranges join cleanly
D. Because the final value is reserved as a terminator

**Answer:** C. Because the length is stop minus start, and ranges join cleanly

**Why:** Half-open intervals make length arithmetic trivial and make `range(0,10)` and `range(10,20)` tile the numbers exactly once. Both closed ends would force a $+1$ everywhere and a decision about the boundary at every join. The convention costs one moment of surprise and removes a whole class of slicing bugs, which is why it is near-universal.

Page: https://tryals.app/practice/physics-i/algorithms-variables-and-control-flow/a-loop-written-for-i-in-range-1-10-runs-nine-times-not-ten-why-is

### 3. Which of these are valid Python variable names?

A. data1
B. class
C. _count
D. my_var
E. 2fast

**Answer:** A. data1; C. _count; D. my_var

**Why:** Names cannot start with a digit, and 'class' is a reserved keyword.

Page: https://tryals.app/practice/physics-i/algorithms-variables-and-control-flow/which-of-these-are-valid-python-variable-names
