Tuesday, September 27, 2011

Arrays


Arraysit’s a set of similar type value that is store sequentially. They can be arranged in three different types like –
  1. single dimensional
  2. two dimensional
  3. Jagged arrays.
In C# arrays can be declared as fixed length or dynamic. Fixed length array can store a predefined number of items while the size of dynamic array increases as you add new items to the array.
Single Dimensional Array  These are array which store the values in the form of a row.
Syntax of defining the 1 D Array -
(Type) [] (name) = new (type) [size];
int [] arr = new int [4];
or
int [] arr ;
arr = new int[5];
or

int [] arr = {list of values};

Program
using System;
class SDArray
{
static void Main()
{
int[] arr = new int[5];
arr[0]=10;arr[1]=20;arr[2]=30;arr[3]=40;arr[4]=50;
for(int i=0;i<5;i++)
Console.Write(arr[i] + " ");
Console.WriteLine();
foreach(int i in arr)
Console.Write(i + " ");
}
}
foreach loop As discussed earlier it has been specially designed for processing values of an array or collection for each iteration of the loop one value of the array is assigned to the variable of the loop and returned to us.
In case of a for loop the variable of the loop refers to index of the array whereas in case of the foreach loop it refers to values of the array.
In case of a for loop the loop variable will always be in only whereas in case of a foreach loop the loop variables type depends upon the type of values in the array.
Note – Arrays are reference type because they give us flexibility to declare a array without specifying any size and later the size can be specified, where this flexibility is given only to reference type.
Array Class- It is a predefined class under the system name space which provides a set of methods and properties (variables) which can be applied on an array.
  • sort (array)
  • reverse (array)
  • copy (src, dest , n)
  • length
Note- short, reverse, copy is static members. It is declared like as Arr.short etc..

Program
using System;
class SDArray2
{
static void Main()
{
int[] arr = {34, 78, 20, 98, 72, 48, 56, 61, 70, 8, 15};
for(int i=0;i
Console.Write(arr[i] + " ");
Console.WriteLine();
Array.Sort(arr);
foreach(int i in arr)
Console.Write(i + " ");
Console.WriteLine();
Array.Reverse(arr);
foreach(int i in arr)
Console.Write(i + " ");
Console.WriteLine();
int[] brr = new int[10];
Array.Copy(arr, brr, 5);
foreach(int i in brr)
Console.Write(i + " ");
Console.WriteLine();
}
}
TWO Dimensional Arrays – These are the arrays which can store values in the form of rows and columns.
(type)[,](name) = new (type)[rows,cols];
int[,]arr = new int[3,4];
or
int[,]arr;
arr = new int[2,3];
or
int[,] = {list of values};

Program

Operators in C#



Arithmetic –
+, -, *, /, %
Assignment –
=, +=, -=, *=, /=, %=
Comparison –
++, !+, <, <=, >, >=, is, as, like.
Concatenation –
+
Increment and decrement –
++ , --
Logic –
&& , ||, ^
Conditional statement: - A block of code which gets executed basing on a condition is a conditional statement.
There are two types 
  • Conditional branching
  • Conditional looping.

  1. Conditional branching : this statement is also allow you to branch your code depending on whether certain condition were met or not. C# has two constructs for branching code, the if statement which allows you to test whether a specific condition is met and the switch statement which allows you to compare and expression with a no. of different values
    If ( < condition > )
    < stmts > ;
    else if ( < condition > )
    < style="mso-spacerun:yes"> >;
    ----------------------.
    else
    < stmts >;
    Program of IF Statement
    using System;
    class IfDemo
    {
    static void Main ()
    {
    int x, y;
    Console.Write("Enter x Value :");
    x = int.Parse(Console.ReadLine());
    Console.Write("Enter y Value :");
    y = int.Parse(Console.ReadLine());
    if (x>y)
    Console.WriteLine("x is greater");
    else if (x
    Console.WriteLine("y is greater");
    else
    Console.WriteLine("Both are equal");
    }
    }
    Switch case – the expression of switch case.
    switch ( < expression >)
    {
    Case < value > :
    < statement >;
    Break ;
    .-----------------.
    default :
    < stmts >;
    break;
    Using break after each case is mandatory in C# and also break has to be used after default
Program of Switch
using System;
class SwitchDemo
{
static void Main()
{
Console.Write("Enter a student no. (1-3): ");
int sno = int.Parse(Console.ReadLine());
switch (sno)
{
case 1:
Console.WriteLine("Student 1");
break;
case 2:
Console.WriteLine("Student 2");
break;
case 3:
Console.WriteLine("Student 3");
break;
default:
Console.WriteLine("Invalid student no");
break;
}
}
}
Condition loop –
C# provides 4 different loops that allows you to execute a block of code repeatedly until the certain condition is mat those are –
  1. for loop
  2. while loop
  3. do …. while loop
  4. foreach loop
Every loop required three things in common
  1. Initialization  which sets the starting point of loop.
  2. Condition  which sets the end of a loop.
  3. Iteration  this tacks you to the next level of the loop, either in forward or backward direction.
FOR LOOP 
for ( initialize ; condition , iteration)
< statement > ;
for ( int x =1 ; x <= 100; x++)
console.WriteLine (x);



While Loop
while ( condition)
< statement > ;
int x = 1;
while ( x <= 100)
}
Console.writeLine (x);
x++;
}

DO…… WHILE LOOP 
This is similar to while loop but for the first execution of the loop it doesn’t require any condition test. After completing the execution for one time then it checks for the condition to execute for next time.
do
{
< statement >;
while ( condition );
int x = 1 ;
do {
Console.WriteLine(x);
x++;
} while (x<= 100);

FOREACH LOOP  it is a special loop that has been design for the processing of arrays and collection.
Note- array is the set of similar type value, and the collection is the set of dissimilar type value.
foreach (type value in coll / array)
{
< statement>;
}
JUMP STATEMENT  C# provides a no. of statements that allow you to jump immediately to another line in a program w have four different jump statement support in C# those are –
  1. goto
  2. break
  3. continue
  4. return
GOTO  IT allows you to jump directly to another specified line in the program indicated by a label which is an identifier followed by a colon (;).
goto xxx;
console.WriteLine (“ Hello”);
xxx;
Console.WriteLine(“ goto called”);
BREAK  it is used to exit from a case in a switch statement and also used to exit from for, foreach, while and do…..while loop. Which will switch the control to the statement immediately after and of the loop.
for ( int i=1 ; i<= 100; i++)
{
Console.WriteLine (i);
if (i== 50)
break;
}
Console.WriteLine(“ End of the loop “);
CONTINUE  this can be used only in the loop statements which will jumped the control to iteration part without executing the statements present after it.
for ( int i=1 ; i<= 100; i++)
{
if (i=7)
continue;
Console.WriteLine (i);
}
Console.WriteLine(“ End of the loop “);
RETURN  If a jump statement which can come out of a method or function if required in the mid of it execution as well as it is also capable to carry a value from the method or function outside.
program using return -
using System;
class RetDemo
{
static void Main()
{
Console.Write("Enter a numeric value: ");
int no = int.Parse( Console.ReadLine() );
if ( no <>
return;
for(int i=1;i<=10;i++)
Console.WriteLine("{0} * {1} = {2}", no, i, no * i);
}
}