Programming Concepts Compared in Different Languages with Code Examples

Here is example code for basic programming concepts that cannot be summarized with a keyword. For details, see Keywords Compared in Different Languages.

Code examples are presented for the following tasks:

  • Assignment Statements
  • Comments
  • Conditional Statements
  • Declaring Variables
  • FOR Loops
  • Hiding Base Class Members
  • Initializing Value Types
  • Parameter Passing by Reference
  • Parameter Passing by Value
  • Selection Statements
  • Set an Object Reference to Nothing
  • Structured Exception Handling
  • WHILE Loops

Declaring Variables

Visual Basic

Dim x As Integer
Public x As Integer = 10

Visual J#

int x;
int x = 10;

C++

int x;
int x = 10;

C#

int x;
int x = 10;

JScript

var x : int;
var x : int = 10;
var x = 10;

Visual FoxPro

LOCAL x
X = 10

Comments

Visual Basic

' comment
x = 1   ' comment
Rem comment 

Visual J#

//
/* multiline 
comment */
/**
   Class Documentation
 */

C++

// comment

/* multiline
 comment */

C#

// comment
/* multiline
 comment */

JScript

// comment
/* multiline
 comment */

Visual FoxPro

* full line

USE  && end of line

NOTE multiline ;
   comment

Assignment Statements

Visual Basic

nVal = 7

Visual J#

nVal = 7;

C++

nVal = 7;

C#

nVal = 7;

JScript

nVal = 7;

Visual FoxPro

nVal = 7

–or–

STORE 7 to nVal

Conditional Statements

Visual Basic

If nCnt <= nMax Then
   nTotal += nCnt  ' Same as nTotal = nTotal + nCnt.
   nCnt += 1       ' Same as nCnt = nCnt + 1.
Else
   nTotal += nCnt
   nCnt -= 1       
End If

Visual J#

if (nCnt <= nMax)
{
   nTotal += nCnt;
   nCnt++;
}
else
{
   nTotal +=nCnt;
   nCnt--;
}

C++

if(nCnt < nMax) {
 nTotal += nCnt;
 nCnt++;
 }
else {
   nTotal += nCnt;
   nCnt --;    
 };

C#

if (nCnt <= nMax)
{
   nTotal += nCnt;
   nCnt++;
}
else
{
   nTotal +=nCnt;
   nCnt--;
}

JScript

if(nCnt < nMax) {
   nTotal += nCnt;
   nCnt ++;
}
else {
   nTotal += nCnt;
   nCnt --;
};

Visual FoxPro

IF nCnt < nMax
   nTot = nTot * nCnt
   nCnt = nCnt + 1
ENDIF

Selection Statements

Visual Basic

Select Case n
   Case 0
      MsgBox ("Zero")  
     ' Visual Basic .NET exits the Select at the end of a Case.
   Case 1
      MsgBox ("One")
   Case 2 
      MsgBox ("Two")
   Case Else
      MsgBox ("Default")
End Select

Visual J#

switch(n) {
  case 0:
    System.out.println("Zero\n");
    break;
  case 1:
    System.out.println("One\n");
    break;
  default:
    System.out.println("?\n");
}

C++

switch(n) {
 case 0:
  printf("Zero\n");
  break;
 case 1:
  printf("One\n");
  break;
 case 2:
  printf("Two\n");
  break;
 default:
  printf("?\n");}

C#

switch(n) 
{
   case 0:
      Console.WriteLine("Zero");
      break;
   case 1:
      Console.WriteLine("One");
      break;
   case 2:
      Console.WriteLine("Two");
      break;
   default:
      Console.WriteLine("?");
      break;
}

JScript

switch(int(n)) {
   case 0 :
      print("Zero")
      break
   case 1 :
      print("One")
      break
   case 2 :
      print("Two")
   default :
      print("Default")
}

Visual FoxPro

DO CASE
 CASE n = 0
  ? 'Zero'
 CASE n > 0
  ? 'Pos'
 OTHERWISE
  ? 'Neg'
ENDCASE

FOR Loops

Visual Basic

For n = 1 To 10 
   MsgBox("The number is " & n)
Next

For Each prop In obj
    prop = 42
Next prop

Visual J#

for(n=1; n<11;n++)
 System.out.println("The number is " + n);

C++

for(int n=1; n<11; n++)
 printf("%d\n",n);

C#

for (int i = 1; i <= 10; i++) 
   Console.WriteLine("The number is {0}", i);
foreach(prop current in obj)
{
   current=42;
}

JScript

for (var n = 0; n < 10; n++) {
   print("The number is " + n)
}
for (var prop in obj)
obj[prop] = 42

Visual FoxPro

FOR n = 1 TO 10
 ? n
ENDFOR

Hiding Base Class Members

Visual Basic

Public Class BaseCls
   Public Z As Integer = 100   ' The element to be shadowed
   public Sub Test()
      System.Console.WriteLine("Test in BaseCls")
   End Sub
End Class

Public Class DervCls
   Inherits BaseCls
   Public Shadows Z As String = "*"   ' The shadowing element.
   public Shadows Sub Test()
      System.Console.WriteLine("Test in DervCls")
   End Sub
End Class

Public Class UseClasses
   Dim BObj As BaseCls = New DervCls()   ' DervCls widens to BaseCls. 
   Dim DObj As DervCls = New DervCls()   ' Access through derived class.
   Public Sub ShowZ()
      System.Console.WriteLine("Accessed through base class: " & BObj.Z)
      System.Console.WriteLine("Accessed through derived class: " & DObj.Z)
      BObj.Test()
      DObj.Test()
   End Sub 
End Class

Visual J#

public class BaseCls
{
   public int Z = 100;   // The element to be hidden
   public void Test()
   {
      System.Console.WriteLine("Test in BaseCls");
   }
}

public class DervCls extends BaseCls
{
   public String Z = "*";   // The hiding element
   public void Test()
   {
      System.Console.WriteLine("Test in DervCls");
   }
}

public class UseClasses
{
   BaseCls BObj = new DervCls();   // DervCls widens to BaseCls
   DervCls DObj = new DervCls();   // Access through derived class
   public void ShowZ()
   {
      System.Console.WriteLine("Accessed through base class: " + BObj.Z);
      System.Console.WriteLine("Accessed through derived class: " + DObj.Z);
      BObj.Test();
      DObj.Test();
   }
}

C++

#using <mscorlib.dll>
#include <stdio.h>
public __gc class BaseCls
{
public:
   int Z;   // The element to be hidden
   void Test()
   {
      printf("Test in BaseCls\n");
   }

};

public __gc class DervCls : public BaseCls
{
public:
   char Z;   // The hiding element
   void Test()
   {
      printf("Test in DervCls\n");
   }

};

public __gc class UseClasses
{
public:
   BaseCls * BObj;   // DervCls widens to BaseCls
   DervCls * DObj;   // Access through derived class
   void ShowZ()
   {
      BObj = new DervCls;
      BObj->Z = 100;
      DObj = new DervCls;
      DObj->Z = '*';
      printf("Accessed through base class: %d\n", BObj->Z);
      printf("Accessed through derived class: %c\n", DObj->Z);
      BObj->Test();
      DObj->Test();
   }
};

C#

public class BaseCls
{
   public int Z = 100;   // The element to be hidden
   public void Test()
   {
      System.Console.WriteLine("Test in BaseCls");
   }
}

public class DervCls : BaseCls
{
   public new string Z = "*";   // The hiding element
   public new void Test()
   {
      System.Console.WriteLine("Test in DervCls");
   }
}

public class UseClasses
{
   BaseCls BObj = new DervCls();   // DervCls widens to BaseCls
   DervCls DObj = new DervCls();   // Access through derived class
   public void ShowZ()
   {
      System.Console.WriteLine("Accessed through base class: {0}", BObj.Z);
      System.Console.WriteLine("Accessed through derived class: {0}", DObj.Z);
      BObj.Test();
      DObj.Test();
   }
}

JScript

class BaseCls
{
   function get Z() : int { return 100 }   // The element to be hidden
}

class DervCls extends BaseCls
{
   hide function get Z() : String { return "*" }   // The element to be hidden
}

class UseClasses
{
   var BObj : BaseCls = new DervCls();   // DervCls widens to BaseCls
   var DObj : DervCls = new DervCls();   // Access through derived class

   function ShowZ()
   {
      print("Accessed through base class: " + BObj.Z);
      print("Accessed through derived class: " + DObj.Z);
   }
}

new UseClasses().ShowZ()

Visual FoxPro

oBase = newobject ("BaseCls")
oDerived = newobject ("DerivedCls")

?oBase.Z
?oDerived.Z

DEFINE CLASS BaseCls AS SESSION
   Z = 100      && The element to be hidden
ENDDEFINE

DEFINE CLASS DerivedCls AS BaseCls
   Z = "*"      && The hiding element
ENDDEFINE

WHILE Loops

Visual Basic

While n < 100 ' Test at start of loop.
   n += 1     ' Same as n = n + 1.
End While '

Visual J#

while (n < 100)
 n++;

C++

while(int n < 100)
   n++;

C#

while (n < 100)
   n++;

JScript

while (n < 100)
   n++;

Visual FoxPro

DO WHILE n < 100
 n = n + n
ENDDO

Parameter Passing by Value

Visual Basic

Public Sub ABC(ByVal y As Long) ' The argument Y is passed by value.
' If ABC changes y, the changes do not affect x.
End Sub
   
ABC(x) ' Call the procedure.
' You can force parameters to be passed by value, regardless of how 
' they are declared, by enclosing the parameters in extra parentheses.
ABC((x))

Visual J#

Objects are always passed by reference, and primitive data types are always passed by value.

C++

MyMethod(i,j);

C#

/* Note that there is no way to pass reference types (objects) strictly by value. You can choose to either pass the reference (essentially a pointer), or a reference to the reference (a pointer to a pointer).*/
// The method:
void ABC(int x)
{
   ...
}
// Calling the method:
ABC(i);

JScript

ABC(i)

Visual FoxPro

=ABC(X)

Parameter Passing by Reference

Visual Basic

Public Sub ABC(ByRef y As Long) 
' The parameter y is declared by by referece:
' If ABC changes y, the changes are made to the value of x.
End Sub

ABC(x) ' Call the procedure.

Visual J#

Objects are always passed by reference, and primitive data types are always passed by value.

C++

// Prototype of ABC that takes a pointer to integer.
int ABC(long *py);
ABC(&VAR);
// Prototype of ABC that takes a reference to integer.
int ABC(long &y);
ABC(VAR);

C#

/* Note that there is no way to pass reference types (objects) strictly by value. You can choose to either pass the reference (essentially a pointer), or a reference to the reference (a pointer to a pointer).*/
// Note also that unsafe C# methods can take pointers just like C++ methods. For details, see unsafe.
// The method:
void ABC(ref int x)
{
   ...
}
// Calling the method:
ABC(ref i);

JScript

/* Reference parameters are supported for external objects, but not internal JScript functions. Use '&' to call by reference */
myObject.ByRefMethod(&x);  

Visual FoxPro

=ABC(@X)

–or–

DO ABC WITH X

Structured Exception Handling

Visual Basic

Try
   If x = 0 Then
      Throw New Exception("x equals zero")
   Else
      Throw New Exception("x does not equal zero")
   End If
Catch err As System.Exception
   MsgBox("Error: " & Err.Description)
Finally
   MsgBox("Executing finally block.")
End Try

Visual J#

try{
   if (x == 0)
      throw new Exception ("x equals zero");
   else
      throw new Exception ("x does not equal zero");
}
catch (Exception err){
   if (err.getMessage() == "x equals zero")
      System.out.println(err.getMessage());
      // Handle error here
}
finally
{
   System.out.println("Executing finally block");
}

C++

      __try{
      if (x == 0)
         throw new Exception ("x equals zero");
      else
         throw new Exception ("x does not equal zero");
         }
      __catch(Exception e)
{
            Console.WriteLine("Caught Exception"); 
      }
      __finally
{
         Console.WriteLine("Executing finally block");
      }

C#

// try-catch-finally
try
{
   if (x == 0)
      throw new System.Exception ("x equals zero");
   else
      throw new System.Exception ("x does not equal zero");
}
catch (System.Exception err)
{
   System.Console.WriteLine(err.Message);
}
finally
{
   System.Console.WriteLine("executing finally block");
}

JScript

try {
   if (x == 0)
      throw "x equals zero"
   else
      throw "x does not equal zero"
}   
catch(e) {                          
   print("Error description: " + e)
}
finally {
   print("Executing finally block.")
}

Set an Object Reference to Nothing

Visual Basic

o = Nothing

Visual J#

stringVar = null;

C++

C#

o = null;

JScript

o = undefined;

Visual FoxPro

MyObj = null

-or-

Obj.RELEASE

Initializing Value Types

Visual Basic

Dim dt as New System.DateTime(2001, 4, 12, 22, 16, 49, 844)

Visual J#

System.DateTime dt = new System.DateTime(2001, 4, 12, 22, 16, 49, 844);

C++

System::DateTime dt = System::DateTime(2001, 4, 12, 22, 16, 49, 844);

C#

System.DateTime dt = new System.DateTime(2001, 4, 12, 22, 16, 49, 844);

JScript

var dt : System.DateTime = new System.DateTime(2001, 4, 12, 22, 16, 49, 844);

Visual FoxPro

LOCAL ltDateTime AS Datetime 
ltDateTime = DATETIME(2001, 4, 12, 22, 16, 49) 

See Also

Language Equivalents | Keywords Compared in Different Languages | Data Types Compared in Different Languages | Operators Compared in Different Languages | Controls and Programmable Objects Compared in Different Languages and Libraries