codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🔷

C# Programming

6 / 60 topics
6Control Structures in C#7If-Else Statements8Switch Case Statements9For Loops10While Loops11Do-While Loops12Break and Continue Statements
Tutorials/C# Programming/Control Structures in C#
🔷C# Programming

Control Structures in C#

Updated 2026-05-15
10 min read

Control Structures in C#

Introduction

In programming, control structures are essential for controlling the flow of execution based on certain conditions. They allow you to make decisions and repeat actions, making your code more dynamic and responsive. In this tutorial, we will explore two fundamental types of control structures in C#: conditional statements and loops.

Concept

Conditional Statements

Conditional statements are used to execute different blocks of code based on whether a condition is true or false. The most common conditional statement in C# is the if statement.

If Statement

The basic syntax of an if statement is as follows:

if (condition)
{
    // Code to execute if the condition is true
}

You can also use `else` and `else if` clauses to handle multiple conditions:

```csharp
if (condition1)
{
    // Code to execute if condition1 is true
}
else if (condition2)
{
    // Code to execute if condition2 is true
}
else
{
    // Code to execute if none of the above conditions are true
}

### Loops

Loops allow you to repeat a block of code multiple times. There are several types of loops in C#, including `for`, `while`, and `do-while`.

#### For Loop

The `for` loop is used when you know the number of iterations in advance:

```csharp
for (initialization; condition; increment)
{
    // Code to execute repeatedly
}

#### While Loop

The `while` loop continues to execute as long as a specified condition is true:

```csharp
while (condition)
{
    // Code to execute repeatedly
}

Do-While Loop

The do-while loop is similar to the while loop, but it guarantees that the code block will be executed at least once:

do
{
    // Code to execute repeatedly
} while (condition);

Examples

If Statement Example

Let's look at an example of using an if statement to check if a number is positive or negative:

csharp
1int number = -5;
2
3if (number > 0)
4{
5 Console.WriteLine("The number is positive.");
6}
7else if (number < 0)
8{
9 Console.WriteLine("The number is negative.");
10}
11else
12{
13 Console.WriteLine("The number is zero.");
14}
Output
The number is negative.

For Loop Example

Here's an example of a for loop that prints numbers from 1 to 5:

csharp
1for (int i = 1; i <= 5; i++)
2{
3 Console.WriteLine(i);
4}
Output
1
2
3
4
5

While Loop Example

This example demonstrates a while loop that counts down from 5 to 1:

csharp
1int count = 5;
2
3while (count > 0)
4{
5 Console.WriteLine(count);
6 count--;
7}
Output
5
4
3
2
1

Do-While Loop Example

Finally, here's an example of a do-while loop that asks the user for input until they enter "exit":

csharp
1string userInput;
2
3do
4{
5 Console.Write("Enter something (or 'exit' to quit): ");
6 userInput = Console.ReadLine();
7} while (userInput != "exit");

What's Next?

In the next section, we will delve deeper into conditional statements and explore more advanced topics such as nested if statements and switch-case constructs. Stay tuned!

Info

Remember, control structures are powerful tools that can make your code more efficient and easier to understand. Practice using them in different scenarios to build a strong foundation.


PreviousOperators in C#Next If-Else Statements

Recommended Gear

Operators in C#If-Else Statements