If-Else Statements
If-else statements allow you to execute different blocks of code based on a specific condition.
Here's an example:
int num = 10; if(num > 0) { Console.WriteLine("The number is positive."); } else { Console.WriteLine("The number is negative."); }In this example, if the value of num is greater than 0, the first block of code will be executed, and if it's less than or equal to 0, the second block of code will be executed.
Switch Statements
Switch statements are used to execute different blocks of code based on the value of a variable.
Here's an example:
int num = 2; switch(num) { case 1: Console.WriteLine("The number is 1"); break; case 2: Console.WriteLine("The number is 2"); break; case 3: Console.WriteLine("The number is 3"); break; default: Console.WriteLine("The number is not between 1 and 3"); break; }In this example, the value of num is checked against three possible values. If it matches one of those values, the corresponding block of code is executed. If it doesn't match any of those values, the default block of code is executed.
Loops
Loops are used to execute a block of code repeatedly. There are three types of loops in C#: for loops, while loops, and do-while loops.
For Loops:
For loops are used when you know the number of times you want to execute a block of code.
Here's an example:
for(int i = 0; i < 10; i++) { Console.WriteLine(i); }In this example, the block of code inside the for loop is executed 10 times, with the value of i incrementing by 1 each time.
While Loops:
While loops are used when you want to execute a block of code as long as a specific condition is true.
Here's an example:
int num = 0; while(num < 5) { Console.WriteLine(num); num++; }In this example, the block of code inside the while loop is executed as long as num is less than 5.
Do-While Loops:
Do-while loops are similar to while loops, but the block of code is always executed at least once, even if the condition is false.
Here's an example:
int num = 5; do { Console.WriteLine(num); num--; } while(num > 0);In this example, the block of code inside the do-while loop is executed at least once, even though the value of num is already greater than 0
Jump Statements
These statements allow you to transfer control to another part of your program. There are three types of jump statements in C#:
break: This statement is used to terminate the nearest enclosing loop or switch statement.
continue: This statement is used to skip the current iteration of a loop and move to the next iteration.
goto: This statement is used to transfer control to a labeled statement in your program.
Here's an example:
for (int c = 0; c < 5; c++) { if (c == 3) { break; } Console.WriteLine(c); } for (int d = 0; d < 5; d++) { if (d == 3) { continue; } Console.WriteLine(d); } for (int e = 0; e < 5; e++) { if (e == 3) { goto mylabel; } Console.WriteLine(e); } mylabel: Console.WriteLine("Jumped to mylabel");Now that you understand the different types of control statements in C#, let's dive into some code examples to see how they work in practice.
Code Example
Let's start with a simple program that uses some of the control statements we've discussed so far.
using System; namespace ControlStatementsExample { class Program { static void Main(string[] args) { // If statement int x = 10; if (x > 5) { Console.WriteLine("x is greater than 5"); } // If-else statement int y = 3; if (y > 5) { Console.WriteLine("y is greater than 5"); } else { Console.WriteLine("y is less than or equal to 5"); } // Switch statement int day = 4; switch (day) { case 1: Console.WriteLine("Monday"); break; case 2: Console.WriteLine("Tuesday"); break; case 3: Console.WriteLine("Wednesday"); break; case 4: Console.WriteLine("Thursday"); break; case 5: Console.WriteLine("Friday"); break; default: Console.WriteLine("Weekend"); break; } // While loop int i = 0; while (i < 5) { Console.WriteLine(i); i++; } // Do-while loop int j = 0; do { Console.WriteLine(j); j++; } while (j < 5); // For loop for (int k = 0; k < 5; k++) { Console.WriteLine(k); } // Nested loop for (int a = 0; a < 3; a++) { for (int b = 0; b < 3; b++) { Console.WriteLine("a: {0}, b: {1}", a, b); } } // Break statement for (int c = 0; c < 5; c++) { if (c == 3) { break; } Console.WriteLine(c); } // Continue statement for (int d = 0; d < 5; d++) { if (d == 3) { continue; } Console.WriteLine(d); } // Goto statement for (int e = 0; e < 5; e++) { if (e == 3) { goto mylabel; } Console.WriteLine(e); } mylabel: Console.WriteLine("Jumped to mylabel"); } } }In conclusion, control statements are an essential part of C# programming language. They allow you to make decisions based on different conditions and execute code accordingly. In this tutorial, we have covered the basics of control statements including the if-else statement, switch statement, for loop, while loop, do-while loop, and foreach loop. We have also provided code examples to demonstrate how to use these control statements in practical scenarios. By mastering control statements, you can write efficient and flexible code that can perform complex tasks. We hope that this tutorial has provided you with a solid understanding of control statements in C#, and you are now equipped with the knowledge to apply them in your own programs.