csharp_statements.md - brainchildservices/curriculum GitHub Wiki

SLIDE-1

  • Csharp Statements

    • The actions of a program are expressed using statements. C# supports several different kinds of statements, a number of which are defined in terms of embedded statements.

SLIDE-2

  • End points and reachability
    • Every statement has an end point.

    • In intuitive terms, the end point of a statement is the location that immediately follows the statement.

    • For example, when control reaches the end point of a statement in a block, control is transferred to the next statement in the block.

    • If a statement can possibly be reached by execution, the statement is said to be reachable.

    • Conversely, if there is no possibility that a statement will be executed, the statement is said to be unreachable.

    • To determine whether a particular statement or end point is reachable, the compiler performs flow analysis according to the reachability rules defined for each statement.

    • The flow analysis takes into account the values of constant expressions (https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/expressions#constant-expressions) that control the behavior of statements, but the possible values of non-constant expressions are not considered.

SLIDE-3

  • C# Blocks
    • A block is a combination of zero or more statements that is enclosed inside curly brackets { }.
      • Blocks with statements

                         using System;
        
                         namespace Blocks
                         {
                         	class BlockExample
                         	{
                         		public static void Main(string[] args)
                         		{
                         			double temperature = 42.05;
                         			if (temperature > 32)
                         			{	// Start of block
                         				Console.WriteLine("Current temperature = {0}", temperature);
                         				Console.WriteLine("It's hot");
                         			}	// End of block
                         		}
                         	}
                         }
        

SLIDE-3(DOWNWARDS)

- Here, the two statements inside { }:

                        Console.WriteLine("Current temperature = {0}", temperature);

                                    and

                        Console.WriteLine("It's hot");

  forms a block.

SLIDE-4

  • EMPTY STATEMENT
    • An empty_statement does nothing.
    • An empty statement is used when you no need to perform an operation where a statement is required.
    • It simply transfers control to the end point of the statement.
    • Execution of an empty statement simply transfers control to the end point of the statement. Thus, the end point of an empty statement is reachable if the empty statement is reachable.
    • It is also very useful with a while loop with the blank body and label statements.

SLIDE-4(DOWNWARDS)

  • Example:

               while(DoSomething())
               { 
                 ;
               } 
    
               and 
    
               void M() {
                         if(someCondition) goto exit;
                         // ...
                         exit: ;
                         }
    

SLIDE-5

  • LABELED STATEMENT
    • Labels are used to transfer program control directly to the specified statement.
    • The scope of a label is the entire function in which it is declared.
    • Generally, in c#, the defined labeled statement must always exist in the goto statement's scope.
    • A labeled_statement permits a statement to be prefixed by a label.

SLIDE-6

  • DECLARATION STATEMENT
    • A declaration_statement declares a local variable or constant.

    • Declaration statements are permitted in blocks, but are not permitted as embedded statements.

    • Example: (https://dotnetfiddle.net/LVhGgx)

                   public static void Main()
                   {
                   	int a = 1, b = 2, c = 5;     //Declaration statement
                       Console.WriteLine("{0} {1} {2}", a, b, c);      
                   }
      

SLIDE-7

  • EXPRESSION STATEMENT
    • An expression_statement evaluates a given expression.
    • The value computed by the expression, if any, is discarded.
    • Not all expressions are permitted as statements. In particular, expressions such as x + y and x == 1 that merely compute a value (which will be discarded), are not permitted as statements.
    • 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.

SLIDE-8

  • Csharp STATEMENT CONTAINS

      - A block permits multiple statements to be written in contexts where a single statement is allowed. 
        A block consists of a list of statements written between the delimiters { and }.
      - Declaration statements are used to declare local variables and constants.
      - Expression statements are used to evaluate expressions. Expressions that can be used as statements include method invocations, 
        object allocations using the new operator, assignments using = and the compound assignment operators, 
        increment and decrement operations using the ++ and -- operators and await expressions.
      - Selection statements are used to select one of a number of possible statements for execution 
        based on the value of some expression. This group contains the if and switch statements.
      - Iteration statements are used to execute repeatedly an embedded statement. 
        This group contains the while, do, for, and foreach statements.
    

SLIDE-8(DOWNWARDS)

    - Jump statements are used to transfer control. This group contains the break, continue, goto, throw, return, and yield statements.
    - The try...catch statement is used to catch exceptions that occur during execution of a block, 
      and the try...finally statement is used to specify finalization code that is always executed, 
      whether an exception occurred or not.
    - The checked and unchecked statements are used to control the overflow-checking context for integral-type arithmetic operations and conversions.
    - The lock statement is used to obtain the mutual-exclusion lock for a given object, execute a statement, and then release the lock.
    - The using statement is used to obtain a resource, execute a statement, and then dispose of that resource.

SLIDE-9

- The following lists the kinds of statements that can be used:

    - Local variable declaration.
    - Local constant declaration.
    - Expression statement.
    - if statement.
    - switch statement.
    - while statement.
    - do statement.
    - for statement.
    - foreach statement.
    - break statement.
    - continue statement.
    - goto statement.
    - return statement.
    - yield statement.
    - throw statements and try statements.
    - checked and unchecked statements.
    - lock statement.
    - using statement.

REF link-