Team LiB
Previous Section Next Section

Chapter 2: Expressions and Operators

Expressions

Chapter 1 demonstrated how to use statements to get the computer to do something. The statements were made up of keywords and symbols that end with a semicolon. The next step is to examine expressions.

An expression uses operators and operands to instruct the compiler to perform a computation. Operators act on operands. Operators come in three flavors:

The operands consist of variables, constants, and method calls. The following shows examples of expressions.

Code Example: Expressions
Start example
using System;

namespace Client.Chapter_2___ Expressions_and_Operators
{
      class Expressions
      {
            static void Main(string[] args)
            {
                  int MyInt = 12345;
                  int MyInt2 = 10000;
                  int Sum = 0;
                  long MyLong = MyInt;
                  short MyShort = (short)MyInt;
                  //Bool expression
                  if (MyInt == MyInt2)
                  {
                        //Simple calculation
                        Sum = MyInt + MyInt2;
                  }
                  else
                  {
                        Sum = MyInt - MyInt2;
                  }
            }
      }
}
End example

Team LiB
Previous Section Next Section