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;
}




Thursday, 1 February 2018

006 C# Loop

C # tutorial 006 Loop 
Loop is a part of a program to do anything continuously and repeatedly until a condition satisfied. Sometimes we do some work, until we reach the goal, sometimes we repeatedly do something to reach any conclusion. For example, to find , how many 3 are there in 15. We can go adding 3 until we reach 15. The first  3 + 3 will give us 6 , the Second time 6 + 3 will give us 9, condition is to reach 15. Then we go adding 3 until you reach 15. This is the concept of loops. Continuously doing the same thing until the condition is reached. 

There are mainly three kind of loop
  •   While loop 
  •   For loop 
  •   Do while loop 
Another kind of loop, that is nested loop. Nested loop can be any loop , do while, while, for.

 1)While loop 

It is a control structure loop where entry is controlled once. In while loop, the condition of exit the loop comes under while statement. Until the condition is false  the loop will execute repeatedly. And under this loop, if there is any program statement , that will execute repeatedly.






inti = 1; 

Example 1
while (true)
{
     //required code
}
 
Example 2

while (i < 4)
{
       Console.WriteLine(i.ToString());
}
//Output:1,2,3

2)For loop

For loop also entry control loop which will execute repeatedly. It's action is predefined number of times of repeatation. The syntax of for loop as below. Fast a variable is defined and initialise, then

the exist condition  is written, then the variable which was initialised, step is written incremental / decremental to satisfy the exit condition. One major difference for for loop other than , while and do while loop,  that variable can be defined in the loop statement directly. Here is the example of follow which show how a variable is defined within the loop.


Syntax

for(initialization; condition; step)

Example 


for (int i = 1; i <5 font="" i="i+1)">
{
      Console.WriteLine(i.ToString());
}
//Output :1,2,3,4
 

3)Do while loop

Do while loop also loop which to repeat work until the condition satisfied. It is an exit control loop. Depending upon the condition, if satisfied, the control send back to the loop again. In this kind of loop , the condition is checked finally and decision is taken whether the loop will continue or exit the loop.

Syntax 


do
{
    //statement
} while (C
ondition is true)
 
    
int i = 1;
do
{
         Console.WriteLine(i.ToString());
} while (i <= 10);


//Output :1,2,3..10

Here is the difference between loops


while

for

do while
Entry control Entry control Exit control
Not predefined reparation Predefined reparation Not predefined reparation


Infinite Loop


An infinite loop is a loop which is executed endlessly, no termination condition satisfy to terminate the loop or the loop was instructed continuous repeat. Infinite loop maybe while loop, do loop, for loop. In this kind of loop, if we do not write termination condition properly, the loop will execute endlessly. Here are the example of infinite loop.

Example 1

while (true)
{
}

Example 2
for (; ; )
{
}

Example 3
do
{
} while (true);



Break and Continue
 
Break is a statement to break a loop which is running. Break statement break the existing loop and return that control to the program. Generally break is written when are in a particular situation the loop need to be stop. Continue is a statement to continue the loop to next Iteration . When the continue statement is executed the control of the loop the to next Iteration . In the below example, continue statement execute, it goes to the Iteration work equal to 26.

for (int i = 1; i < 100; i++)
{
if (i>50)
        break;//
if (i==25)
        continue;
         Console.WriteLine(i.ToString());
}


Nested loop
Nested loop is loop which comes under any loop. Can be a for loop under a while loop, while loop under a do loop. The below is example of nested loop. The nesting of loops level is not constant, there may be multiple level of nesting depending upon the situation, . Net I have no control on restriction over the level of nesting. Break and continue statement can be implemented each level of nesting.

Example 1 
for (int i = 1; i < 100; i++)
{
         for (int l = 1; l < 10; l++)
         {
           //statment
         }
}

Example 2 
int[,] myArray = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

for (int i = 0; i < 4; i++)
{
                   for (int l = 1; l < 2; l++)
                    {
                          //statment
                            var val = myArray[i, l];
                           Response.Write(val+"");
                     }
}

//Output:
2
4
6
8


The below example is a sample of nested loops. And multidimensional array is initialized with data 
and nested loop is created to access this data. This a nested loop can be created any level.
 Generally,to access long data set , array nested loop is used. Below is a example of nested loop 
with break statement.

Example


int[,] myArray = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
for (int i = 0; i < 4; i++)
  {
          for (int l = 1; l < 2; l++)
           {
               //statment
                var val = myArray[i, l];
                if (val==6)
                 break;//
           }
}


Wednesday, 31 January 2018

005 C# Structure & Enum

Structure
Structure is a user defined type which is lightweight than a class. Structure instance 
cannot be created. Here are some features of a structure
  • Structured contain member , like fields and methods.
  • An instance of structure cannot be created. 
  • Structure is value type 
  • Interface can be implemented 
  • Structure does not support inheritance, private, protected, public cannot be defined.
  • C# use the keyword struct , to define a structure. 
  • Structure allow nested type. 
During structures initialization, when a new keyword is called , an appropriate structure constructor 
is called to initialize the structure. We do not need to declare constructor separately.  
Here is the example of structure.  
struct Student
{
       private int m_Age;
       private string m_Name;
      public int Age
      {
      get
       {
        return m_Age;
        }
       set
      {
      m_Age = value;
        }
      }
     public string Name
    {
    get
   {
    return m_Name;
   }
   set
   {
    m_Name = value;
   }
    }

}

Student obj = new Student();
obj.Age = 12;
obj.Name = "John";


Console.WriteLine("Age :" + obj.Age);
Console.WriteLine("Name :" + obj.Name);

Here, the structure contain fields and properties. Age,Name can be assign to the structure and get 
the value of the name and age when required. Structure is value type and no instance can be created, 
it is lightweight compare two class. 

Here is a difference between class and structure 

  • Class is reference type but the structured is value type.
  • Class is created on the heap, structure is created on the stack. 
  • Long heavy weight object, persist for long time, required more instance, then class is used. 
Small object, lightweight, little less data passes for small time, then structure is used. 

As we have discussed earlier, we can implement interface on a structure. Here is a example. 
An interface is defined, School. Structure is defined, named as student. The example shows that, 
how the GetAttenDance method is implemented on the structure.

interface School
{
int GetAttenDance();
}

struct Student : School
{
          public int GetAttenDance()
         {
          //implementation ommitted
         return 50;
          }
}

Student obj = new Student();
int att = obj.GetAttenDance();
Console.WriteLine("Attandace :" + att.ToString());

Enum : It is a value type, data type. The full form is enumeration. This content a set all values 
which are constant. Each value are associated with a numeric instant. If we do not assign any value, 
the default first value is 0, then it is increased by 1 for each value. It is value type and created 
on the stack. It comes under the namespace system.enum The default value type of enum is integer, 
using the cast operator, we can cast the value to long, you int. 

Here is the example of a enum type. An enum is declared named days.

enum Days { SunDay = 1, MonDay, TueDay, WedDay, TruDay, FriDay };
int i=(int)Days.TueDay;
When it is called The returns the days number.





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

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