A program is more than a collection of instructions. It is a sequence of instructions that execute in a particular order.
Earlier, you worked mostly with straight-line flow:
That model is still important. Later instructions can depend on objects, values, or state created by earlier instructions.
Module 1.4 adds an important idea:
A program can evaluate information and decide whether a particular action should happen.
People often fill in missing details automatically. Programs cannot safely depend on unstated assumptions.
Consider this requirement:
This instruction contains two parts:
The action should not happen every time. It should happen only when the condition is satisfied.
This is the foundation of conditional program flow.
Without a decision, the flow might be:
With a condition, the flow changes:
The program still follows defined instructions. The difference is that one instruction determines which path is followed next.
A program needs a condition it can evaluate precisely.
A Boolean value gives the program two possible results:
true
falseSuppose a Player object contains:
player1.isAvailableIf the current value is:
truethen the statement “the player is available” is true.
If the current value is:
falsethen that statement is false.
The Boolean value does not perform the action by itself. It provides the result that a decision can use.
When reading conditional logic, identify these two ideas separately.
Question:
Is the player available?Action when true:
Add the player to the match listThis separation matters because the program must first determine whether the condition is true before it knows whether the controlled action should execute.
A useful reasoning pattern is:
In C#, a simple if statement expresses the same idea:
if (player1.isAvailable)
{
System.Console.WriteLine("Add player to match list.");
}Read it conceptually before worrying about the punctuation:
The condition is evaluated first.
If the condition is true, the controlled block runs.
If the condition is false, the controlled block is skipped.
A common mistake is to think that a false condition means execution ends.
Consider:
if (player1.isAvailable)
{
System.Console.WriteLine("Player is available.");
}
System.Console.WriteLine("Roster loaded.");If player1.isAvailable is false, the first output is skipped.
The program then continues with:
System.Console.WriteLine("Roster loaded.");The condition controls only the block associated with the if.
Program flow continues afterward.
Program state and program flow are closely connected.
Suppose:
player1.isAvailable = trueThat state can cause one path to execute.
An instruction on that path might then change another value in the program.
This creates a cycle of reasoning:
When you trace a program, ask both:
The same decision can be represented at different levels.
A requirement might say:
A conceptual flow can show:
An activity diagram can model that behavior visually.
C# can then implement it with an if statement.
These are different representations of the same reasoning:
The syntax matters, but the decision should make sense before you translate it into code.
Before executing an if statement, identify the condition and predict its Boolean result.
For example, if:
player1.isAvailable = falsethen for:
if (player1.isAvailable)predict:
This habit connects program flow to debugging. Instead of only observing what the program did, you first state what you expect it to do and then compare the actual path with your prediction.
Program flow answers:
For a straight sequence, the answer is usually the next instruction in order.
For a decision, the answer depends on a condition.
A simple if statement gives a program one controlled action path:
The activity diagrams that follow give you a visual way to design and trace this kind of flow before translating it into C#.