Code for Loops and examples to understand the main structure., Lab Reports of Programming Languages

This document covers loops and its description with examples.

Typology: Lab Reports

2019/2020

Uploaded on 07/22/2020

codecolon
codecolon 🇵🇰

1 document

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Accessing Array elements using different loops
// 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...
pf3
pf4
pf5

Partial preview of the text

Download Code for Loops and examples to understand the main structure. and more Lab Reports Programming Languages in PDF only on Docsity!

Accessing Array elements using different loops

// 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 + " "); } } }

Multidimensional Arrays

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) : "

  • intarray_d[1, 1]); Console.WriteLine("2DArray[1][0] (other)"
  • intarray_d[1, 0]); Console.WriteLine("3DArray[1][0][1] : "
  • intarray3D[1, 0, 1]); Console.WriteLine("3DArray[1][1][2] : "
  • intarray3D[1, 1, 2]); Console.WriteLine("3DArray[0][1][1] (other): "
  • intarray3Dd[0, 1, 1]); Console.WriteLine("3DArray[1][0][2] (other): "
  • intarray3Dd[1, 0, 2]); // using nested loop show string elements Console.WriteLine("To String element"); for (int i = 0; i < 4; i++) for (int j = 0; j < 2; j++) Console.Write(str[i, j] + " "); }