control statements in c#

Control Statements in C#-:  C# provides a variety of statements. Most of these statements will be familiar to developers who have programmed in C and C++.

C# provide following statement-:

1     labeled-statement
2     declaration-statement
3      embedded-statement-: In the c# embedded statement has many types as like

  • block
  • empty-statement
  • expression-statement
  • selection-statement
  • iteration-statement
  • jump-statement
  • try-statement
  • checked-statement
  • unchecked-statement
  • lock-statement
  • using-statement
  • yield-statement

The embedded-statement non-terminal is used for statements that appear within other statements. The use of embedded-statement rather than statement excludes the use of declaration statements and labeled statements in these contexts. The example

void    F   (bool b) {
                                  if (b)
                                  int i = 44;
                                }

results in a compile-time error because an if statement requires an embedded-statement rather than a statement for its if branch. If this code were permitted, then the variable i would be declared, but it could never be used.

Block -: A block permits multiple statements to be written in contexts where a single statement is allowed. A block is executed as following condition

  •  If the block is empty, control is transferred to the end point of the block.
  • If the block is not empty, control is transferred to the statement list. When and if control reaches the end point of the statement list, control is transferred to the end point of the block.

block:
{ statement-listopt }

Empty statement-: An empty statement is used when there are no operations to perform in a context where a statement is required.
An empty-statement does nothing. For example

      while (text.indexof(“xx”) ! =  -1);

         {

              text=text.replace(“xx’,”x”);

           }

Labeled-Statement -:A labeled-statement permits a statement to be prefixed by a label. Labeled statements are permitted in blocks, but are not permitted as embedded statements.

labeled-statement:
identifier : statement

A labeled statement declares a label with the name given by the identifier. The scope of a label is the whole block in which the label is declared, including any nested blocks. It is a compile-time error for two labels with the same name to have overlapping scopes.

int F(int x)

    {

                  if (x >= 0) goto x;  

                     x = -x; 

                   x: return x;

     }

Declaration Statement-:A declaration-statement declares a local variable or constant. Declaration statements are permitted in blocks, but are not permitted as embedded statements. This is two types

  • Local variable declaration-:A local-variable-declarator consists of an identifier that names the variable
  • Local constant declaration-: A constant-declarator consists of an identifier that names the constant.

Expression Statement -: An expression-statement evaluates a given expression. The value computed by the expression, if any, is discarded. Execution of an expression-statement evaluates the contained expression and then transfers control to the end point of the expression-statement. The end point of an expression-statement is reachable if that expression-statement is reachable.

Selection statements-: In this selection statement select one of a number of possible statements for execution based on the value of some expression. there are two type statement in this

  • if-statement
  • switch-statement

if Statement-: The if statement selects a statement for execution based on the value of a boolean expression.

if-statement:
if ( boolean-expression ) embedded-statement
if ( boolean-expression ) embedded-statement else embedded-statement

if statement is executed as following way

  • The boolean-expression  is evaluated.
  • If the boolean expression yields true, control is transferred to the first embedded statement. When and if control reaches the end point of that statement, control is transferred to the end point of the if statement.
  • If the boolean expression yields false and if an else part is present, control is transferred to the second embedded statement. When and if control reaches the end point of that statement, control is transferred to the end point of the if statement.
  • If the boolean expression yields false and if an else part is not present, control is transferred to the end point of the if statement.
  • For example

if (x) {
                     if (y) {
                 F();
        }
             else {
                           G();
                     }
          }

switch Statement-: The switch statement selects for execution a statement list having an associated switch label that corresponds to the value of the switch expression.

switch-statement:
switch ( expression ) switch-block

switch-block:
{ switch-sectionsopt }

switch-sections:
switch-section
switch-sections switch-section

switch-section:
switch-labels statement-list

switch-labels:
switch-label
switch-labels switch-label

switch-label:
case constant-expression :
default :

A switch-statement consists of the keyword switch, followed by a parenthesized expression (called the switch expression), followed by a switch-block. The switch-block consists of zero or more switch-sections, enclosed in braces. Each switch-section consists of one or more switch-labels.

The governing type of a switch statement is established by the switch expression.

  •  If the type of the switch expression is sbyte, byte, short, ushort, int, uint, long, ulong, bool, char, string, or an enum-type, or if it is the nullable type corresponding to one of these types, then that is the governing type of the switch statement.
  • Otherwise, exactly one user-defined implicit conversion must exist from the type of the switch expression to one of the following possible governing types: sbyte, byte, short, ushort, int, uint, long, ulong, char, string, or, a nullable type corresponding to one of those types.
  • Otherwise, if no such implicit conversion exists, or if more than one such implicit conversion exists, a compile-time error occurs.

 

Iteration statements-: It repeatedly execute an embedded statement.It has four types.

  1. While statements
  2. do statements
  3. for statements
  4. Foreach statements

 

While statements-:The while statement conditionally executes an embedded statement zero or more times.It repeats a statement or a group of statements while a given condition is true. It tests the condition before executing the loop body.

Do statement -: The do statement conditionally executes an embedded statement one or more times.

do-statement:
do embedded-statement while ( boolean-expression ) ;

For Statement -:The for statement evaluates a sequence of initialization expressions and then, while a condition is true, repeatedly executes an embedded statement and evaluates a sequence of iteration expressions.

in other words we can say that The for loop is used to go through a code block a specified number of times. It uses three parameters. The first parameter initializes a counter and is always executed once, before the loop. The second parameter holds the condition for the loop and is checked before each iteration. The third parameter contains the increment of the counter and is executed at the end of each iteration.

Foreach Loop-:The foreach loop provides an easy way to iterate through arrays. At each iteration, the next element in the array is assigned to the specified variable (the iterator) and the loop continues to execute until it has gone through the entire array.
int[] a = { 1, 2, 3 };
foreach (int n in a) {
System.Console.Write(n); // “123”
}

Jump statements-: It  unconditionally transfer control.

  • break-statement-:The break statement exits the nearest enclosing switch, while, do, for, or foreach statement.
  • continue-statement-:The continue statement starts a new iteration of the nearest enclosing while, do, for, or foreach statement.
  • goto-statement-:The goto statement transfers control to a statement that is marked by a label.
    goto-statement:
    goto identifier ;
    goto case constant-expression ;
    goto default ;
  • return-statement-:The return statement returns control to the caller of the function member in which the return statement appears.
    return-statement:
    return expressionopt ;
  • throw-statement-:A throw statement with an expression throws the value produced by evaluating the expression. The expression must denote a value of the class type System.Exception, of a class type that derives from System.Exception or of a type parameter type that has System.Exception (or a subclass thereof) as its effective base class. If evaluation of the expression produces null, a System.NullReferenceException is thrown instead.

 

 

 

Try Statement -: The try statement provides a mechanism for catching exceptions that occur during execution of a block. Furthermore, the try statement provides the ability to specify a block of code that is always executed when control leaves the try statement.for example

try in c# programme

The checked and unchecked statements -: These are used to control the overflow checking context for integral-type arithmetic operations and conversions.
checked-statement:
checked block

unchecked-statement:
unchecked block

The lock statement-: This obtains the mutual-exclusion lock for a given object, executes a statement, and then releases the lock.

lock-statement:
lock ( expression ) embedded-statement

Using Statement-:  It obtains one or more resources, executes a statement, and then disposes of the resource.A resource is a class or struct that implements System.IDisposable, which includes a single parameter-less method named Dispose. Code that is using a resource can call Dispose to indicate that the resource is no longer needed. If Dispose is not called, then automatic disposal eventually occurs as a consequence of garbage collection.

 yield statement-: It is used in an iterator block  to yield a value to the enumerator object or enumerable object of an iterator or to signal the end of the iteration. for example

c# statements

 

 

 

Leave a Comment