Monday, 28 April 2014

What are conditional constructs and why do we need it?

What are conditional constructs and why do we need it?
        
        The  ability  to take decisions is fundamental to human beings.  Decision-making can be incorporated into programs as well.  This helps in determining the sequence in which program will execute instructions.
            
             You can control the flow of a program by using conditional constructs.  Conditional constructs allow you to execute selective statements based on the result of the expression being evaluated.  The various conditional constructs that can be included in a c# program are 


  •  The if ....else construct
  •  The switch....case construct
            Now let me explain that in general view.  First we will go to the first conditional construct 
The if....else construct
             Here the if statement in the if...else conditional construct is followed by a logical expression where data is compared and a decision is made on the basis of the result of the comparison.  The following statements depict the syntax for the if....else construct:
              
             if(expression)
{
              statements:
          else
{
            statements;

}   


Consider an example where you have decide if you want to have dinner or you want to go to sleep.  The condition on which you will make a decision will be your hunger.  If you are hungry, you will have dinner ;  else you will go to sleep.

        The following code snippet shows the decision making of the preceding example by using the if...else construct;

    if (hungry==true )
{
console.writeline("Go and have dinner!");
}
    else
{
console.writeline("Go to sleep");
}
  

The folllowing code shows the use of the if...else construct

using System;
class Game
{   static void Main( string[] args)
     { 

int age;
Console.Writeline("Enter your age::");
Age = Convert.ToInt32(Console.Readline());
if(Age<12)
{
     Console.Writeline("Play the game!");

}
else
{
Console.Writeline(" Sorry!  This game is for children below 12 years");
}


The preceding code checks whether the age of the player is less than 12.  The condition in the if statement checks the age of the player and if the condition is true,  the statement in the if block are executed.  If the condition in the if statement is false, the statement in the else block are executed.

The output of the preceding code will be:
Enter Your age:
14
Sorry!  This game is for children below 12 yearrs
                           The Switch......Case Construct

          Another conditional construct available in C# is the switch.....case construct.  It is used when you have to evaluate a variable for multiple values.

          The following code depicts the syntax for the switch.....case construct:

switch(VariableName)
{
     case ConstantExpression_1;
             statements;
     break;
     case ConstantExpression_2;
             statements;
     break;
     case ConstantExpression_3;
             statements;
     break;
     default:
             statements;
     break;
}

  When the switch statement is executed, the variable given in the switch statement in evaluated and compared with each case constant.  If one of the case constants is equal to the value of the variable given in the switch statement, control is passed to the statement following the matched case statement.  A break statement is used to exit the switch statement.  This prevents the execution of the remaining case structures by ending the execution of the switch...case construct.

No comments:

Post a Comment