Control Flow and Decision Making

Tutorial 4 of 5

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!