Previous Post | Top | Next Post |
TOC
This was originally written and created around 2013 and may require to be updated. (2021)
Language basics
See Wikipedia article on programing language.
Please note this may be inaccurate.
- statement
;
as statement terminator: C, C++, Java, Vala, …;
as statement separator: Perl, Pascal, PL/I, SQL, …- EOL as statement terminator: Python
;
or EOL as statement terminator: Shell;
as NOP statement: Lua
- block statement
{
statement...
}
: C, C++, Java, Vala, Perl, Shell, Lua, …BEGIN
statement...
END
: Pascal- indented block : Python
- comment
/* ... */
or// ...
: C, C++, Java, Vala# ...
: Perl, Python, Shell-- ...
or--[[ ... ]]
: Lua (line and block comment)
- character constant
'a'
: C, C++
- string
"abcdef"
: C, C++, Vala, Python, Perl, Lua; Shell(subst)'abcdef'
: Python, Perl, Lua; Shell(non-subst.)"""abcdef"""
: Vala, Python (string literals that span multiple lines)[==[abcdef]==]
: Lua (string literals that span multiple lines)
- string concatenate operator
.
: Perl..
: Lua+
: Vala, Python, Pascal- put together side by side: Shell
- call some function: C (think about glib)
- modulo
%
: C, C++, Vala, Python, Perl, Luamod
: Pascal
- floor
//
: Pythondiv
: Pascal
- power operator
**
: Python, Perl^
: Lua- use
pow(...)
function : C, C++, Vala
- bit not operator
~
: C, C++, Vala, Python, Perl
- bit or operator:
|
: C, C++, Vala, Python, Perl
- bit and operator:
&
: C, C++, Vala, Python, Perl
- bit exclusive-or operator:
^
: C, C++, Vala, Python, Perl
- logical not operator
!
: C, C++, Vala, Shell, Perlnot
: Python, Lua
- logical or operator:
|
: Python||
: C, C++, Vala, Perl, Shellor
: Lua
- logical or operator:
&
: Python&&
: C, C++, Vala, Perl, Shelland
: Lua
if
– triple choices as exampleif
(...)
{
statement...
}
else
if
(...)
{
statement...
}statement...
}` : C, C++, Vala, Javaif
...:
statement...
elif
...:
statement...
else:
statement...
: Python (multilined and indented)if
(...)
{
statement...
}
elsif
(...)
{
statement...
}
else
{
statement...
}
: Perlif
...
then
statement...
elseif
...
then
statement...
else
statement...
end
: Luaif
[...];
then
statement; ...
elif
[...]
;
statement; ...
else
statement; ...
fi
: Shell
- for – indexed loop
for (i = 1; i <= n; i++)
{
statement...
}
: C, C++, Vala, Javafor i in range(1,n):
statement...
: Python (multilined and indented)for ($i = 1; $i <= $n; $i++)
{
statement...
}
: Perlfor i=1, n, 1
do
statement...
end
: Lua
- foreach – iterator loop
for (type &i : items)
{
statement...
}
: C++11foreach (type i in items)
{
statement...
}
: Valafor (type i : items)
{
statement...
}
: Javafor i in items:
statement...
: Python (multilined and indented)foreach $i (@items)
{
statement...
}
: Perlfor i in items ... ;
do
statement; ...
done
: Shell
- while loop
while
(loop_true)
{
statement...
}
: C, C++, Vala, Java, Perlwhile
loop_true:
statement...
: Python (multilined and indented)while
(loop_true)
do
statement...
end
: Luawhile
[...];
do
statement; ...
done
: Shell
- do while loop
do
{
statement...
}
while
(loop_true)
;
: C, C++, Vala, Java, Perlwhile true:
statement...
if
!
loop_true:
break
: Python (multilined and indented)repeat
statement...
until
!
loop_true
: Lua
break
- supported : C, C++, Vala, Java, Python, Lua, Shell
- Instead, use
last
: Perl
continue
- supported : C, C++, Vala, Java, Python, Shell
- Instead, use
next
: Perl - NOT supported : Lua (Use
if
to skip the rest.)
return
- supported : C, C++, Vala, Java, Python, Lua, Shell
- multiple branching
switch(c)
{
case 'a':
statement…break;
default:
statement...
}
: C, C++, Vala, Javacase
$x
in
a)
statement;...
;;
?)
statement;...
;;
esac
: Shell- NOT supported. Use
if ...
: Perl, Python, Lua
Both return
and exit
are used for similar purposes for many languages.
- C :
exit
is a function in thelibc
library.- when
exit
is called, it terminates the process.
- when
- C :
return
is a reserved keyword of the C language.- when
return
is in themain()
function, it terminates the process. - when
return
is not in themain()
function, it returns to the calling function code.
- when
- Shell :
exit
is a builtin.- when
exit
is called, it terminates the shell code.
- when
- Shell :
return
is a builtin.- when
return
is in the main shell code, it terminates the main shell code. - when
return
is in the block code{ ...; }
, it terminates the main shell code. - when
return
is in the subshell( ...; )
, it returns to the main shell code. - when
return
is in the sourced file, it returns to the sourcing shell code.
- when
For the C++ language, you need to be careful uing exit
in main()
since no
destructor will be called for the locally scoped objects for exit
.
Previous Post | Top | Next Post |