Lab 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

DHA SUFFA UNIVERSITY

Department of Computer Science


CS-1002L
Object Oriented Programming Lab
Summer 2021

Lab 02 – Loops and Arrays


What are Loops?
A loop statement allows us to execute a statement or a group of statements multiple
times and following is the general from of a loop statement in most of the programming
languages. C# supports loop handling. There are basically 4 types of loops.
1. For Loop: It executes a sequence of statements multiple times and abbreviates
the code that manages the loop variable
2. While Loop: It repeats a statement or a group of statements while a given
condition is true. It tests the condition before executing the loop body.
3. Do.. while Loop: It is similar to a while statement, except that it tests the condition
at the end of the loop body
For Loop
Basic Syntax

Example of For-Loop
using System;

public class Program


{
public static void Main()
{
for (double d = 1.01D; d < 1.10; d += 0.01D)
{
Console.WriteLine("Value of i: {0}", d);
}
}
}
While Loop
Basic Syntax

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);
}

else if (i >3 && i<7)


{
Console.WriteLine(i);
continue;
}
else
{
break;
}

}
}
}
}
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;

public class Program


{
public static void Main()
{
int[,] arr2d = new int[3, 2]{
{1, 2},
{3, 4},
{5, 6}
};

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:

Function name: It is a unique name that is used to make Function call.

Return type: It is used to specify the data type of function return value.

Body: It is a block that contains executable statements.

Access specifier: It is used to specify function accessibility in the application.

Parameters: It is a list of arguments that we can pass to the function during call.

C# Function Syntax

(Type 1) C# Function: using no parameter and return type


Example # 1
using System;
namespace FunctionExample
{
class Program
{
// User defined function without return type
public void Show() // No Parameter
{
Console.WriteLine("This is non parameterized function");
// No return statement
}
// Main function, execution entry point of the program
static void Main(string[] args)
{
Program program = new Program(); // Creating Object
program.Show(); // Calling Function
}
}
}
(Type 2) C# Function: using parameter but no return type
using System;
namespace FunctionExample
{
class Program
{
// User defined function without return type
public void Show(string message)
{
Console.WriteLine("Hello " + message);
// No return statement
}
// Main function, execution entry point of the program
static void Main(string[] args)
{
Program program = new Program(); // Creating Object
program.Show("Rahul Kumar"); // Calling Function
}
}
}

(Type 3) C# Function: using parameter and return type


using System;
namespace FunctionExample
{
class Program
{
// User defined function
public string Show(string message)
{
Console.WriteLine("Inside Show Function");
return message;
}
// Main function, execution entry point of the program
static void Main(string[] args)
{
Program program = new Program();
string message = program.Show("Rahul Kumar");
Console.WriteLine("Hello " + message);
}
}
}
Lab Task 02
1. Write a program in which the program asks for marks of 10 students in Maths subject.
The program must calculate the Sum, Average, Maximum and Minimum values from the
marks. The program must be implemented using Arrays and Loops.

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.

You might also like