C# / C# Basics
Control Flow and Decision Making
This tutorial will cover the control flow in C#, including conditional statements and loops. You'll learn how to control the flow of your program based on conditions and how to it…
Section overview
5 resourcesIntroduces the fundamental concepts of C#, including syntax, data types, and control structures.
Control Flow and Decision Making in C
1. Introduction
Goal of the Tutorial
The goal of this tutorial is to help you understand the control flow in C# and introduce you to conditional statements and loops.
Learning Outcomes
By the end of this tutorial, you will be able to:
- Understand how to use conditional statements in C#
- Understand how to use loops in C#
- Control the flow of a program
- Iterate over collections of data.
Prerequisites
Basic understanding of C# programming is required. Familiarity with the syntax and basic data types in C# would be beneficial.
2. Step-by-Step Guide
Conditional Statements
In C#, we have two primary types of conditional statements: if statements and switch statements.
ifstatement: This is the most basic control flow statement. It allows the program to execute a certain section of code only if a particular test evaluates to true.
if (condition)
{
// code to be executed if condition is true
}
switchstatement: This is a type of selection statement that allows a variable to be tested for equality against a list of values.
switch (variable)
{
case value1:
// code to be executed if variable equals value1;
break;
case value2:
// code to be executed if variable equals value2;
break;
default:
// code to be executed if variable doesn't match any values;
break;
}
Loops
In C#, we have three primary types of loops: for loops, while loops, and do-while loops.
forloop: The for loop is used when you know in advance how many times the script should run.
for (initialization; condition; increment/decrement)
{
// code block to be executed
}
whileloop: The while loop loops through a block of code as long as a specified condition is true.
while (condition)
{
// code block to be executed
}
do-whileloop: The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.
do
{
// code block to be executed
}
while (condition);
3. Code Examples
Let's see some practical examples of these concepts.
Example: if Statement
int a = 10;
int b = 20;
if (a < b)
{
Console.WriteLine("a is less than b");
}
// The output will be "a is less than b"
In this example, the condition a < b is true, so the code within the if statement is executed.
Example: 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;
default:
Console.WriteLine("Invalid day");
break;
}
// The output will be "Thursday"
In this example, the variable day is checked against each case. When it matches with 4, the corresponding code is executed.
Example: for Loop
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
// The output will be
// 0
// 1
// 2
// 3
// 4
In this example, the for loop runs five times, and each time it prints the value of i.
4. Summary
In this tutorial, we covered control flow in C#, including conditional statements (if and switch) and loops (for, while, and do-while).
Next, you should practice these concepts and try to create some programs using these control flow statements.
Additional resources:
- Microsoft C# documentation
- C# Station
5. Practice Exercises
Here are some exercises to help you practice:
- Write a program that prints out all even numbers from 1 to 100 using a
forloop. - Write a program that takes a number as input and prints whether the number is positive or negative using an
ifstatement. - Write a program that takes a character as input and determines whether it's a vowel or consonant using a
switchstatement.
Solutions and explanations will be provided upon request. Happy coding!
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article