We are providing online training of realtime Live project on Asp.Net MVC with Angular and Web API. For more information click here. If you have any query then drop the messase in CONTACT FORM

Saturday, July 4, 2015

Conditional Statement in C#

What is mean by Conditional statement?
A block of code that gets executed basis on a condition is known as conditional Statement. These are of two types:
  1. Conditional Branching
  2. Conditional Looping.
What is mean by Conditional Branching?
These statements allow us to branch our code depending on whether certain condition was met or not. C# has two constructs for branching code
  1. If
  2. Switch
The If Statement allows us to test whether a specific condition is met. The switch statement allows us to compare an expression with a number of different values.

If:
  • Condition is a Boolean(that is, true or false ) expression.
  • If condition is true, then the statement is executed.
  • If condition is false, then the statement is bypassed.
     A. Simple If
     B. If Else
     C. Multiple Ifs

     D. Nested Ifs

The syntax for Simple If:-                     

if(Condition)
{
//Statements
}

The syntax for  If Else:-                           

if(Condition)
{
//Statements
}
else
{
//Statements
}

The syntax for Multiple If Else:-                        

if(Condition)
{
//Statements
}
else if(condition
{
//Statements
}
. . .
. . .
else
{
//Statements
}

Switch:
  • The switch expression must be of an integer type, such as char, byte, short, or int, or of type string. 
  • The default statement sequence is executed if no case constant matches the expression.
  • The default is optional.
  • If the default is not present, no action takes place if all matches fail.
  • When a match is found, the statements associated with that case are executed until the break is encountered.
switch(expression)
{
case constant1:

statement sequence
break:

case constant2:
statement sequence
break;

case constant3:
statement sequence
break;

default:
statement sequence
break;
}

What is mean by Conditional Looping?
C# provides three different loops that allows us to execute a block of code repeatedly until a certain condition is met, those are
  1. For Loop
  2. While Loop
  3. Do While Loop
Every Loop requires three things in common.
  • Initialization: That sets the starting point of the loop.
  • Condition: That sets the ending point of the loop.
  • Iteration: Which takes you to the next level of the cycle either forward or backward direction.
For Loop:
  • You can repeatedly execute a sequence of code by creating a loop.
  • The general form of the for loop for repeating a single statement is
syntax:
for(initialization;condition;iteration)
statement;
The general form for repeating statements is
for(initialization;condition;iteration)
{
statements;
}

Do While Loop:
  • A Do-While loop will always execute at least once.
  • The general form of the Do-While Loop is 
do 
{
statement1;
statement2;
increment/decrement;
}while(condition);

Note:

  •  In case of for/while the execution starts after checking the conditions so if the condition gets satisfied then only the code gets executed. So, the minimum number of execution will be zero.
  • Whereas in the case of Do-While loop after completing the 1 st execution then only, the condition gets verified. So, the minimum number of execution here will be one.
jump Statements:
C# provides a number of statements that allows you to jump immediately toi another line in a program. Those are
  1. goto
  2. break
  3. continue
  4. return
1. goto: It allows you to jump directly to another specified line in the program, indicate by a label which is an identifier followed by a colon.

2. Break: It is used to exit from a case in a switch statement and also used to exit from any conditional loop statement which will switch the control to the statement immediately after the end of the loop.

3. Continue: The jump statement is mostly used in c for loop for skipping a few iterations of a cycle. That is whenever continue executes a control directly transfer to the iteration part without executing any statement present after it.

4. Return:
  • It is a jump statement that can jump out of a method or function in execution.
  • While jumping out it can also carry a value out of the method or function

No comments: