In this lesson we’ll review arrays. Also you’ll learn if, if-else, while, do-while, for, foreach loops.

Agenda

One-dimensional arrays

An array is a special variable, which can hold more than one value at a time.

In other words, array it’s a group of elements of the same type located in RAM one by one.

Following code demonstrates how to create an array according to the picture above:

int[] a = new int[6]; // array creation

//array initialization
a[0] = 12;// the first element
a[1] = 2;// the second element
a[2] = 1;
//and so on
a[3] = 232;
a[4] = 16;
a[5] = 99;

Please pay your attention that first element of array has index zero - 0.

Any array element can be accessed by its index:

//prints '99'
Console.WriteLine(a[5]);

Conditional statements

if

Use if to specify a block of code to be executed, if a specified condition is true:

Example:

bool raining = true;

Console.WriteLine("Hello!");

if (raining == true)
{
    Console.WriteLine("Please take your umbrella");
}

Console.WriteLine("Have a nice day.");

Console.WriteLine("Press ENTER to exit.");
Console.ReadLine();

if-else

Use else to specify a block of code to be executed, if the same condition is false:

Example:

string name;

Console.WriteLine("What is your name?");

name = Console.ReadLine();

Console.WriteLine("Do you have some money? (y/n)");
string answer = Console.ReadLine();

if (answer == "y")
{
    Console.WriteLine($"{name} is rich!");
}
else
{
    Console.WriteLine($"{name} should work a bit more.");
}

Console.WriteLine("Press ENTER to exit.");
Console.ReadLine();

switch

switch statement is a good alternative to if-else operator if many cases has to be verified. switch can be used instead of a long if-else chain.

switch operator diagram:

In the source code it’ll look like:

Console.WriteLine("Enter girl's name:");
string girlName = Console.ReadLine();
switch (girlName)
{
    case "Kate":
        Console.WriteLine("Just ignore her.");
        break;
    case "Samanta":
        Console.WriteLine("Give her your phone number!");
        break;
    case "Melissa":
        Console.WriteLine("She has a birthday pretty soon!");
        break;
    default:
        Console.WriteLine("Sorry, I don't know {0}", girlName);
        break;
}

With a long if-else chain it’ll look as:

Console.WriteLine("Enter girl's name:");
string girlName = Console.ReadLine();

if (girlName == "Kate")
{
    Console.WriteLine("Just ignore her.");
}
else if (girlName == "Samanta")
{
    Console.WriteLine("Give her your phone number!");
}
else if (girlName == "Melissa")
{
    Console.WriteLine("She has a birthday pretty soon!");
}
else
{
    Console.WriteLine("Sorry, I don't know {0}", girlName);
}

Loops

while

The while statement continually executes a block of statements while a particular condition is true.

It can be expressed with the following source code:

int moneyInWallet = 100;
int iPhonePrice = 499;

while (moneyInWallet <= iPhonePrice)
{
    //keep on making money
    moneyInWallet = moneyInWallet + 1;
}

do-while

do-while loop is a bit different than just while. while loop checks condition first before to execute its body code. do-while performs it vise versa. It executes body code first and then verifies condition whether to execute body code again or not. Please find do-while diagram below:

Following source demonstrates it in action:

int moneyInWallet = 0;
int iPhonePrice = 499;

do
{
    //keep on making money
    moneyInWallet++; //increment
}
while (moneyInWallet >= iPhonePrice);

for

Loop ‘for’ has more functionality than any other loop statement. ‘for’ loops can execute a block of code a predefined or calculated number of times.

This diagram demonstrates how it works:

An example below just simply iterates 9 times:

for (int index = 1; index < 10; index++)
{//Body
   Console.WriteLine("Number {0}", index);
}

It shows:

References

Presentation

Lesson2 from Alex Honcharuk

Homework

  1. Try to build and run all examples from this lesson.

  2. Implement a Calculator console application.

    • It should ask you to enter two numbers.
    • When numbers are entered, then it should as operation that has to be performed (/, *, + or -).
    • Show calculation result.
    • Start process from scratch. It should ask user to enter two numbers and so on…