The English term for program control flow is: Flow of Control
When we build a program, most of our logic lives inside methods. The rest can be found within properties, operators and other function members.
Categories of Control Flow
The so-called flow of control describes the execution path of program code.
By default, programs run statements sequentially from top to bottom.
Control flow statements let you alter this default execution order.
There are many kinds of control statements, which we will cover thoroughly in later lessons. This lesson serves as a quick introduction to help you get familiar with control flow basics.
C# features three primary categories of control flow:
Sequential execution forms the foundation. The three control flow types implement logic for branching, iteration and jumping:
- Selection statements
- Iteration statements (loops)
- Jump statements
Each category includes multiple syntax variations:
Selection Statements
These execute certain blocks of code conditionally while skipping others, based on defined criteria.
- if — Runs code only when a condition evaluates to true
- if–else — Executes one of two possible code paths
- switch — Matches values against multiple cases and runs the matching branch
Iteration Statements
These repeat a block of code as long as a specified condition holds true.
- for — Checks the condition before executing each loop iteration
- while — Checks the condition before executing each loop iteration
- do while — Executes code first, then evaluates the condition afterwards
- foreach — Automatically iterates over every element inside a collection
Jump Statements
- break — Immediately exits the current loop
- continue — Ends the current iteration early, skips remaining code and proceeds straight to the next loop cycle
- goto — Jumps execution to a labelled statement
- return — Terminates the current method and returns control back to the caller
You can simply keep these concepts in mind for now; we will dive deeper later on.
class Program
{
static void Main()
{
int age = 18;
if (age >= 18) // Code inside the if block runs when the condition is satisfied
{
Console.WriteLine("You have reached adulthood");
}
int score = 85;
if (score >= 90) // Multiple conditional checks
{
Console.WriteLine("Excellent");
}
else if (score >= 80) // Test this condition if the prior one fails
{
Console.WriteLine("Good");
}
else if (score >= 70) // Test this condition if the first two fail
{
Console.WriteLine("Average");
}
else if (score >= 60) // Test this condition if the first three fail
{
Console.WriteLine("Pass");
}
else // Runs if none of the above conditions match, acts as fallback
{
Console.WriteLine("Fail");
}
// for loop: i starts at 0, loop executes while i less than 10, i increments after each cycle
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
}
int sum = 0;
while (sum < 100) // Run loop body while sum remains below 100
{
sum += 10; // Increase sum by 10 each iteration
Console.WriteLine(sum);
}
int count = 5;
do
{
Console.WriteLine("This line always runs at least once");
count++; // Increase count after each iteration
} while (count<5); // Loop body executes once even if condition is false
int[] scores = { 90, 80, 70, 60, 50 }; // Array holding five values
foreach (var item in scores) // Iterate each element in array, assign value to item and run loop body until finished
{
Console.WriteLine(item);
}
}
}
Code language: JavaScript (javascript)
Feel free to copy this sample code into Visual Studio and run it. Every statement will be explained thoroughly in upcoming lessons.
Program Control Flow