C# Lesson 3
C# Lesson 3
C# Lesson 3
Jalil Verdiyev
TOPICS
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
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.
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!");
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.
int counter = 0;
while (counter < 10)
{
Console.WriteLine(“While”);
counter++;
}
int[] arr = { 1, 2, 3, 4, 5, 6, 7 };
// Will print all elements of array
while (var a in arr)
{
Console.WriteLine(a);
}