



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
Modern Programing Language is about different languages of today era. It explains pros and cons of some new languages and their differences with old ones. Languages like java, c sharp, c plus plus, c, fotran are included in this course. This lecture handout is about: Pointers, Arrays, Jagged Enumerations, Operator, Evaluation, Order, Conversion, Loop, Switch, Statement
Typology: Exercises
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Pointers and Arrays
Just as in C++, a pointer can be declared in relation to an array:
int[] a = {4, 5}; int *b = a;
In the above example memory location held by b is the location of the first type held by a. This first type must, as before, be a value type.
Arrays
Arrays in C# are more similar to Java than to C++. We can create an array as did in Java by using the new operator. Once created, the array can be used as usual as shown below:
int[] i = new int[2]; i[0] = 1; i[1] = 2;
By default all arrays start with their lower bound as 0. Using the .NET framework's System.Array class it is possible to create and manipulate arrays with an alternative initial lower bound.
Like Java, C# supports two types of multidimensional arrays: rectangular and jagged. A rectangular array is a single array with more than one dimension, with the dimensions' sizes fixed in the array's declaration. Here is an example:
int[,] squareArray = new int[2,3];
As with single-dimensional arrays, rectangular arrays can be filled at the time they are declared.
Jagged Arrays
Jagged arrays are multidimensional arrays with irregular dimensions. This flexibility derives from the fact that multidimensional arrays are implemented as arrays of arrays.
int[][] jag = new int[2][]; jag[0] = new int [4]; jag[1] = new int [6];
Each one of jag[0] and jag[1] holds a reference to a single-dimensional int array.
Enumerations
C# brought back enumerations which were discarded by Java designers. However, they are slightly different from C++.
An enumeration is a special kind of value type limited to a restricted and unchangeable set of numerical values. By default, these numerical values are integers, but they can also be longs, bytes, etc. (any numerical value except char) as will be illustrated below. Consider the following example:
public enum DAYS { Monday=1, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }
In C# enumerations are type-safe, by which we mean that the compiler will do its best to stop you assigning illicit values to enumeration typed variables. For instance, the following code should not compile:
int i = DAYS.Monday; DAYS d = i;
In order to get this code to compile, you would have to make explicit casts both ways (even converting from DAYS to int), ie:
int i = (int)DAYS.Monday; DAYS d = (DAYS)i;
A useful feature of enumerations is that one can retrieve the literal as a string from the numeric constant with which it is associated. In fact, this is given by the default ToString() method, so the following expression comes out as true:
DAYS.Monday.ToString()=="Monday"
Loops
C# provides a number of the common loop statements. These include while, do-while, for, and foreach.
The syntax of while, do-while, and for loops is similar to C++. The only difference is that the loop control expression must be of Boolean type.
foreach loop
The 'foreach' loop is used to iterate through the values contained by any object which implements the IEnumerable interface. It has the following syntax:
foreach (variable1 in variable2) statement[s]
When a 'foreach' loop runs, the given variable1 is set in turn to each value exposed by the object named by variable2. Here is an example:
int[] a = new int[]{1,2,3}; foreach (int b in a) System.Console.WriteLine(b);
Other Control Flow Statements
C# supports a number of control statements including break, continue, goto, if, switch, return, and throw.
switch statement
They are more or less similar to their counterparts in C++. The switch statement is however significantly different is explained below. The syntax of the switch statement is given below:
switch (expression) { case constant-expression : statements jump statement [default: statements jump statement ] }
The expression can be an integral or string expression. Control does not fall through. Jump statement is required for each block – even in default block. Fall through is allowed to stack case labels as shown in the following example:
switch(a){ case 2: Console.WriteLine("a>1 and ");
goto case 1; case 1: Console.WriteLine("a>0"); break; default: Console.WriteLine("a is not set"); break; }
As mentioned earlier, we can also use a string in the switch expression. This is demonstrated with the help of the following example:
void func(string option){ switch (option) { case "label": goto case "jump": case "quit": return; case "spin": for(;;){ } case "jump": case "unwind": throw new Exception(); default: break; } }
In this example, note that jump and unwind are stacked together and there is no jump statement in the case of jump. When a case is empty, that is there is no statement in the body of the case then it may not have any jump statement either.