By default it will show the recent post/content added.
To read from starting....Click Here
See "ChapterWise Posts" right side for complete list.

Tuesday, 21 April 2015

Control Instructions

Decision Control Instructions

By default, the instructions in a program are executed sequentially. We can change this order or make certain statements to execute based on some condition.

The decision control instruction can be implemented in C# using if, if-else, conditional operators and switch statement.

 if(condition or any expression evaluates to bool value)  
 execute this statement; //this particular statement(or statements if we group them in block) will get executed only when the above condition is true.



We express a condition using relational operators.











There is difference in = and ==.
= is for assignment and == is for comparison.

If we want to keep a separate branch for execution when a condition is false, use if-else.

if(condition or any expression evaluates to bool value)  
 execute this statement; // this will be executed when the above expression evaluates to true.  
 else  
 execute this statement; // this will be executed when the above expression evaluates to false.  

If want to check more conditions after a condition is true or false we can use Nested if-else.

if(condition1 or any expression evaluates to bool value)  
 {  
    If(condition2)  
     {  
         execute this statement; // this will be executed when the above expression evaluates to true.  
     }  
    else  
      {  
          Execute this statement;  
      }  
 }  
 else  
 {  
     If(condition3)  
     {  
         execute this statement; // this will be executed when the above expression evaluates to true.  
     }  
    else  
      {  
          Execute this statement;  
      }  
 }  

Instead of nested we can use below option also.

if(condition1 or any expression evaluates to bool value)  
 {  
         execute this statement; // this will be executed when the above expression evaluates to true.  
 }  
 else if(condition2) //executes condition1 is false 2 is true  
 {  
        Execute this statement;  
 }  
 Else  //executes condition1 is false 2 is false  
 {  
        Execute this statement;  
 }  

We can combine conditions using logical operators.&&(and),||(or),!(not)

As we are including logical operators also, below is the revised hierarchy.














Conditional/ternary operator ( ?:)
Expression1?expression2:expression3   à checks expression1, if it evaluates to true then expression2 gets executed else expression3 will.

Note: The execution for ternary operator is right to left.

Loop Control Instructions

This particular type is for executing set of instructions repeatedly.
There are 3 types.(for,while,do-while)

 while(condition/expression)  
 {  
    //these statements will be executed repeatedly till the above expression evaluates to false.  
 }  
 do  
 {  
   //set of statements   
 }while(condition/expression)  

The difference between while and do-while is in while the checking is done before execution of statements whereas in do-while the checking is done later.

You can do this by keeping some counter variable and increment it inside the loop.

This loop counter logic is implemented directly in for.

 for(initialize counter;test counter;increment counter)  
 {  
    Set of statements  
 }  

Example:

 for(int i=0;i<=89;i++) // we can do multiple initializations for(int i=0,j=8;i<=89;i++)   
 {  
   //set of statements executed repeatedly 90 times—0 to 89.  
 }  

We can use break to stop the loop execution in the middle of repetition. Whereas continue will stop the current execution and continue the loop from next counter.

switch, can be used if we are having multiple options and select one of the branch based on condtion or value of switch variable.This will be useful instead of sometimes to use instead of very big nested if branch.

switch(expression)  
 {  
    case constant 1:  
       do this;  
       break;   //if break is not there then the execution will continue to next branch also.  
    case constant 2:  
       do this;  
       break;  
    case constant 3:  
       do this;  
       break;  
    case constant 4:  
    case constant 5:  // this branch will be executed if the expression evaluates to constant 4 or 5  
       do this;  
       break;  
    case default:  
       do this;  
       break;  
 }  

goto Keyword is been used traditionally since batch scripts.
It is not advisable to use it as it may create many issues if we miss any logic.

We can label a part of code using colon “:”and use goto keyword to send the execution to this part of code.

We can use return keyword to return control from Main() function which will terminate the execution of the program.

No comments:

Post a Comment