Document Details
Uploaded by NobleForesight1133
Tags
Full Transcript
8/21/2023 C# Tutorial Lecture No.3 Made By : Lecturer Eng. / Ahmed Al-Antary Nested for Loop Eng.Ahmed AL-Antary 2...
8/21/2023 C# Tutorial Lecture No.3 Made By : Lecturer Eng. / Ahmed Al-Antary Nested for Loop Eng.Ahmed AL-Antary 2 1 8/21/2023 Nested for Loop Eng.Ahmed AL-Antary 3 Nested for Loop Eng.Ahmed AL-Antary 4 2 8/21/2023 Nested for Loop Eng.Ahmed AL-Antary 5 Nested for Loop Eng.Ahmed AL-Antary 6 3 8/21/2023 Nested for Loop Eng.Ahmed AL-Antary 7 do…while Repetition Statement Eng.Ahmed AL-Antary 8 4 8/21/2023 do…while Repetition Statement Eng.Ahmed AL-Antary 9 switch Multiple-Selection Statement Eng.Ahmed AL-Antary 10 5 8/21/2023 switch Multiple-Selection Statement Eng.Ahmed AL-Antary 11 switch Multiple-Selection Statement Eng.Ahmed AL-Antary 12 6 8/21/2023 break Statement Eng.Ahmed AL-Antary 13 continue Statement Eng.Ahmed AL-Antary 14 7 8/21/2023 Arrays An array is a group of variables (called elements or components) containing values that all have the same type. Arrays are objects, so they’re considered reference types (a reference to an array object in memory). The elements of an array can be either primitive types or reference types. Eng.Ahmed AL-Antary 15 Logical Array Representation The following figure shows a logical representation of an integer array called c. Eng.Ahmed AL-Antary 16 8 8/21/2023 Logical Array Representation The previous array contains 12 elements. To refer to a particular element in an array, we specify the name of the reference to the array and the position number of the element in the array. The position number of the element is called the element’s index. Example: c refer to element -45 c refer to element 6 … … c refer to element 78 Eng.Ahmed AL-Antary 17 Logical Array Representation The first element in every array has index zero. The highest index in array c is 11, which is 1 less than 12 (the number of elements in the array). An index must be a nonnegative integer that’s less than the array’s size. Eng.Ahmed AL-Antary 18 9 8/21/2023 Declaring and Creating Arrays Array objects occupy space in memory. Like other objects, arrays are created with keyword new. Example: int[ ] c; // declare the array variable. c = new int; // create the array; assign the size of the array. Creating the array in previous example can also be performed in one step as follows: int[] c = new int; Eng.Ahmed AL-Antary 19 Declaring and Creating Arrays When an array is created, each of its elements receives a default value as follows: Examples: int[] a = new int; Console.WriteLine(a); // The output is 0 bool [] b = new bool ; Console.WriteLine(b); // The output is false String[] c=new String; Console.WriteLine(c); // The output is null long[] d=new long; Console.WriteLine(d); // The output is 0 double[] e=new double; Console.WriteLine(e); // The output is 0 Eng.Ahmed AL-Antary 20 10 8/21/2023 Creating and Initializing an Array You can create an array and initialize its elements with an array initializer, as follows: Example: int[] n = {10, 20, 30, 40, 50}; In the previous example we creates a five-element array with index values 0–4. Element n is initialized to 10, n is initialized to 20, and so on. Eng.Ahmed AL-Antary 21 Arrays Eng.Ahmed AL-Antary 22 11 8/21/2023 Arrays Eng.Ahmed AL-Antary 23 Arrays Eng.Ahmed AL-Antary 24 12 8/21/2023 Arrays Eng.Ahmed AL-Antary 25 foreach Statement It iterates through the elements of an entire array or collection. The syntax of a foreach statement is: foreach ( type identifier in arrayName ) statement The type and identifier are the type and name (e.g., int number) of the iteration variable. arrayName is the name of the array through which to iterate. The type of the iteration variable must be consistent with the type of the elements in the array. Eng.Ahmed AL-Antary 26 13 8/21/2023 foreach Statement Eng.Ahmed AL-Antary 27 Multidimensional Arrays Multidimensional arrays with two dimensions are often used to represent tables of values with data arranged in rows and columns. To identify a particular table element, you specify two indices, the first identifies the element’s row and the second its column. The following figure illustrates a two-dimensional array named a with three rows and four columns. Eng.Ahmed AL-Antary 28 14 8/21/2023 Multidimensional Arrays Declaring, Creating & Initializing Multidimensional arrays String stdNames[ , ] = new String[2, 3]; stdNames[0, 0]="Ahmed"; stdNames[0, 1]="Naser"; stdNames[0, 2]="Faten"; stdNames[1, 0]="Salem"; stdNames[1, 1]="Saleh"; stdNames[1, 2]="Areej"; There is another way: String stdNames[ , ] = { {"Ahmed","Naser","Faten"}, {"Salem","Saleh","Areej"} }; Eng.Ahmed AL-Antary 29 Multidimensional Arrays Eng.Ahmed AL-Antary 30 15 8/21/2023 Homework 1. Write an application that displays the following patterns separately. Eng.Ahmed AL-Antary 31 16