C# Programming · Lesson 5
Getting into the Program Flow — Programming Examples
Five runnable C# examples covering nested decisions, switch forms, input loops, do-while repetition, and nested for loops.
Example 1 — Validate a Course Score with Nested Decisions
An outer if rejects scores outside 0–100 before an inner if decides whether a valid score passes. The nested blocks keep the passing decision from running on invalid data.
using System;
class Program
{
static void Main()
{
const int PassingScore = 70;
int score = 86;
bool completedFinal = true;
if (score < 0 || score > 100)
{
Console.WriteLine("Score must be between 0 and 100.");
}
else
{
if (score >= PassingScore && completedFinal)
{
Console.WriteLine("Passed the course.");
}
else
{
Console.WriteLine("Course requirements are not complete.");
}
}
}
}
Example 2 — Classify Enrollment with Two Switch Forms
A switch statement selects a message for a course track, with two labels sharing one action. A switch expression then uses relational patterns and a _ fallback to classify credit hours.
using System;
class Program
{
static void Main()
{
string track = "Web";
int creditHours = 9;
switch (track)
{
case "Web":
case "App":
Console.WriteLine("Software development track");
break;
case "Network":
Console.WriteLine("Network technology track");
break;
default:
Console.WriteLine("Unknown track");
break;
}
string enrollment = creditHours switch
{
>= 12 => "full-time",
>= 6 and < 12 => "half-time",
>= 1 and < 6 => "less than half-time",
_ => "not enrolled"
};
Console.WriteLine("Enrollment: " + enrollment);
}
}
Example 3 — Keep Asking for a Valid Exam Score
The while (true) loop retries invalid input. continue skips the remaining work on an invalid attempt, while break exits once the student enters a score from 0 through 100.
using System;
class Program
{
static void Main()
{
while (true)
{
Console.Write("Enter exam score (0-100): ");
string input = Console.ReadLine() ?? "";
if (!int.TryParse(input, out int score) || score < 0 || score > 100)
{
Console.WriteLine("Enter a whole number from 0 through 100.");
continue;
}
Console.WriteLine("Recorded score: " + score);
break;
}
}
}
Example 4 — Record Sales with a Do-While Loop
The sales prompt must appear at least once, so this program uses do…while and asks whether to record another sale after each entry. A valid amount adds to the running total; an invalid amount leaves it unchanged.
using System;
class Program
{
static void Main()
{
decimal total = 0m;
int saleCount = 0;
string again;
do
{
Console.Write("Sale amount: ");
string input = Console.ReadLine() ?? "";
if (decimal.TryParse(input, out decimal sale) && sale >= 0m)
{
total += sale;
saleCount++;
}
else
{
Console.WriteLine("Enter a nonnegative amount.");
}
Console.Write("Record another sale? (y/n): ");
again = (Console.ReadLine() ?? "n").Trim().ToLower();
} while (again == "y");
Console.WriteLine("Sales recorded: " + saleCount);
Console.WriteLine("Total: $" + total.ToString("F2"));
}
}
Example 5 — Print an Exam Seating Plan with Nested For Loops
The outer for loop moves through rows while the inner loop prints seat labels. A break closes only the current row when a blocked seat is reached; openSeats lives outside both loops so the final count remains available.
using System;
class Program
{
static void Main()
{
int openSeats = 0;
for (int row = 1; row <= 3; row++)
{
Console.Write("Row " + row + ":");
for (int seat = 1; seat <= 4; seat++)
{
if (row == 2 && seat == 3)
{
Console.Write(" [row closed]");
break;
}
string label = row + "-" + seat;
Console.Write(" " + label);
openSeats++;
}
Console.WriteLine();
}
Console.WriteLine("Open seats: " + openSeats);
}
}
Course Notes