Pages

Tuesday 3 March 2015

Integrating C#.NET with Embedded System:chapter-2


DATA TYPES AND VERIABLES

“Variables” are simply storage locations for data. You can place data into them and retrieve their contents as part of a C# expression. The interpretation of the data in a variable is controlled through “Types”.

The C# simple types consist of:

·         Boolean type
·         Numeric types: Integrals, Floating Point, Decimal
·         String type

BOOLEAN TYPES

Boolean types are declared using the keyword “bool”. They have two values: “true” or “false”. In other languages, such as C and C++, boolean conditions can be satisfied where 0 means false and anything else means true. However, in C# the only values that satisfy a boolean condition is true and false
 Example:
         bool content = true; bool  
         noContent=false ;

Numeric types: Integrals, Floating Point, Decimal

Example:int i=35;
                long y=654654;
                float x;
                decimal z;


String type
            Example: string myString=”Hai everyone”;

Special characters that may be used in strings:
  


Arrays             
          Example:   int[] myInts = { 5, 10, 15 };

Control Flow
To be able to control the flow in your program is important in every programming language.
 
The two most important techniques are:
        The if Statement
        The switch Statement 



The if Statement 
The if statement is probably the most used mechanism to control the flow in your application. An if statement allows you to take different paths of logic, depending on a given condition. When the condition evaluates to a Boolean true, a block of code for that true condition will execute. You have the option of a single if statement, multiple else if statements, and an optional else statement. 


Example:
      myTest=false;
 if (myTest==false)
  {
    essageBox.Show("Hello”);
  } 


output:
 For more complex logic we use the if … else statement.

Example:
               bool myTest;
               myTest=true;
               if (myTest == false)
               {
                  MessageBox.Show("Hello1");
                }
      else
               {
                        MessageBox.Show("Hello2");
                } 


Or you can use nested if… else if sentences.

 Example:
         int myTest;
         myTest=2;
         if (myTest == 1)
    {
       MessageBox.Show("Hello1");
     }
    else if (myTest == 2)
     {
         MessageBox.Show("Hello2");
      }
else
     {
        MessageBox.Show("Hello3");
     } 




The switch Statement

Another form of selection statement is the switch statement, which executes a set of logic depending on the value of a given parameter. The types of the values a switch statement operates on can be booleans, enums, integral types, and strings.
 example:

           switch (myTest)
     {
        case 
             1:MessageBox.Show("Hello1");break;
       case
            2:MessageBox.Show("Hello2");break;
       default:MessageBox.Show("Hello”);break;
}