Comparing the programming languages ... the same program in 10 languages
How do I add one to a variable? Some languages have a
++ operator, other languages allow you to use
+=, others have an
incr command, and in some you have to write a long(er) assignment statement.
How do I set up a variable in the first place? In some languages, simply mention a variable and it will appear as if by magic, nicely initialised to zero or an empty string. In others you need to set an initial value. And in others you need to tell the computer the type of data it will contain as well as defining the type.
How do I output results? There are all sorts of
puts and
print statements, and even operators such as
<<. And how you join values on the same line, and add the end of line character, is a further question.
I can teach you any of ten languages, and one of the big advantages I have of knowing so many is that I can compare and contrast for you if you're in the process of converting, or if you're learning anew and have vague recollections from School days.
[Course Schedule]
The sample program I've written takes a variable up from nothing / zero to 7, displaying its value twice and incrementing it twice each time around a loop. Results - consistently in all the languages - look like this:
wizard:incr graham$ ./incr.sh
xxx 1
yyy 1
xxx 3
yyy 3
xxx 5
yyy 5
xxx 7
yyy 7
wizard:incr graham$
The programs differ - here are links to the code to do that in ...
C C++ Java Lua PHP Perl Python Ruby Shell (Bash) and
Tcl.
Let's look at just three examples within this main article. Here's the program in Perl:
#!/usr/bin/env perl
while ($n < 7) {
print "xxx ",++$n,"\n";
print "yyy ",$n++,"\n";
}
in Tcl:
#!/usr/bin/env tclsh
set n 0
while {$n < 7} {
puts "xxx [incr n]"
puts "yyy $n"
incr n
}
and in C++:
#include <iostream>
using namespace std;
int main() {
int n = 0;
while (n < 7) {
cout << "xxx " << ++n << endl;
cout << "yyy " << n++ << endl;
}
}
In Perl, the variable is automatically created and assigned an empty value when first encountered, with Perl deciding it has to be a number when it's incremented. In Tcl, variable (almost) always need to be initialised, but you don't need to tell the system the type of data they will contain. And in C you must declare the type, and it's strongly recommended that you intiialise; if you don't, the variable takes the initial value form whatever was left in memory from the previous program.
Both Perl and C++ have the
++ operator to increment a variable. If the ++ is written before the variable name, then it's incremented before it's used for anything else in the context in which it's written. In Tcl, the
incr command provides a shortand way of incrementing a variable.
C++ variable names never start with a $. Perl (scalar) variable names always start with a $. And in Tcl, you specify a $ in contexts where you are just using the existing contents of a variable, but never in the contexts where you are (or may be) setting the value.
Perl and Tcl programs start at the top of the file, whereas your C++ program starts at the function called
main.
In all 3 of the example languages I have chosen, you indicate the scope of a loop using a {} block. But please note that differs when you look at other languages in other examples such as Ruby (the word
end) and Python (where it's done by spacing)
C++ is a compiled language - you need extra instructions to convert the code above into a runnable program. Tcl is a pure interpretter language - each line is interpretted as the program runs (quick to start, slower to run if you have loops). Perl, and most of the other languages for which I've supplied examples, use a Virtual Machine approach which means that the source code is only interpretted once - a little slower to actually start the program than an intepretter, but then much faster to run, and without the hassle of C++'s extra instructions.
If you're unsure which language is right for you, and you're not too far from us, please let me know and we'll arrange for you to pop in for an evening to help you make a choice. There's a lot more to the choice than just the syntax of the source.
(written 2012-06-27, updated 2012-06-30)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Q102 - Choosing your language [76] Learning to program in - (2004-10-07)
[1990] Speaking all the languages - (2009-01-12)
[2001] I have not programmed before, and need to learn - (2009-01-19)
[2048] Learning to program in PHP, Python, Java or Lua ... - (2009-02-19)
[2507] Admission - (2009-11-19)
[2535] When should I use Java, Perl, PHP, or Python? - (2009-12-13)
[2536] All the Cs ... and Java too - (2009-12-13)
[2700] The same very simple program in many different programming languages - (2010-03-31)
[2866] Ruby - how does it compare and where is it the right language? - (2010-07-11)
[3169] Rekeying a table - comparison in #Ruby #Perl and #Python - (2011-02-14)
[3558] Python or Lua - which should I use / learn? - (2011-12-21)
[3619] Ruby v Perl - a comparison example - (2012-02-21)
[3764] Shell, Awk, Perl of Python? - (2012-06-14)
G908 - Well House Consultants - Language Comparisons [209] FAQ - Perl or PHP - (2005-02-11)
[1582] Ruby, C, Java and more - getting out of loops - (2008-03-19)
[1717] Q - Should I use Perl or Python? - (2008-07-23)
[2755] Books in the store in the USA - still a portent of the UK market to come? - (2010-05-08)
[2947] Teaching Lua to a Perl advocate - (2010-09-06)
[3003] What will we be teaching in six years? - (2010-10-17)
[3112] Public and private courses - subjects available for 2011 - (2010-12-29)
Some other Articles
More than just matching with a regular expression in PHPGetting more than a yes / no answer from a regular expression pattern matchMelksham Pride - the Chamber of Commerce, and the futureImproving Wiltshire Rail Offer - it WILL be happeningProgramming languages - what are the differences between them?Steam train calls at Melksham - PicturesLoad path, load and require in Ruby, and a change from 1.8 to 1.9Standard methods available on all objects in RubyPrivate, Protected, Public in Ruby. What about interfaces and abstract classes in Ruby?Ruby of Rails - cleanly displaying model data in the view