Control Flow, Methods, Parameters, and Scope
Control execution using conditionals and loops, and build reusable logic through methods, parameters, and proper scoping.
Inside this chapter
- Conditionals and Loops
- Methods
- Parameter Types
- Switch and Pattern Matching Basics
- Scope and Lifetime
- Real-World Usage Snapshot
Series navigation
Study the chapters in order for the clearest path from C# syntax and OOP to modern .NET web development, data access, async programming, architecture, and advanced engineering practice. Use the navigation at the bottom to move smoothly through the full series.
Conditionals and Loops
if (score >= 90)
{
Console.WriteLine("Excellent");
}
else if (score >= 75)
{
Console.WriteLine("Good");
}
else
{
Console.WriteLine("Needs improvement");
}
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
C# offers familiar control-flow constructs, but readability and intent remain more important than syntax alone.
Methods
static int Add(int a, int b)
{
return a + b;
}
Methods should have focused responsibilities and meaningful names. Clear method boundaries make code easier to test and reuse.
Parameter Types
C# supports value parameters, optional parameters, named arguments, ref, out, and params. Beginners should start with value parameters and then learn the advanced forms with care.
static void PrintUser(string name, int age = 18)
{
Console.WriteLine($"{name} is {age}");
} Switch and Pattern Matching Basics
string message = score switch
{
>= 90 => "Excellent",
>= 75 => "Good",
_ => "Keep working"
};
Modern C# uses expressive switch expressions and pattern matching, which make many decision structures cleaner than long chains of if-else logic.
Scope and Lifetime
Variables exist only within their scope. Understanding block scope and method scope helps students avoid confusion about reuse, side effects, and accidental shadowing.
Real-World Usage Snapshot
Control flow and method design shape every application, from web APIs and services to desktop tools and automation scripts. Good method design is one of the earliest markers of clean code.