Comparison of programming languages (basic instructions)

This is an old revision of this page, as edited by 208.138.31.76 (talk) at 19:33, 12 December 2007 (Standard Input and Standard Output). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Data types

How to declare variable x as the following types:

Integer Unsigned integer Float String
C int x; unsigned int x; float x; or double x; char x[]; or char *x;
C++ (STL) int x; unsigned int x; float x; or double x; string x;
Java int x; float x; or double x; String x;
Pascal var x: integer; var x: word; var x: real; var x: string;
Visual Basic Dim x As Integer Dim x As Single or Dim x As Double Dim x As String
Visual Basic .NET as above Dim x As UInteger as above as above
Python x = value x = value x = value x = "string"
S-Lang x = value x = value x = value x = "string"

Logical cycles

if else if while do while for i = 1 to N select case
C if (condition) { instructions } else { instructions } if (condition) { instructions } else if (condition) { instructions } while (condition) { instructions } do { instructions } while (condition) for (i = 0; i<N; i++) { instructions } switch (variable) { case case1: instructions break; ... default: instructions;}
C++ (STL) as above as above as above as above as above as above
Java as above as above as above as above as above as above
Pascal if condition then begin \n instructions \n end; if condition then begin \n instructions \n end\n else begin \n instructions \n end; repeat \n instructions \n until condition; for i := 1 to N do \n begin \n instructions \n end; case variable of \n meaning: instructions; \n default: instructions \n ... end;
Visual Basic If condition Then \n instructions \n Else instructions \n End If If condition Then \n instructions \n ElseIf condition Then \n instructions \n Else instructions \n End If Do While condition \n instructions \n Loop Do \n instructions \n Loop While condition For i = 1 To N Step 1 \n instructions \n Next i Select Case variable \n Case case1 \n instructions \n ... Case Else \n instructions \n End select
Python if condition : \n \t instructions \n else: \n \tinstructions \n for i in range(1, N): \n \t instructions \n
S-Lang if (condition) { instructions } else { instructions } if (condition) { instructions } else if (condition) { instructions } while (condition) { instructions } for (i = 0; i<N; i++) { instructions } switch (variable) { case case1: instructions } { case case2: instructions } ...

Function declaration

basic value-returning function
C void foo(parameters) { } type foo(parameters) { }
C++ (STL) as above as above
Java public or private + as above public or private +as above
Pascal procedure foo(...); function foo(...):type;
Visual Basic Sub foo(parameters) Function foo(parameters) As type
Python def foo(parameters): \n \t instructions \n add return value at the end.
S-Lang define foo (parameters ; qualifiers) { } define foo (parameters ; qualifiers) { ... return value; }

Data conversions

string to integer string to float integer to string float to string
C integer = atoi(string); float = atof(string); sprintf(string, "%i", integer); sprintf(string, "%f", float);
C++
Java integer = Integer.parseInt(string); float = Float.parseFloat(string); or double = Double.parseDouble(string); string = integer; string = float;
Pascal integer := StrToInt(string); float := StrToFloat(string); string := IntToStr(integer); string := FloatToStr(float);
Visual Basic integer = CInt(string) float = CSng(string) or double = CDbl(string) string = CStr(integer) string = CStr(float)
Python int('5') float('5.0') str(5) str(5.0)
S-Lang integer = atoi(string); float = atof(string); string = string(integer); string = string(float);

Standard Input and Standard Output

stdin stdout stderr
C scanf( ... );
fscanf(stdin, ... );
printf( ... );
fprintf(stdout, ... );
fprintf(stderr, ... );
C++ cin >> x; cout << x; cerr << x;
Java x = System.in.read();
x = System.in.readln();
System.out.print(x);
System.out.println(x);
System.err.print(x);
System.err.println(x);
Pascal read( ... );
readln( ... );
write( ... );
writeln( ... );
var IOResult;
Visual Basic Input x Print x ?
Visual Basic .NET x = Console.ReadLine() Console.WriteLine(x) Debug.WriteLine(x)
Python
  • For numbers: x = input()
  • For strings: x = raw_input()
print( ... )
sys.stdout.write( string )
sys.stderr.write( string )
S-Lang fgets (&x, stdin) fputs (x, stdout) fputs (x, stderr)

Reading arguments

Argument values Argument counts
C char **argv int argc
C++ as above as above
Java string = args[x]; integer = args.length;
Pascal string := ParamStr(integer); integer := ParamCount;
Visual Basic
Python sys.argv len(sys.argv)
S-Lang __argv __argc