1. Control Flow and Functions in C#
Understanding Core Concepts in C#
Programming
2. Introduction
• • Objective: Understand control flow
structures and functions in C#.
• • Context: Helps developers write logical,
reusable, and efficient code.
3. What is Control Flow?
• The order in which individual statements,
instructions, or function calls are executed or
evaluated.
4. Types of Control Flow in C#
• 1. Sequential Execution: Default line-by-line
execution.
• 2. Conditional Statements: if/else, switch.
• 3. Loops: for, while, do-while, foreach.
• 4. Exception Handling: try/catch/finally.
5. Conditional Statements: if/else
• • if/else evaluates conditions to control code
execution.
• Example:
• if (x > 0)
• {
• Console.WriteLine("Positive");
• }
• else
• {
6. Conditional Statements: switch
• • switch handles multiple conditions
efficiently.
• Example:
• switch (day)
• {
• case "Monday":
• Console.WriteLine("Start of the week");
• break;
• case "Friday":
7. Functions in C#
• • Functions encapsulate reusable code.
• • Benefits:
• - Avoid duplication
• - Enhance modularity
• - Improve readability
• Example:
• int Add(int a, int b)
• {
• return a + b;
8. Summary
• • Control flow structures guide the logical
execution of code.
• • Functions improve reusability and
readability in programs.
• • Mastering these concepts is key to writing
effective C# code.