



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This document covers loops and its description with examples.
Typology: Lab Reports
1 / 6
This page cannot be seen from the preview
Don't miss anything!




// C# program to illustrate creating an array // of integers, puts some values in the array, // and prints each value to standard output. using System; namespaceABC{ class EXAMPLRY { // Main Method public static void Main() { // declares an Array of integers. int[] intArray; // allocating memory for 5 integers. intArray = new int[5]; // initialize the first elements // of the array intArray[0] = 10; // initialize the second elements // of the array intArray[1] = 20; // so on...
intArray[2] = 30; intArray[3] = 40; intArray[4] = 50; // accessing the elements // using for loop Console.Write("For loop :"); for (int i = 0; i < intArray.Length; i++) Console.Write(" " + intArray[i]); Console.WriteLine(""); Console.Write("For-each loop :"); // using for-each loop foreach(int i in intArray) Console.Write(" " + i); Console.WriteLine(""); Console.Write("while loop :"); // using while loop int j = 0; while (j < intArray.Length) { Console.Write(" " + intArray[j]); j++; } Console.WriteLine(""); Console.Write("Do-while loop :");
// Displaying Elements of array foreach(string day in weekDays) Console.Write(day + " "); } } }
The multi-dimensional array contains more than one row to store the values. It is also known as a Rectangular Array in C# because it’s each row length is same. It can be a 2D-array or 3D-array or more. To storing and accessing the values of the array, one required the nested loop. The multi-dimensional array declaration, initialization and accessing is as follows. // C# program to illustrate creating // an multi- dimensional array // puts some values in the array, // and print them using System; namespace ABC { class EXAMPLRY { // Main Method public static void Main() { // Two-dimensional array int[, ] intarray = new int[, ] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; // The same array with dimensions // specified 4 row and 2 column. int[, ] intarray_d = new int[4, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; // A similar array with string elements. string[, ] str = new string[4, 2] { { "one", "two" }, { "three", "four" }, { "five", "six" },
{ "seven", "eight" } }; // Three-dimensional array. int[,, ] intarray3D = new int[,, ] { { { 1, 2, 3 }, { 4, 5, 6 } }, { { 7, 8, 9 }, { 10, 11, 12 } } }; // The same array with dimensions // specified 2, 2 and 3. int[,, ] intarray3Dd = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } }, { { 7, 8, 9 }, { 10, 11, 12 } } }; // Accessing array elements. Console.WriteLine("2DArray[0][0] : " + intarray[0, 0]); Console.WriteLine("2DArray[0][1] : " + intarray[0, 1]); Console.WriteLine("2DArray[1][1] : " + intarray[1, 1]); Console.WriteLine("2DArray[2][0] " + intarray[2, 0]); Console.WriteLine("2DArray[1][1] (other) : "