Illustrates the use of the arithmetic operators

image_pdfimage_print

   
 
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example3_2.csc illustrates the use of
  the arithmetic operators
*/

public class Example3_2
{

  public static void Main()
  {

    // integers and arithmetic operators
    System.Console.WriteLine("10 / 3 = " + 10 / 3);
    System.Console.WriteLine("10 % 3 = " + 10 % 3);
    int intValue1 = 10;
    int intValue2 = 3;
    System.Console.WriteLine("intValue1 / intValue2 = " +
      intValue1 / intValue2);
    System.Console.WriteLine("intValue1 % intValue2 = " +
      intValue1 % intValue2);

    // floats and arithmetic operators
    System.Console.WriteLine("10f / 3f = " + 10f / 3f);
    float floatValue1 = 10f;
    float floatValue2 = 3f;
    System.Console.WriteLine("floatValue1 / floatValue2 = " +
      floatValue1 / floatValue2);

    // doubles and arithmetic operators
    System.Console.WriteLine("10d / 3d = " + 10d / 3d);
    System.Console.WriteLine("10.0 / 3.0 = " + 10.0 / 3.0);
    double doubleValue1 = 10;
    double doubleValue2 = 3;
    System.Console.WriteLine("doubleValue1 / doubleValue2 = " +
      doubleValue1 / doubleValue2);

    // decimals and arithmetic operators
    System.Console.WriteLine("10m / 3m = " + 10m / 3m);
    decimal decimalValue1 = 10;
    decimal decimalValue2 = 3;
    System.Console.WriteLine("decimalValue1 / decimalValue2 = " +
      decimalValue1 / decimalValue2);

    // multiple arithmetic operators
    System.Console.WriteLine("3 * 4 / 2 = " + 3 * 4 / 2);

  }

}

           
         
     


Illustrates the use of the ternary operator

image_pdfimage_print

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
Example3_5.cs illustrates the use of
the ternary operator
*/

public class Example3_5
{

public static void Main()
{

int result;

result = 10 > 1 ? 20 : 10;
System.Console.WriteLine(“result = ” + result);

result = 10 < 1 ? 20 : 10; System.Console.WriteLine("result = " + result); } } [/csharp]

Ternary operator

image_pdfimage_print

   
 
/*
Learning C# 
by Jesse Liberty

Publisher: O&#039;Reilly 
ISBN: 0596003765
*/

 using System;

 public class ThreeInputValues
 {
     static void Main()
     {
         int valueOne = 10;
         int valueTwo = 20;

         int maxValue = valueOne > valueTwo ?  valueOne : valueTwo;

         Console.WriteLine("ValueOne: {0}, valueTwo: {1}, maxValue: {2}",
             valueOne, valueTwo, maxValue);

     }
 }
           
         
     


Illustrates the use of the Boolean logical operators

image_pdfimage_print

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example3_4.cs illustrates the use of
the Boolean logical operators
*/

public class Example3_4
{

public static void Main()
{

bool result;

// use of the Boolean logical AND operator
result = (1 == 1) && (2 > 1);
System.Console.WriteLine(“(1 == 1) && (2 > 1) is ” + result);
result = (1 == 1) && (2 < 1); System.Console.WriteLine("(1 == 1) && (2 < 1) is " + result); // use of the Boolean logical OR operator result = (1 == 1) || (1 == 0); System.Console.WriteLine("(1 == 1) || (1 == 0) is " + result); result = (1 == 0) || (1 == 0); System.Console.WriteLine("(1 == 0) || (1 == 0) is " + result); // use of the Boolean logical NOT operator result = !(1 == 0); System.Console.WriteLine("!(1 == 0) is " + result); result = !(1 == 1); System.Console.WriteLine("!(1 == 1) is " + result); } } [/csharp]

Illustrates the use of the comparison operators

image_pdfimage_print

   
 
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example3_3.cs illustrates the use of
  the comparison operators
*/

public class Example3_3
{

  public static void Main()
  {

    bool result;

    // false expressions
    result = 10 == 1;
    System.Console.WriteLine("10 == 1 is " + result);
    result = 10 < 1;
    System.Console.WriteLine("10 < 1 is " + result);
    result = 10 <= 1;
    System.Console.WriteLine("10 <= 1 is " + result);

    // true expressions
    result = 10 != 1;
    System.Console.WriteLine("10 != 1 is " + result);
    result = 10 > 1;
    System.Console.WriteLine("10 > 1 is " + result);
    result = 10 >= 1;
    System.Console.WriteLine("10 >= 1 is " + result);
    int intValue1 = 10;
    int intValue2 = 1;
    result = intValue1 != intValue2;
    System.Console.WriteLine("intValue1 != intValue2 is " + result);

  }

}

           
         
     


Self decrease

image_pdfimage_print

   
 
/*
Learning C# 
by Jesse Liberty

Publisher: O&#039;Reilly 
ISBN: 0596003765
*/

 using System;
 public class SelfMiValues
 {
     static void Main()
     {
         int original = 10;
         int result;

         // increment then assign
         result = --original;
         Console.WriteLine("After prefix: {0}, {1}", original,
             result);

         // assign then increment
         result = original--;
         Console.WriteLine("After postfix: {0}, {1}",
             original, result);
     }
 }
           
         
     


Self increment

image_pdfimage_print

   
 
/*
Learning C# 
by Jesse Liberty

Publisher: O&#039;Reilly 
ISBN: 0596003765
*/
 using System;
 public class SelfValues
 {
     static void Main()
     {
         int original = 10;
         int result;

         // increment then assign
         result = ++original;
         Console.WriteLine("After prefix: {0}, {1}", original,
             result);

         // assign then increment
         result = original++;
         Console.WriteLine("After postfix: {0}, {1}",
             original, result);
     }
 }