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…

Tutorial 4 of 5 5 resources in this section

Section overview

5 resources

Introduces 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.

  • if statement: 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
}
  • switch statement: 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.

  • for loop: 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
}
  • while loop: 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-while loop: 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:

  1. Write a program that prints out all even numbers from 1 to 100 using a for loop.
  2. Write a program that takes a number as input and prints whether the number is positive or negative using an if statement.
  3. Write a program that takes a character as input and determines whether it's a vowel or consonant using a switch statement.

Solutions and explanations will be provided upon request. Happy coding!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Keyword Density Checker

Analyze keyword density for SEO optimization.

Use tool

Favicon Generator

Create favicons from images.

Use tool

PDF to Word Converter

Convert PDF files to editable Word documents.

Use tool

Percentage Calculator

Easily calculate percentages, discounts, and more.

Use tool

Random String Generator

Generate random alphanumeric strings for API keys or unique IDs.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI 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 article

AI 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 article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help