Pages

Tuesday, 21 June 2011

Control Statement


1 if Statement


The if statement is a control statement that executes a block of code
if an expression evaluates to true. It takes this form:

if (expression)
statement1
else

statement2
where




• expression is an expression that can be implicitly converted to bool or
a type
that contains overloading of the true and false operators
• statement1 is the embedded statement(s) to be executed if expression is true
• statement2 is the embedded statement(s) to be executed if expression is false


Note that unlike C++, C# and Java do not allow the expression to be
evaluated to 1 or 0. That is, the integer 1 does not stand for true, and 0 does
not stand for false.

Example:
int a=1;
if(a==i)
{
response.write("The integer value is One"); 
}
else
{
response.write("The integer value is not One"); 

}

2 The while Loop





The while statement executes a statement or a block of statements until a

specified expression evaluates to false. It takes this form:

while (expression) statement where 
• expression is an expression that can be implicitly converted to bool or a type

that contains overloading of the true and false operators. The expression is

used to test the loop-termination criteria.


• statement is the embedded statement(s) to be executed.


Because the test of expression takes place before each execution of the loop,

a while loop executes zero or more times. A while loop can be terminated

when a break, goto, return, or throw statement transfers control outside the

loop. To pass control to the next iteration without exiting the loop, you

use the continue statement.


while loops can be nested. In nested while loops, a break or a goto statement

in the innermost loop merely transfers control to the outer loop. A return

statement exits from the outermost loop, and a throw statement exits from the

outer loop depending on whether the thrown exception gets caught or

ignored. Listing 5.1 shows the while loop in action.



Listing 1 C# while Loop

using System;



public class Test {

  public static void Main(string[] args) {

    int i = 0;

    int j = 0;

    while (j < 5) {

      Console.WriteLine("j "+j);    



 while (i < 8) {

        Console.WriteLine("i "+i);

        ++i;

        j +=10;

      }

    }

  }

}


3 The do while Loop


The  do  while  statement  executes  a  statement  or  a  block  of  statements repeatedly  until  a  specified  expression  evaluates  to  false.  It  takes  the following form:



do statement while (expression);

 <<Code>>

where 
<<Condition>>



     expression  is  an  expression  that  can  be  implicitly  converted  to  bool  or  a type  that  contains  overloading  of  the  true  and  false  operators.  The expression is used to test the loop-termination criteria.

    statement is the embedded statement(s) to be executed. Unlike the while statement, the body loop of the do statement is executed

at least once regardless of the value of the expression. 


4 The for Loop 
he for loop executes a statement or a block of statements repeatedly until a specified expression evaluates to false. It takes the following form:

for ([initializers]; [expression]; [iterators]) statement

where

     initializers  is  a  comma-separated  list  of  expressions  or  assignment
statements to initialize the loop counters.
     expression  is  an  expression  that  can  be 
 implicitly  converted  to  bool  or  a type  that  contains  overloading  of  the  true  and  false  operators.  The
expression is used to test the loop-termination criteria.
     iterators  are  expression  statements  to  increment  or  decrement  the  loop counters.

    statement is the embedded statement(s) to be executed. 
The for statement executes the statement repeatedly as follows:

1.   The initializers are evaluated.
2.   While the expression evaluates to true, the statement(s) are executed and the iterators are evaluated.
3.   When  the  expression  becomes  false,  control  is  transferred  outside  the loop

Monday, 20 June 2011

4 C# Operators


4. C# Operators



The details of the IL language can be found in Ilinstrset.doc, which is in
C\Program Files\Microsoft.NET\FrameworkSDK\Tool Developer Guide. Reading
the screen dump of Figure 2.6, you can see that the Hello class has two  runtime for any class, and the other is the Main method. You can see that the Main method contains the entry point instruction indicating to the CLR that it is the entry point method. The next instruction of the Main method is ldstr, which loads the string literal into the local method stack. This is followed by a call to the framework class,System.Console. The last instruction is ret (which is the return of the Main method).
discussion assumes that we are dealing with numeric data types.
The +, -, /, *, and % operators have the same meaning in C# as they have in Java. In the context of two numeric operands the +,-,/,*, and %operators f dividing the LHS operand by the
RHS. shows these operators in action



Arithmetic                                                  +, –, *, /, %

Logical                                                      &&, ||, true, false, !

Bitwise                                                       &, |, ^, ~

String concatenation +

Increment, decrement                                   ++, --

Shift                                                                           >>, <<

Relational                                                   ==,< =, >=, <, >, !=

Assignment                                                 =, +=, -=, *=, /=, %=, &=, |=, ^=,

Member access                                            <<=, >>=

Casting                                                               ()

Indexing                                                             []

Conditional                                                     ?

Object creation                                                 new

Type information                                   is, as, sizeof, typeof

Overflow exception control                   checked, unchecked

                                      Pointer-based

3. C# Tools

3. C# Tools
This section explores some of the tools provided with the .NET SDK that
can improve your C# programming productivity.
Visual Studio .NET  
Microsoft provides an IDE for fast and productive development with C#. Visual  Studio  .NET  is  an  IDE  for  doing  .NET  development  with  Visual Basic, Visual  C++, and  C#.  Figure  2.1  shows  the  start  screen  of  Visual Studio .NET.

          2.1 Microsoft Visual Studio .net Startup Screen

Typically, IDEs are helpful when you have a lot of files to compile, and so you would group them into projects. Therefore, let's start a new project.  

Step 1  
Click on the New menu option under the File menu item.  
Step 2

Click  on  the  Project  submenu  option.  You  will  see  a  dialog  box  (Figure 2.2). Select the Visual C# option on the left side. On the right side you will

figure 2.2 dilog box showing types of Project
                         see several kinds of applications that you can create using the IDE.Click on Console Application.  
Step 3

Type  the  name  of  your  new  project  in  the  Name  textbox, and  type  the name  of  the  location  where  you  want  to  create  the  new  project.  For  this example, type  Project1  for  the  name, and  type  C:\WorkingFolder  for  the
location. Figure 2.3 shows what you see next.

       figure 2.3 Templet for a console application


Step 4

The  IDE  creates  a  template  class  that  you  can  modify.  The  class  file  is named  Class1.cs. This name is not very intuitive, so save the file as  Hello.cs and  delete  the  Class1.cs  file  from  the  C:\WorkingFolder\Project1  directory.
Now change the name of the class to Hello. It is not necessary that the class name be the same as the file name, but as you will realize when you have large  projects  with  lots  of  files, it's  easier  to  associate  classes  with  files  if each file has only one class with the same name.

Step 5  
The comments that you see above the class definition and the Main method are the C# equivalent of the Javadoc documentation file. This file is XML- based.  Remove  the  TODO  comment  from  the  body  of  the  Main  method.
Type the following line inside the Mainmethod's body:

Console.WriteLine("Hello");
The final screen looks something like Figure 2.4.




While typing the text in the window, you may notice pop-up boxes. This is  the  Visual  Studio  IntelliSense  feature, which  shows  you  the  syntax  of the construct as you are typing it.

Step 6

Now save your project by clicking on File and then Save All.



Behind the scenes, all your project details are stored in an XML file called Project1.csproj.  If  you  open  the  file  using  WordPad, you  will  notice  that the various project files are listed. The IDE also creates an AssemblyInfo.cs file  that  essentially  is  a  programmable  instruction  set  for  deploying  the project.  We  will  also  discuss  the  IDE  when  it  comes  time  to  create  new types  of  applications, such  as  class  libraries, Webforms, Windows applications, and so on.


3.2 The Intermediate Language Disassembler  

The Intermediate Language Disassembler (ILDASM) can do the following: 

    Open an assembly

    Show all types in an assembly

    Show  the  methods  defined  for  those  types  and  the  IL  (Intermediate

Language) that was generated for those methods



To see the IL code generated for MySample.1.cs, you open the MySample.1.EXE file using the ildasm executable. The  ildasm executable can be found in the following path: 

C:\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Bin  

If you have trouble finding the file, just do a search on the file name. 
After you  start the  ILDASM  tool,  click  on  File  and  then  Open.  Select  the EXE  file  you  want  to  view.  The  tool  then  displays  the  EXE  file  in  a  tree format.  To  see  the  IL  of  the  individual  tree  components, highlight  the component and then press Enter. Alternatively, you can do a tree dump of the file in an IL file. Click on File and then Dump. A dialog box pops up asking to save the dump in an IL file. Save the details in the Project1.il file.  The ILDASM can be called on the command line, too. Figure 2.6 shows the details of the Project1.IL file without the comments.