-
Notifications
You must be signed in to change notification settings - Fork 5
Control Statements
Sann Lynn Htun edited this page Nov 19, 2024
·
1 revision
Control statements in C# determine the flow of execution in a program based on conditions or loops. They allow decision-making, branching, and iteration.
Used to execute specific code blocks based on a condition.
Statement | Description | Example |
---|---|---|
if |
Executes if the condition is true . |
if (x > 10) { Console.WriteLine("Yes"); } |
else |
Executes if the if condition is false . |
else { Console.WriteLine("No"); } |
else if |
Tests another condition if previous if is false . |
else if (x == 5) { ... } |
switch |
Selects one case to execute. | switch (choice) { case 1: ... break; } |
int x = 10;
if (x > 15) {
Console.WriteLine("Greater than 15");
} else if (x == 10) {
Console.WriteLine("Equal to 10"); // Output: Equal to 10
} else {
Console.WriteLine("Less than 10");
}
Used to execute a block of code multiple times.
Statement | Description | Example |
---|---|---|
for |
Loops a set number of times. | for (int i = 0; i < 5; i++) { ... } |
while |
Loops while the condition is true . |
while (x < 10) { ... } |
do-while |
Executes first, then checks the condition. | do { ... } while (x < 10); |
foreach |
Iterates over a collection. | foreach (var item in list) { ... } |
for (int i = 1; i <= 5; i++) {
Console.WriteLine($"Count: {i}");
}
// Output: Count: 1 to Count: 5
Alter the normal flow of execution.
Statement | Description | Example |
---|---|---|
break |
Exits a loop or switch statement. |
if (i == 5) break; |
continue |
Skips the current iteration and continues. | if (i % 2 == 0) continue; |
return |
Exits the method and optionally returns a value. | return x; |
goto |
Transfers control to a labeled statement. | goto Label; Label: ...; |
for (int i = 1; i <= 10; i++) {
if (i == 5) break; // Exits loop when i is 5
Console.WriteLine(i);
}
// Output: 1 to 4
Handle errors during runtime.
Statement | Description | Example |
---|---|---|
try |
Encapsulates code that may throw an exception. | try { int x = 10 / 0; } |
catch |
Handles exceptions thrown by try . |
catch (Exception ex) { ... } |
finally |
Executes code regardless of exceptions. | finally { Console.WriteLine("End"); } |
try {
int result = 10 / 0; // Throws DivideByZeroException
} catch (DivideByZeroException ex) {
Console.WriteLine("Cannot divide by zero.");
} finally {
Console.WriteLine("Execution completed.");
}
// Output: Cannot divide by zero. Execution completed.
Redirect execution based on conditions.
Statement | Description | Example |
---|---|---|
if-else |
Executes code based on a true/false condition. | if (x > 10) { ... } else { ... } |
switch |
Executes one of many possible cases. | switch (value) { case 1: ... } |
int day = 3;
switch (day) {
case 1: Console.WriteLine("Monday"); break;
case 2: Console.WriteLine("Tuesday"); break;
default: Console.WriteLine("Other day"); // Output: Other day
}
-
Conditional Statements: Control execution flow (
if
,else
,switch
). -
Looping Statements: Repeat actions (
for
,while
,foreach
). -
Jump Statements: Change flow (
break
,continue
,return
). -
Exception Handling: Handle runtime errors (
try
,catch
,finally
). - Selection Statements: Branching based on multiple conditions.
Control statements are crucial for managing the logical flow of a program, enabling it to handle conditions, repeat tasks, and manage errors effectively.
-
Introduction to C#
What is C#? -
Variables and Data Types
Understand how to declare and use variables. -
Operators
Arithmetic, relational, and logical operators in C#. -
Control Statements
If-else, switch, and looping constructs.
-
Classes and Objects
Basics of OOP in C#. -
Inheritance
How to use inheritance effectively. -
Polymorphism
Method overriding and overloading. -
Encapsulation
Understanding access modifiers.
-
LINQ Basics
Working with Language Integrated Query. -
Asynchronous Programming
Introduction to async/await. -
File Handling
Reading and writing files.
-
Number Formatting
Formatting numbers in C#. -
Exception Handling
Handling runtime errors effectively. -
Working with Dates
DateTime and time zone handling. -
Using Keyword in C#
Different usages of theusing
keyword in C#.
-
Setting Up Development Environment
Installing Visual Studio and .NET SDK. - Useful Libraries Libraries and tools for C# development.