Lecture overview -- Keyboard shortcut: 'u'  Previous page: Declaration and Types -- Keyboard shortcut: 'p'  Next page: Expressions and Operators -- Keyboard shortcut: 'n'  Lecture notes - all slides together  Annotated slide -- Keyboard shortcut: 't'  Textbook -- Keyboard shortcut: 'v'  Alphabetic index  Help page about these notes  Course home    Introduction to C# - slide 28 : 43

Expressions and Operators
Option Strict On
Option Explicit On

Module ExpressionsDemo

    Sub Main()
        Dim i as Integer = 13 \ 6       ' i becomes 2
        Dim r as Integer = 13 Mod 6     ' r becomes 1
        Dim d as Double = 13 / 6        ' d becomes 2,16666666666667
        Dim p as Double = 2 ^ 10        ' p becomes 1024

'       Dim b as Boolean i = 3          ' Illegal - Compiler error
        Dim b as Boolean, c as Boolean  ' b and c are false (default values)
        c = b = true                    ' TRICKY: c becomes false 
    End Sub

End Module
using System;
class ExpressionsDemo{

  public static void Main(){
    int i = 13 / 6;             // i becomes 2
    int r = 13 % 6;             // r becomes 1
    double d = 13.0 / 6;        // d becomes 2.16666666666667
    double p = Math.Pow(2,10);  // p becomes 1024

    bool b = i == 3;            // b becomes false
    bool c = b = true;          // both b and c become true
  }

}
Option Strict On
Option Explicit On

Module ExpressionsDemo

    Sub Main()
        Dim i as Integer = 2, r as Integer = 1

        If i <> 3 Then
          Console.WriteLine("OK")    ' Writes OK
        End If               

        If Not i = 3 Then
          Console.WriteLine("OK")    ' Same as above
        End If  
    End Sub

End Module
using System;
class ExpressionsDemo{

  public static void Main(){
    int i = 2, r = 1;

    if (i != 3) Console.WriteLine("OK");       // Writes OK
    if (!(i == 3)) Console.WriteLine("OK");    // Same as above
  }

}
Option Strict On
Option Explicit On

Module ExpressionsDemo

    Sub Main()
        Dim i as Integer = 2, r as Integer = 1

        If i = 3 AndAlso r = 1  Then   ' Writes OK
          Console.WriteLine("Wrong") 
        Else 
          Console.WriteLine("OK") 
        End If  

        If i = 3 OrElse r = 1  Then   ' Writes OK
          Console.WriteLine("OK") 
        Else 
          Console.WriteLine("Wrong") 
        End If  
    End Sub
End Module
using System;
class ExpressionsDemo{

  public static void Main(){
    int i = 2, r = 1;

    if (i == 3 && r == 1)
      Console.WriteLine("Wrong");
    else
      Console.WriteLine("OK");

    if (i == 3 || r == 1)
      Console.WriteLine("OK");
    else
      Console.WriteLine("Wrong");
  }
}