Control structures and loops are used in C# programming to control the flow of the program and perform operations based on specific conditions or loop processes. These structures enhance the functionality of programs and enable them to adapt to various scenarios. In this article, we will delve into the types, usage, and examples of control structures and loops in C#.
1. if-else Statement
The if-else statement is used to perform different operations based on whether a certain condition is true or false. Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | int number = 10; if (number > 0) { Console.WriteLine("The number is positive."); } else if (number < 0) { Console.WriteLine("The number is negative."); } else { Console.WriteLine("The number is zero."); } |
2. switch-case Statement
The switch-case statement is used to perform operations based on different values of a variable. Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | int choice = 2; switch (choice) { case 1: Console.WriteLine("Choice 1 is selected."); break; case 2: Console.WriteLine("Choice 2 is selected."); break; default: Console.WriteLine("Invalid choice."); break; } |
3. for Loop
The for loop is used to perform operations repeatedly based on a specific starting value and a condition. Example:
1 2 3 4 5 6 | for (int i = 0; i < 5; i++) { Console.WriteLine("Loop iteration: " + i); } |
4. while Loop
The while loop is used to perform operations repeatedly based on a certain condition. Example:
1 2 3 4 5 6 7 8 | int j = 0; while (j < 5) { Console.WriteLine("While loop iteration: " + j); j++; } |
5. do-while Loop
The do-while loop is used to perform operations at least once based on a specific condition. Example:
1 2 3 4 5 6 7 8 | int k = 0; do { Console.WriteLine("Do-while loop iteration: " + k); k++; } while (k < 5); |
6. break Statement
The break statement is used to terminate the enclosing loop or switch-case structure. Example:
1 2 3 4 5 6 7 8 9 10 | for (int m = 0; m < 10; m++) { if (m == 5) { break; } Console.WriteLine("Break statement: " + m); } |
7. continue Statement
The continue statement is used to skip the current iteration of a loop based on a specific condition. Example:
1 2 3 4 5 6 7 8 9 10 | for (int n = 0; n < 5; n++) { if (n == 2) { continue; } Console.WriteLine("Continue statement: " + n); } |
8. goto Statement
The goto statement is used to transfer control to a labeled statement in the same function. Example:
1 2 3 4 5 6 7 8 9 10 | int p = 0; start: Console.WriteLine("Usage of goto statement: " + p); p++; if (p < 5) { goto start; } |
9. Nested if-else Statement
The nested if-else statement is used to handle complex conditions using nested if-else statements. Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | int x = 10; if (x > 0) { if (x < 5) { Console.WriteLine("x is less than 5."); } else { Console.WriteLine("x is greater than 5."); } } else { Console.WriteLine("x is negative."); } |
10. foreach Loop
The foreach loop is used to iterate over each element of a collection. Example:
1 2 3 4 5 6 7 | string[] names = { "John", "Mary", "David", "Sarah" }; foreach (string name in names) { Console.WriteLine("Name: " + name); } |
Control structures and loops are fundamental tools in C# programming for controlling the flow of code and managing repeated operations. These structures enable programs to be more flexible, functional, and adaptable to complex scenarios.