Pages

Thursday, December 30, 2010

30/12 Operators in C#

DATE 30/12/2010 Thursday

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 –

· for loop

· while loop

· do …. while loop

· foreach loop

Every loop required three things in common

1. Initialization which sets the starting point of loop.

2. Conditionwhich sets the end of a loop.

3. Iterationthis 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 STATEMENTC# 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

GOTOIT 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”);

BREAKit 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 “);

RETURNIf 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);

}

}

0 comments:

Post a Comment