$ dotnet run32lastGo!1int n = 3;2while (n > 0)3{4 if (n == 1)5 Console.WriteLine("last");6 else7 Console.WriteLine(n);8 n = n - 1;9}10Console.WriteLine("Go!");int n??
Violet card: the program; the violet arrow marks the line running nowBlue cell: the int variable n; rose block: the condition, a bool: true or falseTokens: n's value going to a test, a new value going into n, output going to the terminalOn the left: the C# program Program.cs, with an arrow beside the line that runs now. On the right: the variable n, the condition block and the terminal. The while loop tests n > 0 before every pass; inside it, if/else picks one of two lines to print; line 8 makes n smaller. The arrow jumps back from the loop's } to the test until the test is false. The output is 3, 2, last and Go!. The same countdown can be written as a for loop, which keeps the start, the test and the step on one line: for (int n = 3; n > 0; n--) { ... }. n-- is short for n = n - 1. C# also has do-while, switch, break and continue.

One line after another

  1. Control flow is the order in which lines run. With top-level statements, Program.cs runs one statement at a time, from the top down. Line 1 runs first: int n = 3; makes an int variable n that holds 3.

Choosing a path

Repeating

What happens

;; step 1/9 · sequence
Program.cs runs its statements in order, top to bottom
the arrow beside the card marks the line running now
line 1   int n = 3;   →  n holds 3
control flow = the order the lines run in
if and while change that order (next steps)

In C# a condition must be a bool. There is no conversion from int to bool, so while (n) or if (n) does not compile (error CS0029). Writing if (n = 1) by mistake is also error CS0029, because n = 1 is an int, not a bool. With top-level statements there is no Main method to write. When the last statement finishes, the program ends with exit code 0 (success).