Basic Programming Elements

There are a number of basic programming elements found in most all programming languages. Some of those are compared here.

Comments

There are 3 basic types of comments
C++ Pascal Basic
Rest of line // ' (apostrophy)
Block /* .. */ None Available

Notes on Other Languages:


Data Types

  Bytes Basic C++ Pascal Java
String 0 - 2Gig
0 - 64K
 
Dim x As String
Dim x As String * length
Dim x$
char *x;
char x[256];
string x string x
String const   "test" "test" 'test' "test"
Character const 1 or 2 N/A 'a' ?? 'a'
Byte
0-255 (unsigned)
-128 to +127 (signed)
char is compiler dependent
1 Dim x As Byte char x;
unsigned char x;
signed char x;
byte x;
Boolean - true =-1 false=0 2 Dim x As Boolean bool x; boolean x;
Integer
-32,768 to 32,767 (signed)
0 to 65535 (unsigned)
2 Dim x As Integer
Dim x%
short x;
unsigned short x;
int x; (16-bit comp)
short x;
char x; (unicode)
Long Integer
-2,147,483,648 to
2,147,483,647
4 Dim x As Long
Dim x&
int x; (32-bit comp)
unsigned int x;
long x;
unsigned long x;
int x;
Very Long Integer
-9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
8 N/A N/A long x;
Single
1.401298E-45 to
3.402823E38
4 Dim x As Single
Dim x!
float x; float x;
Double
4.94065645841247E-324 to
1.79769313486232E308
8 Dim x As Double
Dim x#
double x;
long double x;
double x;
Date/Time
Jan 1, 100 to Dec 31, 9999
8 Dim x As Date N/A N/A
Variants 16 or 22 Dim x, y N/A N/A
Arrays   Dim x(5)
Dim x(5, 4) As Integer
Dim x(1 to 10)
Dim x()
int x[5];
int x[5][4];
N/A
N/A
Same as C++

Visual Basic Variants can contain almost any data type

Important Constants

    VisualBasic  3.0          VisualBasic  6.0
         RED                       vbRed 
         KEY_RIGHT                 vbKeyRight
  ESC$ = Chr$(KEY_ESCAPE)   ESC$ = Chr$(vbKeyEscape)
   cr$ = Chr$(KEY_RETURN)    cr$ = Chr$(vbKeyReturn)

   Constant.txt is a part of VB 3.0; it is definitely not in VB 6.0. 
   It appears that I copied Constant.txt to create globcons.bas 
   and then included globcons.bas in my programs. (I can't remember.)
   At any rate, the constant names are now different in VB 6.0 
   (big supprise) :(
In C++
char *x; Allocates a variable which can point to a string -
x = new char[20]; would work.
char x[256]; Allocates a variable which points to a string and the memory to hold the string. The string actually contains all the bytes up to the first null '\0' byte. It is up to the programmer to be sure not to write beyond the end of the allocated space.
int x; This can be various sizes depending on the compiler.

Control

if .. then .. else

           VisualBasic

Single   if a>5 then a=5 : b=7 else b=2
         IIf(a>5, 5, a)    ' IIf(expr, truepart, falsepart)

Block    if a>5 then
            a=5
            b=7
         end if

If condition Then single statement
IIf(expr, truepart, falsepart)

If condition Then
  block
  of
  statements
End If

If i=5 Then
  'do this
ElseIf i = 2 Then
  'do this
Else      
  'statements
End If


               C++

Single   if a>5 then a=5;
         a>5 ? a=5; b=7  : a=1;

Block    if a>5 then
         {
          a=5;
          b=7;
         }

              Delphi

Single   if a>5 then a:=5;
         a>5 ? a:=5; b:=7  : a:=1;

Block    if a>5 then
         begin
          a:=5;
          b:=7;
         end

Control

Loops

           VisualBasic

   Select Case rtfData.SelColor
      Case vbBlack
         cmbFontColor.ListIndex = 0
      Case vbBlue
         cmbFontColor.ListIndex = 1
      Case vbRed
         cmbFontColor.ListIndex = 2
      Case Else
         cmbFontColor.ListIndex = 3
   End Select

             Pascal


             C++ and Java

  for( [init-expr]; [cond-expr]; [loop-expr] )
       statement


  for ( int i = 0; i < 10; i++ )
  {
      code goes here
  }

  // Do loop, always executes at least once
  do
  {    
    // Place code here

  } while ( i <= 10 );

  // While loop never executes if condition is not initially true
  while ( i < 10 )
  {
    // Place code here
  }


  // Switch matches the expression results with a case constant
  // Warning: If break; is omitted, the next line is executed!
  switch ( someExpression ){
    case 1:
        {
        doCase1( );
        break;
        }
    case 2:
        {
        doCase2( );
        break;
        }
    case 3:
        {
        doCase3A( );
        doCase3B( ); // Fall thru to default 
                     // (always comment a case fall thru 
                     //  so that the reader will know that it is not an accident)
        }
    default:
        doDefault( );
  }

  break;
    transfers control out of the innermost enclosing 
    while, do, for, or switch statement. 
    Java supports an optional label to detrmine the target loop.

  continue; 
    passes control to the next iteration of the innermost enclosing 
    while, do, or for statement.
    Java supports an optional label to detrmine the target loop.

String Functions

  Basic C++ Pascal Java
Declare Dim str1 As String
Dim str1 As String * 5
char str1[256] string str1 string str1
or same a C++
Assignment str1 = "test" strcpy(str1, "test");
No error checking
str1 := 'test' str1 = "test";
Compare if str1 = str2
StrComp(string1, string2)
if strcmp(str1, str2) if str1 = str2 if str1==str2
Substring Mid(string, start[, length]) strncpy(str1, "test", 2);
strncpy(str1, str2[3], 2);
cde.substring(1, 2);
Set Case UCase(string)
LCase(string)
Concatenate str1 = str1 & str2 (preferred)
str1 = str1 + str2
Int to String str$(tempInt)
str(tempInt)
cstr(tempInt)
itoa(int1, str1, bytes);
Length len(TempStr) strlen(str1)
Misc Left(string, length)
Right(string, length)
LTrim(string)
RTrim(string)
Trim(string)
Search InStr([start, ]string1, string2[, compare]) strchr(str1, ch1)
strrchr(str1, ch1)
strstr(str1, str2)
strpbrk(str1, str2)
strtok(str1, str2, ??)
Format Format(...) sprintf(...)
Asc(string)
Chr(charcode)
Space(number)
String(number, character)

Functions and Subroutines

Visual Basic (Starting with 4.0)

Named Arguments can be used when calling functions and subroutines. For instance, instead of

 Function testFunc(str1 as String, str2 as String) as String

  testFunc "String for Str1", "String for str2"
You could use
  testFunc str2:="String for str2", str1:="String for Str1" 
This allows the parameters to appear in any order.

If the colon is omitted, str1="test" will compare 2 strings and return a Boolean (true or false).

************

C vs C++

C++ function names are mangled (modified) in order to indicate the number an types of parameters. C function names are exported just as you type them.

In order to export non-mangled function names from C++, precede each function with extern "C".

MS Windows API calls require non-mangled function names.


Author: Robert Clemenzi - clemenzi@cpcug.org
URL: http:// cpcug.org / user / clemenzi / technical / Languages / index.html