Content

  • If ... Else
  • Switch Case
  • While and Do..While Loop
  • For Loop
  • Foreach Loop
  • Break and Continue
  • Arrays

If ... Else

We can use conditions to perform different actions for different decisions.

  • if : use to specify a block of code to be executed, if a specified condition is true
  • else : use to specify a block of code to be executed, if the same condition is false
  • else if : use to specify a new condition to test, if the first condition is false

Example :

int time = 22;
if (time < 10) 
{
    Console.WriteLine("Good morning.");
} 
else if (time < 20) 
{
    Console.WriteLine("Good day.");
} 
else 
{
    Console.WriteLine("Good evening.");
}
// Outputs "Good evening."

//shorthand if..else
int time = 20;
string result = (time < 18) ? "Good day." : "Good evening.";
Console.WriteLine(result);

Switch Case

We Use the switch statement to select one of many code blocks to be executed.

Example :

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;
    case 5:
    Console.WriteLine("Friday");
    break;
    case 6:
    Console.WriteLine("Saturday");
    break;
    case 7:
    Console.WriteLine("Sunday");
    break;
    default:
    Console.WriteLine("No Match");
    break;
}
    // Outputs "Thursday" (day 4)

While and Do..While Loop

The while loop loops through a block of code as long as a specified condition is True

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.

Example :

int i = 0;
while (i < 5) 
{
    Console.WriteLine(i);
    i++;
}

int i = 0;
do 
{
    Console.WriteLine(i);
    i++;
}
while (i < 5);

For Loop

When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop

for (int i = 0; i < 5; i++) 
{
    Console.WriteLine(i);
}

Foreach Loop

There is also a foreach loop, which is used exclusively to loop through elements in an array

string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
foreach (string i in cars) 
{
    Console.WriteLine(i);
}

Break and Continue

The break statement can also be used to jump out of a loop.

The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.

//This example jumps out of the loop when i is equal to 4
for (int i = 0; i < 10; i++) 
{
    if (i == 4) 
    {
    break;
    }
    Console.WriteLine(i);
}

//This example skips the value of 4
for (int i = 0; i < 10; i++) 
{
    if (i == 4) 
    {
    continue;
    }
    Console.WriteLine(i);
}

Arrays

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.To declare an array, define the variable type with square brackets

You access an array element by referring to the index number

To find out how many elements an array has, use the Length property

There are many array methods available, for example Sort(), which sorts an array alphabetically or in an ascending order

Other useful array methods, such as Min, Max, and Sum, can be found in the System.Linq namespace

using System;
using System.Linq;
    
namespace MyApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
            Console.WriteLine(cars[0]);
            Console.WriteLine(cars.Length);
            Array.Sort(cars);
            foreach (string i in cars)
            {
            Console.WriteLine(i);
            }

            int[] myNumbers = {5, 1, 8, 9};
            Console.WriteLine(myNumbers.Max());  // returns the largest value
            Console.WriteLine(myNumbers.Min());  // returns the smallest value
            Console.WriteLine(myNumbers.Sum());  // returns the sum of elements
        }
    }
}