Friday, 2 February 2018

007 C# Decision Making

Decision Making
Decision making comes with if else and switch statement.

if:
If statement, is a statement, a particular condition is checked either true or false. 
We can do any statement execution in either true or false conditions. If the statement is true , 
we can execute some statement, if false we can also execute any statement.The Decision 
making of if statement, not only over a particular variable or calculation, +,-, * and operator 
like ||, && can be applied during the checking. System define function like 
Sum,Add from System. Mathematics can be appear during the checking.
User defined function also can be applied during the checking of if condition. When we are 
checking ,the condition is true or false, the statement are not check partially , as a whole it 
should be checked either true or false. Here is the example  of if statement.

Syntax:

if (boolean check)
{
   /* execute statement
}



 
int age = 20;
if (age > 18)
         Console.WriteLine("Hey! You are eligible for credit card!");

if else statement

If else also a branching, to have both statement .In if condition , there should be some 
statement , in if part and some statement else part. If else condition, can have
multiple branches. In if else, we can write else if to check further condition and do 
branching.If else statement can be nested, that  means write one if else statement under 
another If else statement. Below is the example of nested if else.

Syntax:

if(boolean check)
{
   /* execute statement
}
else
{
  /* execute statement
}

Example 1

int age = 20;
int height = 20;
if ((age > 10) || (height < 6))
          Console.WriteLine("Hey! You are eligible for Vollyball!");
else
          Console.WriteLine("Better Next Tiem!");

Example 2
int age = 20;
int height = 20;
if ((age <= 10) && (height >= 4))
         Console.WriteLine("You are eligble for participate in sports!");
else
         Console.WriteLine("Hey! Sorry Next Time!");


Nested if statements


Syntax:

 if(boolean check)
{
   /* execute statement
}
else if( next boolean check)
{
   /* execute statement
else 
{
   /* execute statement
}

 Example 1
 
if (age > 18)
        Console.WriteLine("Hey! Your age suitable for Participate!");
else
if (height < 5)
         Console.WriteLine("Hey! Your height not suitable for Participation !");
else
        Console.WriteLine("Yes ! Both you age and heigh permit for Participation !");

Switch Statement

The switch statement is multiple branching statement, depending upon the condition , the 
control is transferred to the many possible points. For example I have written i=5. 
The switch statement content,i=0, one condition,i=1, another condition, i=2, another 
condition and so on. When we write switch statement, the control will go directly to the i=5. 
The switch statement consists of break statement, break statement works when the 
condition satisfied. Default is an optional part of switch statement, which says that,
when no condition satisfied, the control will go to default. Switch statement  can be nested,
that means a switch statement can be written to another switch statement.The innermost 
will break first and the immediate parent statement will be break next . Here is the 
example of switch statement and nested switch statement. 



Example 1 
int age = 4;
switch (age)
{
         case 0:
                     Console.WriteLine("Your age is less than zero");
                     break;
          case 1:
                Console.WriteLine("Your age is one");
                     break;
         case 2:
                     Console.WriteLine("Your age is two");
                    break;
         case 3:
                   Console.WriteLine("Your age is three");
                  break;
         case 4:
                    Console.WriteLine("Your age is four");
                    break;
}

Example 2
int age = 1000;
switch (age)
{
           case 0:
               Console.WriteLine("Your age is less than zero");
                          break;
            case 1:
                    Console.WriteLine("Your age is one");
                          break;
           case 2:
                          Console.WriteLine("Your age is two");
                          break;
           case 3:
                          Console.WriteLine("Your age is three");
                           break;
           case 4:
                         Console.WriteLine("Your age is four");
                          break;
         default:
                      Console.WriteLine("I'm sorry, I don't understand that!");
                      break;
}

Nested Switch Statements
Example 1 
char i = 'l';
switch (i)
{
           case 'i':
           case 'j':
                      switch (i)
                     {
                     case 'k':
                     case 'l':
                     return true;
                      }
                   break;
}



The ? : Operator

Ternary operator , can be applied like if else. Here a condition is written fast, question mark 
is put, condition written, the first expression is written, is the second expression is written.
It works as a simple if else statement in a smart way. Here is the example of ternary operator. 
The ternary operator can be nested type also, another Ternary operator can be written within 
a ternary operator. The whole act as a simple statement of if else and return the results. Here is the 
example of nested ternary operator. This example so, how a and b initialize and check 
which is greater.

  condition ? first_expression : second_expression; 


// if-else construction.
if (i > 0)
l = "true";
else
l = "false"

Example 1  
// ?: conditional operator.
l = (i > 0) ? "true" : "true";
Example 2
       
int a = 2;
int b = 5;
string r = a > b ? "a is greater than b" : b > a ? "b is greater than a" : "not sure";

Goto : Go to statement, is a piece of program which moves the control of program to a 
particular part of the program.Go to statement, followed by level name tells the program 
to go the particular level statement. Goto can be applied 
  1. loop 

  2. switch statement 

  3. multi level loop multi level switch statement 

  4. program statement

Here are some example of go to statement. Example shows that. 
How control moves to another program level. 


protected void Page_Load(object sender, EventArgs e)
{
     int age = 21;
       if (age < 18)
        {
       Console.Write("You are not agibible for credit card");
        }
       else if (age >18)
    {
        goto credit_card_process;
      }
      else if (age == 18)
      {
    Console.Write("we are checking your eligiblity");
     }

     credit_card_process: 
       card_process();
}

public void card_process()
{
//statement ommitted
}


This example shows that how a for loop can be break by go to statement. When a particular 
condition is reached, the go to statement moves the control of program form a for loop to a 
particular level. 


protected void Page_Load(object sender, EventArgs e)
  {
     for (int i = 0; i < 50; i++)
       {
           if (i == 25)
          {
           goto final;
         }
}


final:
Console.WriteLine("you number 25 has found , no need to furthher loop");
}




Below is example of switch statement. When a particular case match , go to statement fires to
move controller from another.


switch (i)
{
            case 1:
                stardrad="Student of Class One";
                break;
            case 2:
                stardrad="Student of Class two";
                break;
           case 3:
                stardrad="Student of Class tree";
              break;
          case 4:
               stardrad="Student of Class four";
              break;
         case 10:
        goto case 1;
}




No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...