Comparison of programming languages (syntax)
This comparison of programming languages compares the features of language syntax for over 50 computer programming languages.
Expressions
Programming language expressions can be broadly classified into four syntax structures:;prefix notation
- Lisp )
- Fortran *
- Forth 2 3 + 4 5 ** *
- TUTOR $$ note implicit multiply operator
Statements
- statement separators;
- statement terminators; and
- line continuation
"Line continuation" is a convention in line-oriented languages where the newline character could potentially be misinterpreted as a statement terminator. In such languages, it allows a single statement to span more than just one line.
Language | Statement separator-terminator | Secondary separator |
ABAP | period separated | - |
Ada | semicolon terminated | - |
ALGOL | semicolon separated | - |
ALGOL 68 | semicolon and comma separated | - |
APL | newline terminated | ⋄ separated |
AppleScript | newline terminated | - |
AutoHotkey | newline terminated | - |
BASIC | newline terminated | colon separated |
Boo | newline terminated | - |
C | semicolon terminates statements | comma separates expressions |
C++ | semicolon terminates statements | comma separates expressions |
C# | semicolon terminated | - |
COBOL | whitespace separated, sometimes period separated, optionally separated with commas and semi-colons. | |
Cobra | newline terminated | - |
CoffeeScript | newline terminated | - |
CSS | semicolon separated | - |
D | semicolon terminated | - |
Eiffel | newline terminated | semicolon |
Erlang | colon separated, period terminated | - |
F# | newline terminated | semicolon |
Fortran | newline terminated | semicolon |
Forth | semicolons terminate word definitions | - |
GFA BASIC | newline terminated | - |
Go | semicolon separated | - |
Haskell | newline separated | - |
Haskell | semicolon separated | - |
Java | semicolon terminated | - |
JavaScript | semicolon separated | - |
Kotlin | semicolon separated | - |
Lua | whitespace separated | - |
Mathematica | semicolon separated | - |
MATLAB | newline terminated | semicolon or comma |
Object Pascal | semicolon separated | - |
Objective-C | semicolon terminated | - |
OCaml | semicolon separated | - |
Pascal | semicolon separated | - |
Perl | semicolon separated | - |
PHP | semicolon terminated | - |
Pick Basic | newline terminated | semicolon separated |
PowerShell | newline terminated | semicolon separated |
Prolog | comma separated, semicolon separated, period terminated | - |
Python | newline terminated | semicolon |
Raku | semicolon separated | - |
Red | whitespace separated | - |
Ruby | newline terminated | semicolon |
Rust | semicolon terminates statements | comma separates expressions |
Scala | newline terminated | semicolon |
Seed7 | semicolon separated | - |
Simula | semicolon separated | - |
S-Lang | semicolon separated | - |
Smalltalk | period separated | - |
Standard ML | semicolon separated | - |
Swift | semicolon separated | - |
Visual Basic | newline terminated | colon separated |
Visual Basic.NET | newline terminated | colon separated |
Wolfram Language | semicolon separated | - |
Xojo | newline terminated | - |
Language | Statement separator-terminator | Secondary separator |
Line continuation
Line continuation is generally done as part of lexical analysis: a newline normally results in a token being added to the token stream, unless line continuation is detected.;Whitespace – Languages that do not need continuations
- Ada – Lines terminate with semicolon
- C# – Lines terminate with semicolon
- JavaScript – Lines terminate with semicolon
- Lua
- OCaml
;Backslash as last character of line
- bash and other Unix shells
- C and C++ preprocessor
- Mathematica and Wolfram Language
- Python
- Ruby
- JavaScript – only within single- or double-quoted strings
- PowerShell
- SQL*Plus
- AutoIt
- Cobra
- Visual Basic
- Xojo
- MATLAB: The ellipsis token need not be the last characters on the line, but any following it will be ignored.
- Ruby
- Batch file: starting a parenthetical block can allow line continuation
- Ruby: left parenthesis, left square bracket, or left curly bracket
- Ruby
- AutoHotkey: Any expression operators except ++ and --, as well as a comma or a period
;Some form of inline comment serves as line continuation
- Turbo Assembler:
\
- m4:
dnl
- TeX:
%
- Fortran 77: A non-comment line is a continuation of the previous non-comment line if any non-space character appears in column 6. Comment lines cannot be continued.
- COBOL: String constants may be continued by not ending the original string in a PICTURE clause with
'
, then inserting a-
in column 7 - TUTOR: Lines starting with a tab continue the previous command.
- C and C++ preprocessor: The string is ended normally and continues by starting with a quote on the next line.
Libraries
;File import
- ASP:
#include file="filename"
- AutoHotkey, AutoIt, C, C++:
#include "filename"
,#include <filename>
- COBOL:
COPY filename.
- Falcon:
load "filename"
- Fortran:
include 'filename'
- Lua:
require
- Mathematica and Wolfram Language:
Import
- MATLAB:
addpath
- Objective-C:
#import "filename"
,#import <filename>
- Perl:
require "filename";
- PHP:
include "filename";
,require "filename";
- Prolog:
:-include.
- Pick Basic:
include program
,#include program
- R:
source
- Red:
load %filename
- Rust:
include!;
- Ada:
with package
- C, C++:
#include filename
- Cobra:
use Package.Name
- D:
import package.module;
,import altname = package.module;
- Falcon:
load module
,load module.submodule
- Fortran 90+:
use module
,use module, only : identifier
- Go:
import altname "package/name"
- Haskell:
import Module
,import qualified Module as M
- Java, MATLAB, kotlin:
import package.*
- JavaScript:
import altname from "modname";
,import "modname";
- Lua:
require
- Mathematica and Wolfram Language:
<<name
- Oberon:
IMPORT module
- Objective-C:
@import module;
- Pascal:
uses unit
- Perl:
use Module;
,use Module qw;
- Prolog:
:-use_module.
- Python:
import module
,from module import *
- Rust:
mod modname;
,# mod altname;
,extern crate libname;
,extern crate libname as altname;
- R:
library
- Scala:
import package._
,import package
- Swift:
import module
- Falcon:
import class
- Java, MATLAB, kotlin:
import package.class
- JavaScript:
import class from "modname";
,import from "modname";
,import from "modname";
- PHP:
use Namespace\ClassName;
,use Namespace\ClassName as AliasName;
- Python:
from module import class
- Scala:
import package.class
,import package.
,import package._
- D:
import package.module : symbol;
,import package.module : altsymbolname = symbol;
- Haskell:
import Module
- JavaScript:
import function from "modname";
,import from "modname";
,import from "modname";
- MATLAB:
import package.function
- Perl:
use Module ;
- PHP:
use function Namespace\function_name;
,use Namespace\function_name as function_alias_name;
- Python:
from module import function
- Rust:
use module::submodule::symbol;
,use module::submodule::;
,use module::submodule::symbol as altname;
- Scala:
import package.class.function
,import package.class.
- PHP:
use const Namespace\CONST_NAME;
;Syntactic convenience
- Java:
import package.*
,import package.class
- OCaml:
open module
- Go:
import altname "package/name"
- JavaScript:
import altname from "modname";
- Python:
import module
Blocks
;Braces
:- Curly bracket programming languages: C, C++, Objective-C, Go, Java, JavaScript/ECMAScript, C#, D, Perl, PHP, Rust, Scala, S-Lang, Swift, Windows PowerShell, Haskell, AutoHotkey
- OCaml, Prolog, Standard ML
- Smalltalk
begin
... end
:- Ada, ALGOL, Pascal, Ruby, OCaml, SCL, Simula, Erlang.
do
... end
:;
do
... done
:- Bash, Visual Basic, Fortran, TUTOR, Visual Prolog
do
... end
- Lua, Ruby, Seed7
end
:- Ruby, OCaml, MATLAB, Lua
;:
- Lisp
;Indentation
- Off-side rule languages: Cobra, CoffeeScript, F#, Haskell, occam, Python
- Free-form languages: most descendants from ALGOL ; Lisp languages
- Ada, Visual Basic, Seed7:
if
...end if
- APL:
:If
...:EndIf
or:If
...:End
- Bash, sh, and ksh:
if
...fi
,do
...done
,case
...esac
; - ALGOL 68:
begin
...end
,,
if
...fi
,do
...od
- Lua, Pascal, Modula-2, Seed7:
repeat
...until
- COBOL:
IF
...END-IF
,PERFORM
...END-PERFORM
, etc. for statements;....
for sentences. - Visual Basic.Net:
If
...End If
,For
...Next
,Do
...Loop
- Small Basic:
If
...EndIf
,For
...EndFor
,While
...EndWhile
Comments
- style
- parse rules
- recursivity
- uses
Inline comments
Examples:
Symbol | Languages |
C | Fortran I to Fortran 77 |
REM | BASIC, Batch files |
:: | Batch files, COMMAND.COM, cmd.exe |
NB. | J; from the common abbreviation Nota bene, the Latin for "note well". |
⍝ | APL; the mnemonic is the glyph resembles a desk lamp, and hence "illuminates" the foregoing. |
# | Bourne shell and other UNIX shells, Cobra, Perl, Python, Ruby, Seed7, Windows PowerShell, PHP, R, Make, Maple, Elixir, Nim |
% | TeX, Prolog, MATLAB, Erlang, S-Lang, Visual Prolog |
// | ActionScript, C, C++, C#, D, F#, Go, Java, JavaScript, Kotlin, Object Pascal, Objective-C, PHP, Rust, Scala, SASS, Swift, Xojo |
' | Monkey, Visual Basic, VBScript, Small Basic, Gambas, Xojo |
! | Fortran, Basic Plus, Inform, Pick Basic |
; | Assembly x86, AutoHotkey, AutoIt, Lisp, Common Lisp, Clojure, Rebol, Red, Scheme |
| Euphoria, Haskell, SQL, Ada, AppleScript, Eiffel, Lua, VHDL, SGML, PureScript |
* | Assembler S/360, COBOL I to COBOL 85, PAW, Fortran IV to Fortran 77, Pick Basic |
| Curl |
| Vimscript, ABAP |
| Forth |
*> | COBOL 90 |
Block comments
Block comments are generally those that use a delimiter to indicate the beginning of a comment, and another delimiter to indicate the end of a comment. In this context, whitespace and newline characters are not counted as delimiters.Examples:
Symbol | Languages |
comment ~ ; | ALGOL 60, SIMULA |
¢ ~ ¢ ,# ~ # , co ~ co ,comment ~ comment | ALGOL 68 |
/* ~ */ | ActionScript, AutoHotkey, C, C++, C#, D, Go, Java, JavaScript, kotlin, Objective-C, PHP, PL/I, Prolog, Rexx, Rust, Scala, SAS, SASS, SQL, Swift, Visual Prolog, CSS |
#cs ~ #ce | AutoIt |
/+ ~ +/ | D |
/# ~ #/ | Cobra |
<# ~ #> | Powershell |
<!-- ~ --> | HTML, XML |
=begin ~ =cut | Perl |
#` | Raku |
=begin ~ =end | Ruby |
# ~ # , #stop ~ EOF ,#iffalse ~ #endif , #ifntrue ~ #endif ,#if false ~ #endif , #if !true ~ #endif | S-Lang |
| Haskell |
| Delphi, ML, Mathematica, Object Pascal, Pascal, Seed7, Applescript, OCaml, Standard ML, Maple, Newspeak, F# |
| Delphi, Object Pascal, Pascal, Red |
| Nunjucks, Twig |
| Mustache, Handlebars |
| Handlebars |
~
| Curl |
% | MATLAB |
~
| Lisp, Scheme, Racket. |
| Nim |
-- ~ ,--=] ,--= ...=] | Lua |
" ~ " | Smalltalk |
| Clojure |
Unique variants
;Fortran- Indenting lines in Fortran 66/77 is significant. The actual statement is in columns 7 through 72 of a line. Any non-space character in column 6 indicates that this line is a continuation of the previous line. A '
C
' in column 1 indicates that this entire line is a comment. Columns 1 though 5 may contain a number which serves as a label. Columns 73 though 80 are ignored and may be used for comments; in the days of punched cards, these columns often contained a sequence number so that the deck of cards could be sorted into the correct order if someone accidentally dropped the cards. Fortran 90 removed the need for the indentation rule and added inline comments, using the!
character as the comment delimiter.
- In fixed format code, line indentation is significant. Columns 1–6 and columns from 73 onwards are ignored. If a
*
or/
is in column 7, then that line is a comment. Until COBOL 2002, if aD
ord
was in column 7, it would define a "debugging line" which would be ignored unless the compiler was instructed to compile it.
- Cobra supports block comments with "
/#
...#/
" which is like the "/*
...*/
" often found in C-based languages, but with two differences. The#
character is reused from the single-line comment form "#
...", and the block comments can be nested which is convenient for commenting out large blocks of code.
- Curl supports block comments with user-defined tags as in
|foo#... #foo|
.
- Like raw strings, there can be any number of equals signs between the square brackets, provided both the opening and closing tags have a matching number of equals signs; this allows nesting as long as nested block comments/raw strings use a different number of equals signs than their enclosing comment:
--comment --=]
. Lua discards the first newline that directly follows the opening tag.
- Block comments in Perl are considered part of the documentation, and are given the name Plain Old Documentation. Technically, Perl does not have a convention for including block comments in source code, but POD is routinely used as a workaround.
- PHP supports standard C/C++ style comments, but supports Perl style as well.
- The use of the triple-quotes to comment-out lines of source, does not actually form a comment. The enclosed text becomes a string literal, which Python usually ignores.
- Raku uses
#`
to denote block comments. Raku actually allows the use of any "right" and "left" paired brackets after#`
. Brackets are also allowed to be nested inside comments.
- Block comment in Ruby opens at
=begin
line and closes at=end
line.
- The region of lines enclosed by the
and#<tag>
delimiters are ignored by the interpreter. The tag name can be any sequence of alphanumeric characters that may be used to indicate how the enclosed block is to be deciphered. For example,#</tag>
could indicate the start of a block of LaTeX formatted documentation.#<latex>
- The next complete syntactic component can be commented out with
#;
.
ABAP supports two different kinds of comments. If the first character of a line, including indentation, is an asterisk the whole line is considered as a comment, while a single double quote begins an in-line commet which acts until the end of the line. ABAP comments are not possible between the statements
EXEC SQL
and ENDEXEC
because Native SQL has other usages for these characters. In the most SQL dialects the double dash can be used instead.;Esoteric languages
- Many esoteric programming languages follow the convention that any text not executed by the instruction pointer or otherwise assigned a meaning, is considered a "comment".
Comment comparison
BlockComment
in italics is used here to indicate block comment style.InlineComment
in italics is used here to indicate inline comment style.Language | In-line comment | Block comment |
Ada, Eiffel, Euphoria, Occam, SPARK, ANSI SQL, and VHDL | -- InlineComment | |
ALGOL 60 | comment BlockComment; | |
ALGOL 68 | ¢ BlockComment ¢ comment BlockComment comment co BlockComment co # BlockComment # £ BlockComment £ | |
APL | ⍝ InlineComment | |
AppleScript | -- InlineComment |
|
Assembly language | ; InlineComment one example | |
AutoHotkey | ; InlineComment | /* BlockComment */ |
AWK, Bash, Bourne shell, C shell, Maple, R, Tcl, and Windows PowerShell | # InlineComment | <# BlockComment #> |
BASIC : | 'InlineComment REM InlineComment | |
C, CHILL, PL/I, and REXX | /* BlockComment */ | |
C, C++, Go, Swift and JavaScript | // InlineComment | /* BlockComment */ |
C# | // InlineComment /// InlineComment | /* BlockComment */ /** BlockComment */ |
COBOL I to COBOL 85 | * InlineComment | |
COBOL 2002 | *> InlineComment | |
Curl | || InlineComment | |# BlockComment #| |foo# BlockComment #| |
Cobra | # InlineComment | /# BlockComment #/ |
D | // InlineComment /// Documentation InlineComment | /* BlockComment */ /** Documentation BlockComment */ /+ BlockComment +/ /++ Documentation BlockComment +/ |
DCL | $! InlineComment | |
ECMAScript | // InlineComment | /* BlockComment */ |
Forth | \ InlineComment | stack comment convention |
FORTRAN I to FORTRAN 77 | C InlineComment | |
Fortran 90 | ! InlineComment | |
Haskell | -- InlineComment |
|
Java | // InlineComment | /* BlockComment */ /** BlockComment */ |
Lisp and Scheme | ; InlineComment | #| BlockComment |# |
Lua | -- InlineComment | |
Maple | # InlineComment |
|
Mathematica |
| |
Matlab | % InlineComment | % Note: Both percent–bracket symbols must be the only non-whitespace characters on their respective lines. |
Nim | # InlineComment |
|
Object Pascal | // InlineComment |
|
OCaml |
| |
Pascal, Modula-2, Modula-3, Oberon, and ML: |
| |
Perl and Ruby | # InlineComment | =begin
|
PHP | # InlineComment // InlineComment | /* BlockComment */ /** Documentation BlockComment */ |
PILOT | R:InlineComment | |
PLZ/SYS | ! BlockComment ! | |
PL/SQL and TSQL | -- InlineComment | /* BlockComment */ |
Prolog | % InlineComment | /* BlockComment */ |
Python | # InlineComment |
|
Raku | # InlineComment | #` =comment |
Red | ; InlineComment |
|
Rust | // InlineComment /// InlineComment //! InlineComment | /* BlockComment */ /** BlockComment */ /*! BlockComment */ |
SAS | * BlockComment; /* BlockComment */ | |
Seed7 | # InlineComment |
|
Simula | comment BlockComment; ! BlockComment; | |
Smalltalk | "BlockComment" | |
Smarty |
| |
Standard ML |
| |
TeX, LaTeX, PostScript, Erlang, Elixir and S-Lang | % InlineComment | |
Texinfo | @c InlineComment @comment InlineComment | |
TUTOR | * InlineComment command $$ InlineComment | |
Visual Basic |
Rem InlineComment | |
Visual Basic.NET |
Rem InlineComment | |
Visual Prolog | % InlineComment | /* BlockComment */ |
Wolfram Language |
| |
Xojo | ' InlineComment // InlineComment rem InlineComment'' |