Expressions
Programming language expressions can be broadly classified into four syntax structures: ;prefix notation *(* (+ 2 3) (expt 4 5))
;infix notation
* Fortran (2 + 3) * (4 ** 5)
;suffix, postfix, or Reverse Polish notation
* Forth 2 3 + 4 5 ** *
;math-like notation
* TUTOR (2 + 3)(45) $$ note implicit multiply operator
Statement delimitation
A language that supports the statement construct typically has rules for one or more of the following aspects: * Statement terminator marks the end of a statement * Statement separator demarcates the boundary between two statements; not needed for the last statement * Line continuation escapes a newline to continue a statement on the next line Some languages define a special character as a terminator while some, called ''line-oriented'', rely on theLine continuation
Listed below are notable line-oriented languages that provide for line continuation. Unless otherwise noted the continuation marker must be the last text of the line. ;\
* m4: dnl
* %
;Character position
* Fortran 77: A non-comment line is a continuation of the prior non-comment line if any non-space character appears in column 6. Comment lines cannot be continued.
* '
, then inserting a -
in column 7 (same position as the *
for comment is used.)
* TUTOR: Lines starting with a tab (after any indentation required by the context) continue the prior command.
The C compiler concatenates adjacent Consuming external software
Languages support a variety of ways to reference and consume other software in the syntax of the language. In some cases this is importing the exported functionality of a library, package or module but some mechanisms are simpler text file include operations. Import can be classified by level (module, package, class, procedure,...) and by syntax (directive name, attributes,...). ;File include *#include <''filename''>
or #include "''filename''"
C preprocessor used in conjunction with C and C++ and other development tools
;File import
* addpath(''directory'')
:-include("''filename''").
#include file="''filename''"
ASP
* #include <''filename''>
or #include "''filename''"
AutoHotkey, AutoIt
* #import "''filename''"
or #import <''filename''>
include "''filename''";
PHP
* include ilenameprogram
or #include ilenameprogram
Pick Basic
* include!("''filename''");
load "''filename''"
require "''filename''";
@import("''filename''");
Zig
;Package import
* #include ''filename''
C, C++
* # ath = "''filename''"mod ''altname'';
@import ''module'';
<<''name''
Mathematica, Wolfram Language
* from ''module'' import *
Python
* extern crate ''libname'';
or extern crate ''libname'' as ''altname'';
or mod ''modname'';
library("''package''")
R:
* IMPORT ''module''
import ''altname'' "''package/name''"
Go:
* import ''package.module'';
or import ''altname'' = ''package.module'';
D
* import ''Module''
or import qualified ''Module'' as ''M''
import ''package''.*
import "''modname''";
import ''altname'' from "''modname''";
import ''package''
or import ''package''._
Scala
* import ''module''
import ''module''
V (Vlang)
* import ''module''
Python
* Lua
* use ''module''
Fortran 90+
* use ''module'', only : ''identifier''
Fortran 90+
* use ''Module'';
use Module qw(''import options'');
use ''Package''.''Name''
uses ''unit''
Pascal
* with ''package''
Ada
* @import("''pkgname''");
Zig
;Class import
* from ''module'' import ''Class''
Python
* import ''package''.''class''
import ''class'' from "''modname''";
import from "''modname''";
import from "''modname''";
import ''package''.''class''
Scala
* import ''package''.
Scala
* import ''package''._
Scala
* use ''Namespace\ClassName'';
PHP
* use ''Namespace\ClassName as AliasName'';
PHP
;Procedure/function import
* from ''module'' import ''function''
Python
* import ''package.module'' : ''symbol'';
D
* import ''package.module'' : ''altsymbolname'' = ''symbol'';
D
* import ''Module'' (''function'')
import ''function'' from "modname";
import from "''modname''";
import from "''modname''";
import ''package''.''function''
import ''package''.''class''.''function''
Scala
* import ''package''.''class''.
Scala
* use function ''Namespace\function_name'';
PHP
* use ''Namespace\function_name as function_alias_name'';
PHP
* use ''module''::''submodule''::''symbol'';
use ''module''::''submodule''::;
use ''module''::''submodule''::''symbol'' as ''altname'';
use const ''Namespace\CONST_NAME'';
PHP
The above statements can also be classified by whether they are a syntactic convenience (allowing things to be referred to by a shorter name, but they can still be referred to by some fully qualified name without import), or whether they are actually required to access the code (without which it is impossible to access the code, even with fully qualified names).
;Syntactic convenience
* import ''package''.*
import ''package''.''class''
open ''module''
import altname "''package/name''"
Go
* import ''altname'' from "''modname''";
import ''module''
Python
Block delimitation
A block is a grouping of code that is treated collectively. Many block syntaxes can consist of any number of items (statements, expressions or other units of code) including one or zero. Languages delimit a block in a variety of ways some via marking text and others by relative formatting such as levels of indentation. ;Curley braces (a.k.a. curly brackets)
:
* Curly brace languages: A defining aspect of curly brace languages is that they use curly braces to delimit a block.
;Parentheses (
... )
* Batchfile, F# (lightweight syntax), ... ">/code> ...
/code>
* Rebol, Red, Self
In philosophy, the self is an individual's own being, knowledge, and values, and the relationship between these attributes.
The first-person perspective distinguishes selfhood from personal identity. Whereas "identity" is (literally) same ...
, Smalltalk
Smalltalk is a purely object oriented programming language (OOP) that was originally created in the 1970s for educational use, specifically for constructionist learning, but later found use in business. It was created at Xerox PARC by Learni ...
(blocks are first class objects. a.k.a. closures)
;begin
... end
:
* Ada, ALGOL
ALGOL (; short for "Algorithmic Language") is a family of imperative computer programming languages originally developed in 1958. ALGOL heavily influenced many other languages and was the standard method for algorithm description used by the ...
, F# (verbose syntax), Pascal, Ruby
Ruby is a pinkish-red-to-blood-red-colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapph ...
(for
, do/while
& do/until
loops), OCaml
OCaml ( , formerly Objective Caml) is a General-purpose programming language, general-purpose, High-level programming language, high-level, Comparison of multi-paradigm programming languages, multi-paradigm programming language which extends the ...
, SCL, Simula
Simula is the name of two simulation programming languages, Simula I and Simula 67, developed in the 1960s at the Norwegian Computing Center in Oslo, by Ole-Johan Dahl and Kristen Nygaard. Syntactically, it is an approximate superset of AL ...
, Erlang.
;do
... end
:
* PL/I
PL/I (Programming Language One, pronounced and sometimes written PL/1) is a procedural, imperative computer programming language initially developed by IBM. It is designed for scientific, engineering, business and system programming. It has b ...
, REXX
Rexx (restructured extended executor) is a high-level programming language developed at IBM by Mike Cowlishaw. Both proprietary and open-source software, open source Rexx interpreter (computing), interpreters exist for a wide range of comput ...
;do
... done
:
* Bash (for
& while
loops), F# (verbose syntax) Visual Basic Visual Basic is a name for a family of programming languages from Microsoft. It may refer to:
* Visual Basic (.NET), the current version of Visual Basic launched in 2002 which runs on .NET
* Visual Basic (classic), the original Visual Basic suppo ...
, Fortran, TUTOR (with mandatory indenting of block body), Visual Prolog
;do
... end
* Lua, Ruby
Ruby is a pinkish-red-to-blood-red-colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapph ...
(pass blocks as arguments, for
loop), Seed7 (encloses loop bodies between do
and end
)
;X ... end
(e.g. if
... end
):
* Ruby
Ruby is a pinkish-red-to-blood-red-colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapph ...
(if
, while
, until
, def
, class
, module
statements), OCaml
OCaml ( , formerly Objective Caml) is a General-purpose programming language, general-purpose, High-level programming language, high-level, Comparison of multi-paradigm programming languages, multi-paradigm programming language which extends the ...
(for
& while
loops), MATLAB
MATLAB (an abbreviation of "MATrix LABoratory") is a proprietary multi-paradigm programming language and numeric computing environment developed by MathWorks. MATLAB allows matrix manipulations, plotting of functions and data, implementat ...
(if
& switch
conditionals, for
& while
loops, try
clause, package
, classdef
, properties
, methods
, events
, & function
blocks), Lua (then
/ else
& function
)
;(begin
...):
* Scheme
;(progn ...):
* Lisp
Lisp (historically LISP, an abbreviation of "list processing") is a family of programming languages with a long history and a distinctive, fully parenthesized Polish notation#Explanation, prefix notation.
Originally specified in the late 1950s, ...
;(do
...):
* Clojure
Clojure (, like ''closure'') is a dynamic programming language, dynamic and functional programming, functional dialect (computing), dialect of the programming language Lisp (programming language), Lisp on the Java (software platform), Java platfo ...
;Indentation
* Off-side rule languages: Boo, Cobra
COBRA or Cobra, often stylized as CoBrA, was a European avant-garde art group active from 1948 to 1951. The name was coined in 1948 by Christian Dotremont from the initials of the members' home countries' capital cities: Copenhagen (Co), Brussels ...
, CoffeeScript
CoffeeScript is a programming language that compiles to JavaScript. It adds syntactic sugar inspired by Ruby, Python, and Haskell in an effort to enhance JavaScript's brevity and readability. Some added features include list comprehension an ...
, F#, Haskell
Haskell () is a general-purpose, statically typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research, and industrial applications, Haskell pioneered several programming language ...
(in do-notation when braces are omitted), LiveScript, occam, Python, Nemerle (Optional; the user may use white-space sensitive syntax instead of the curly-brace syntax if they so desire), Nim, Scala (Optional, as in Nemerle)
* Free-form languages: most descendants from ALGOL
ALGOL (; short for "Algorithmic Language") is a family of imperative computer programming languages originally developed in 1958. ALGOL heavily influenced many other languages and was the standard method for algorithm description used by the ...
(including C, Pascal, and Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Though Perl is not officially an acronym, there are various backronyms in use, including "Practical Extraction and Reporting Language".
Perl was developed ...
); Lisp
Lisp (historically LISP, an abbreviation of "list processing") is a family of programming languages with a long history and a distinctive, fully parenthesized Polish notation#Explanation, prefix notation.
Originally specified in the late 1950s, ...
languages
;Others
* Ada, Visual Basic Visual Basic is a name for a family of programming languages from Microsoft. It may refer to:
* Visual Basic (.NET), the current version of Visual Basic launched in 2002 which runs on .NET
* Visual Basic (classic), the original Visual Basic suppo ...
, Seed7: if
... end if
* ALGOL 68
ALGOL 68 (short for ''Algorithmic Language 1968'') is an imperative programming language member of the ALGOL family that was conceived as a successor to the ALGOL 60 language, designed with the goal of a much wider scope of application and ...
: begin
... end
, (
... )
, if
... fi
, do
... od
* APL: :If
... :EndIf
or :If
... :End
* Bash, sh, and ksh: if
... fi
, do
... done
, case
... esac
;
* COBOL
COBOL (; an acronym for "common business-oriented language") is a compiled English-like computer programming language designed for business use. It is an imperative, procedural, and, since 2002, object-oriented language. COBOL is primarily ...
: IF
... END-IF
, PERFORM
... END-PERFORM
, etc. for statements; ... .
for sentences.* Lua, Pascal, Modula-2, Seed7: repeat
... until
* Small Basic: If
... EndIf
, For
... EndFor
, While
... EndWhile
* Visual Basic (.NET): If
... End If
, For
... Next
, Do
... Loop
Comments
With respect to a language definition, the syntax of Comments can be classified many ways, including:
* Line vs. block a line comment starts with a delimiter
A delimiter is a sequence of one or more Character (computing), characters for specifying the boundary between separate, independent regions in plain text, Expression (mathematics), mathematical expressions or other Data stream, data streams. An ...
and continues to the end of the line (newline
A newline (frequently called line ending, end of line (EOL), next line (NEL) or line break) is a control character or sequence of control characters in character encoding specifications such as ASCII, EBCDIC, Unicode, etc. This character, or ...
marker) whereas a block comment starts with one delimiter and ends with another and can cross lines
* Nestable whether a block comment can be inside another block comment
* How parsed with respect to the language; tools (including compilers and interpreters) may also parse comments but that may be outside the language definition
Other ways to categorize comments that are outside a language definition:
* Inline vs. prologue an inline comment follows code on the same line and a prologue comment precedes program code to which it pertains; line or block comments can be used as either inline or prologue
* Support for API
An application programming interface (API) is a connection between computers or between computer programs. It is a type of software interface, offering a service to other pieces of software. A document or standard that describes how to build ...
documentation generation which is outside a language definition
Line comment
Block comment
In these examples, ~
represents the comment content, and the text around it are the delimiters. Whitespace (including newline
A newline (frequently called line ending, end of line (EOL), next line (NEL) or line break) is a control character or sequence of control characters in character encoding specifications such as ASCII, EBCDIC, Unicode, etc. This character, or ...
) is not considered delimiters.
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 prior 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 line comments, using the !
character as the comment delimiter.
;COBOL
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 a D
or d
was in column 7, it would define a "debugging line" which would be ignored unless the compiler was instructed to compile it.
;Cobra
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
Curl supports block comments with user-defined tags as in , foo# ... #foo,
.
;Lua
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 --[=[ nested comment ">[_nested_comment_.html" ;"title="comment --[=[ nested comment ">comment --[=[ nested comment
. Lua discards the first newline (if present) that directly follows the opening tag.
;Perl
Block comments in Perl are considered part of the documentation, and are given the name Plain Old Documentation (POD). Technically, Perl does not have a convention for including block comments in source code, but POD is routinely used as a workaround.
;PHP
PHP supports standard C/C++ style comments, but supports Perl style as well.
;Python
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 (except when it is the first statement in the body of a module, class or function; see docstring).
;Elixir
The above trick used in Python also works in Elixir, but the compiler will throw a warning if it spots this. To suppress the warning, one would need to prepend the sigil ~S
(which prevents string interpolation) to the triple-quoted string, leading to the final construct ~S""" ... """
. In addition, Elixir supports a limited form of block comments as an official language feature, but as in Perl, this construct is entirely intended to write documentation. Unlike in Perl, it cannot be used as a workaround, being limited to certain parts of the code and throwing errors or even suppressing functions if used elsewhere.
;Raku
Raku uses #`(...)
to denote block comments. Raku actually allows the use of any "right" and "left" paired brackets after #`
(i.e. #`(...)
, #` ../code>, #`
, #`<...>
, and even the more complicated #`{}
are all valid block comments). Brackets are also allowed to be nested inside comments (i.e. #`
goes to the last closing brace).
;Ruby
Block comment in Ruby opens at =begin
line and closes at =end
line.
;S-Lang
The region of lines enclosed by the #<tag>
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, #<latex>
could indicate the start of a block of LaTeX formatted documentation.
;Scheme and Racket
The next complete syntactic component (s-expression) can be commented out with #;
.
;ABAP
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 comment 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 language
An esoteric programming language (sometimes shortened to esolang) is a programming language designed to test the boundaries of computer programming language design, as a proof of concept, as software art, as a hacking interface to another language ...
s follow the convention that any text not executed by the instruction pointer (e.g., Befunge
Befunge is a two-dimensional stack-based, reflective, esoteric programming language. It differs from conventional languages in that programs are arranged on a two-dimensional grid. "Arrow" instructions direct the control flow to the left, ri ...
) or otherwise assigned a meaning (e.g., Brainfuck), is considered a "comment".
Comment comparison
There is a wide variety of syntax styles for declaring comments in source code.
''BlockComment''
in italics is used here to indicate block comment style.
''LineComment''
in italics is used here to indicate line comment style.
See also
* C syntax
* C++ syntax
* Curly bracket programming languages, a broad family of programming language syntaxes
* Java syntax
* JavaScript syntax
The syntax of JavaScript is the set of rules that define a correctly structured JavaScript program.
The examples below make use of the log function of the console object present in most browsers for standard text output.
The JavaScript sta ...
* PHP syntax and semantics
* Python syntax and semantics
References
Notes
{{Notelist
*Syntax