|
The same very simple program in many different programming languages
I was asked how many languages I teach yesterday - and the answer's quite a few; at times, it's almost "the more the merrier" as to a very great extent they are varieties on a theme much of the way - but different balances of features make some languages especially good for some uses.
As a geek's relaxation after some very heavy days, I have just taken a very simple example - a loop counting downwards - from yesterday's Lua course, and recoded it into a number of different languages - I'll share the code here:
In C
#include <stdio.h>
main() {
int k;
for (k=10; k>=1; k--) {
printf("%d\n",k);
}
}
C Courses - Source of this example
In C++
using namespace std;
#include <iostream>
main() {
int k;
for (k=10; k>=1; k--) {
cout << k << endl;
}
}
C++ Courses - Source of this example
In Java
public class revl {
public static void main(String [] args) {
int k;
for (k=10; k>=1; k--) {
System.out.println(k);
}
}
Java Courses - Source of this example
}
In Lua
for k=10,1,-1 do
print (k)
end
Lua Courses - Source of this example
In PHP
<?php
for ($k=10; $k>=1; $k--) {
print ("$k\n");
}
?>
PHP Courses - Source of this example
In Perl
for ($k=10; $k>=1; $k--) {
print ("$k\n");
}
Perl Courses - Source of this example
In Python
for k in range(10,0,-1):
print k
Python Courses - Source of this example
In Ruby
for k in 1..10
print ("#{11-k}\n")
end
Ruby Courses - Source of this example
In Shell (works in ksh and bash)
let a=10
while [[ $a -ge 1 ]] ; do
echo $a
let a-=1
done
Linux / Unix / Shell Courses - Source of this example
and in Tcl
set k 10
while {$k >= 1} {
puts $k
incr k -1
}
Tcl, Tk, Expect Courses - Source of this example
In Summary ... there are many ways of doing something like this in all of the languages - the examples above are not necessarily best practise, but they give you an idea of programs, loops, variables, and how they compare. I could have added Javascript, XSLT, Fortran, C Shell, Basic and perhaps a few others - but the above are just the languages I happened to have on the machine I was using - one of our teaching machines - and I could test them quickly. Did someone mention awk ...
(written 2010-03-31, updated 2010-04-01)
23b4
Associated topics are indexed under Q102 - Choosing your language [3785] Programming languages - what are the differences between them? - (2012-06-27) [3764] Shell, Awk, Perl of Python? - (2012-06-14) [3619] Ruby v Perl - a comparison example - (2012-02-21) [3558] Python or Lua - which should I use / learn? - (2011-12-21) [3169] Rekeying a table - comparison in #Ruby #Perl and #Python - (2011-02-14) [2866] Ruby - how does it compare and where is it the right language? - (2010-07-11) [2536] All the Cs ... and Java too - (2009-12-13) [2535] When should I use Java, Perl, PHP, or Python? - (2009-12-13) [2507] Admission - (2009-11-19) [2048] Learning to program in PHP, Python, Java or Lua ... - (2009-02-19) [2001] I have not programmed before, and need to learn - (2009-01-19) [1990] Speaking all the languages - (2009-01-12) [76] Learning to program in - (2004-10-07)
G908 - Well House Consultants - Language Comparisons [3112] Public and private courses - subjects available for 2011 - (2010-12-29) [3003] What will we be teaching in six years? - (2010-10-17) [2947] Teaching Lua to a Perl advocate - (2010-09-06) [2755] Books in the store in the USA - still a portent of the UK market to come? - (2010-05-08) [1717] Q - Should I use Perl or Python? - (2008-07-23) [1582] Ruby, C, Java and more - getting out of loops - (2008-03-19) [209] FAQ - Perl or PHP - (2005-02-11)
Some other Articles
A walk within without - Melksham Without Lua Metatables First and last match with Regular Expressions Is Lua an Object Oriented language? The same very simple program in many different programming languages Lua tables - they are everything Ruth Davis, 1916 - 2010 Email metrics and filtering Garlic bread without garlic TCP v UDP / Client v Server - Python examples
|
4090 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82 at 50 posts per page
This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).
|
411d
|