Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are control statements in C#?
Control statements in C# determine the flow of program execution by specifying which code blocks to execute based on conditions or how many times to repeat certain operations. These statements are fundamental building blocks that allow developers to create dynamic and responsive applications.
C# provides several types of control statements that can be categorized into conditional statements (if, if-else, switch) and loop statements (for, while, do-while, foreach). Let's explore the main control statements with practical examples.
if Statement
An if statement executes a block of code only when a specified boolean condition evaluates to true.
Syntax
if(boolean_expression) {
// statement(s) will execute if the boolean expression is true
}
Example
using System;
class Program {
public static void Main() {
int age = 18;
if(age >= 18) {
Console.WriteLine("You are eligible to vote.");
}
Console.WriteLine("Program continues...");
}
}
The output of the above code is −
You are eligible to vote. Program continues...
if-else Statement
An if-else statement provides an alternative path of execution when the boolean condition is false.
Syntax
if(boolean_expression) {
// statement(s) will execute if the boolean expression is true
} else {
// statement(s) will execute if the boolean expression is false
}
Example
using System;
class Program {
public static void Main() {
int number = 15;
if(number % 2 == 0) {
Console.WriteLine(number + " is even.");
} else {
Console.WriteLine(number + " is odd.");
}
}
}
The output of the above code is −
15 is odd.
for Loop
A for loop executes a block of statements multiple times with a controlled iteration pattern. It's ideal when you know the exact number of iterations needed.
Syntax
for (initialization; condition; increment) {
statement(s);
}
Example
using System;
class Program {
public static void Main() {
Console.WriteLine("First 5 natural numbers:");
for(int i = 1; i
The output of the above code is −
First 5 natural numbers:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
while Loop
A while loop repeats a block of statements as long as a specified condition remains true. The condition is evaluated before each iteration.
Syntax
while(condition) {
statement(s);
}
Example
using System;
class Program {
public static void Main() {
int count = 1;
Console.WriteLine("Countdown:");
while(count
The output of the above code is −
Countdown:
Count: 1
Count: 2
Count: 3
Loop finished!
do-while Loop
A do-while loop is similar to a while loop, but it evaluates the condition after executing the loop body. This ensures the loop executes at least once.
Syntax
do {
statement(s);
} while(condition);
Example
using System;
class Program {
public static void Main() {
int num = 0;
do {
Console.WriteLine("Number: " + num);
num++;
} while(num
The output of the above code is −
Number: 0
Number: 1
Number: 2
Loop completed!
Control Flow Comparison
| Statement Type | When to Use | Key Characteristic |
|---|---|---|
| if | Single condition check | Executes code block only if condition is true |
| if-else | Two-way decision making | Provides alternative execution path |
| for | Known number of iterations | Compact syntax with initialization, condition, and increment |
| while | Unknown number of iterations | Tests condition before each iteration |
| do-while | At least one execution required | Tests condition after each iteration |
Conclusion
Control statements are essential for creating dynamic C# programs that can make decisions and repeat operations. Understanding when to use conditional statements versus loops, and choosing between pre-test (while) and post-test (do-while) loops, enables you to write efficient and readable code that handles various programming scenarios effectively.
