Lab 2
Lab 2
Lab 2
Example of For-Loop
using System;
Example of While-Loop
using System;
namespace Tutlane
{
class Program
{
static void Main(string[] args)
{
int i = 1;
while (i < 4)
{
Console.WriteLine("i value: {0}", i);
i++;
if (i == 2)
break;
}
Console.WriteLine("Press Enter Key to Exit..");
Console.ReadLine();
}
}
}
Do. While Loop
using System;
namespace Tutlane
{
class Program
{
static void Main(string[] args)
{
char i = 'a';
do
{
//i++; forgot to add this
Console.WriteLine(i);
i++;
}
while (i < 'f');
}
}
}
Break & Continue Statement
using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
if (i < 4)
{
Console.WriteLine(i);
}
}
}
}
}
Arrays in C#
An array is the data structure that stores a fixed number of literal values (elements) of the same
data type. Array elements are stored contiguously in the memory.
In C#, an array can be of three types: single-dimensional, multidimensional, and jagged array.
Here you will learn about the single-dimensional array.
Example # 1
using System;
namespace Tutlane
{
class Program
{
static void Main(string[] args)
{
int[] array = new int[5] { 1, 2, 3, 4, 5 };
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(array[i]);
}
Console.WriteLine("Press Enter Key to Exit..");
Console.ReadLine();
}
}
}
Example # 2
using System;
namespace Tutlane
{
class Program
{
static void Main(string[] args)
{
int[] array = new int[5] { 1, 4, 2, 3, 5 };
Console.WriteLine("---Initial Array Elements---");
foreach (int i in array)
{
Console.WriteLine(i);
}
Array.Sort(array);
Console.WriteLine("---Elements After Sort---");
foreach (int i in array)
{
Console.WriteLine(i);
}
Array.Reverse(array);
Console.WriteLine("---Elements After Reverse---");
foreach (int i in array)
{
Console.WriteLine(i);
}
Console.WriteLine("Press Enter Key to Exit..");
Console.ReadLine();
}
}}
Example # 3 (2D Array)
using System;
Console.WriteLine(arr2d[0, 0]);
Console.WriteLine(arr2d[0, 1]);
Console.WriteLine(arr2d[1, 0]);
Console.WriteLine(arr2d[1, 1]);
Console.WriteLine(arr2d[2, 0]);
Console.WriteLine(arr2d[2, 1]);
}
}
Functions in C#
Function is a block of code that has a signature. Function is used to execute statements
specified in the code block. A function consists of the following components:
Return type: It is used to specify the data type of function return value.
Parameters: It is a list of arguments that we can pass to the function during call.
C# Function Syntax
2. Write a program in which the program the user gets to input 2 arrays. Both the arrays
are of length 5. The user will input values in both the arrays. The program must check
that if both the arrays are equal or not. The program must be implemented using arrays,
functions and loops.
3. Create a matrix using 2D array. The matrix must have 3 row and 3 columns. You must
ask the user for the option he/she wants to select. The selection of the option must be
using switch cases. Upon selecting option 1 the program must call a function which will
multiply the entire matrix by 2. Upon selecting option 2 the program must call a function
which will take out the sum of the values of the matrix. Upon selecting option 3 the
program must call a function which prompt the user, telling the user that wrong option
was selected.