C# Lesson 3

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

C# LESSON 3

Jalil Verdiyev
TOPICS

• Switch-case, case jumping


• Getting user input
• Arrays
• Loops

2
CASE JUMPING
We use case jumping, when two or more cases do the same
thing

int choice = 2;
switch (choice)
{
case 1:
case 2:
case 3:
//Do the common thing
break;
default:
//Handle default case
break;
}
USER INPUT
To make our programs more interactive we should get some inputs, from
user. To achieve this we can use Console.ReadLine() or
Console.ReadKey().

Tip: Console.ReadLine() always returns string so, if you want to get number you
should convert it

// Getting input
string input = Console.ReadLine();
int num = Convert.ToInt32(Console.ReadLine());
// or
int num1 = int.Parse(Console.ReadLine());
ARRAYS
Arrays are used when we want to store multiple same data
types in a variable and in sequential order. All arrays are zero-
based, meaning that counting starts from 0. In c# we usually
have 3 types of arrays:

• Single-dimensional arrays
• Multi-dimensional arrays
• Jagged arrays
SINGLE-DIMENSION
ARRAYS

Single dimensional arrays are commonly used arrays that


holds same type of data. For creating(declaring) a single
dimensional array, type[] name is enough. Let's look some
examples:

// Declaring array with length 5


int[] ints = new int[5];
// Declaring array with collection
initializer
int[] ints1 = new int[] { 1, 3, 5,
6, 7, 9 };
int[] ints2 = { 1, 3, 5, 6, 7, 9 };

After declaring you can access its elements by indexer.


Console.WriteLine(ints1[2]);
MULTI-DIMENSIONAL
ARRAYS
Multi-dimensional arrays are mostly used in mathematical
calculations, or in some image processing. They kind of
resemble matrixes.

int[,] twoDimensional = new int[4, 2];


int[,,] threeDimensional = new int[3, 3, 3];
int[,] collection1 = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7,
8 } };one of its elements you should use n times of indexes for n
To access
dimensional, meaning if you two have two dimensional you should use
two indexes. Let's look examples:

// Prints 2 as second element of first


array of collection is '2’
Console.WriteLine(collection1[0, 1]);

For getting the dimension you can use Rank property.


JAGGED ARRAYS
Jagged arrays are arrays in arrays. They aren't mostly used, but
it's nice to know about them. Let's look some examples:

int[][] jagArr = new int[3][];


jagArr[0] = new int[] { 1, 2, 3, 4 };
jagArr[1] = new int[] { 1, 2, 3, 4, 5, 6, 80, 9 };

For accessing:

Console.WriteLine(jagArr[1][3]);
STORING DIFFERENT
TYPES IN ONE ARRAY
If you want to store anything in arrays you can take type as
object because it is base for all types.

object[] objects = new


object[10];
objects[0] = 1;
objects[1] = "Test";
objects[2] = 'A';
objects[3] = 123.45;
LOOPS
We use loops when we have repetitive tasks, such as printing
10 times of "Hello World!". The naive approach is to write 10
times of Console.WriteLine() like in example:

Console.WriteLine("Hello World!");
Console.WriteLine("Hello World!");
Console.WriteLine("Hello World!");
Console.WriteLine("Hello World!");
Console.WriteLine("Hello World!");
Console.WriteLine("Hello World!");
Console.WriteLine("Hello World!");
Console.WriteLine("Hello World!");
Console.WriteLine("Hello World!");
Console.WriteLine("Hello World!");

But what if we need to print it 100 or more times? There


comes loops
THERE ARE 4 TYPES OF LOOPS IN C#:

Do-
While
While

For Foreach
WHILE
While is so simple it takes a condition and if condition is true
it continues the loop while is false. Here are some examples:

while (true)
{
Console.WriteLine("Hello World!");
}

As you can see the condition is always true so, it never stops writing
"Hello World". For making it stop we should declare some terminal
conditions like:
int i = 0;
while (i < 10)
{
Console.WriteLine("Hello World!");
i++;
}
Now, each time we increase i by 1 and check if it equals 10, when it equals
to 10 the loop will stop.
DO-WHILE
Do while looks like, while loop but there is a small difference
between them. While first checks the given condition then
does the job, however do while first does the job then checks
the given condition.

// Not prints "While"


while (false)
{
Console.WriteLine(“While”);
}
// Prints "Do-While"
do
{
Console.WriteLine(“Do-While”);
} while (false);
FOR
For looks like while. But the main difference is the
initialization of iterator(or counter) is inside the for loop. Let's
take a look at example:

int counter = 0;
while (counter < 10)
{
Console.WriteLine(“While”);
counter++;
}

for (int i = 0; i < 10; i++)


{
Console.WriteLine(“For”);
}

The benefit of for, is memory because you can use the


variable counter after for loop, but you can't use i after for.
FOREACH
It is mostly used with collections(in other words arrays). To
iterate through an array, you can use foreach. Let's look
example:

int[] arr = { 1, 2, 3, 4, 5, 6, 7 };
// Will print all elements of array
while (var a in arr)
{
Console.WriteLine(a);
}

You might also like