Difference between revisions of "Syntax Comparison"
From mi-linux
Jump to navigationJump to searchLine 39: | Line 39: | ||
| <pre>None</pre> | | <pre>None</pre> | ||
|- | |- | ||
− | |''' | + | |'''Arithmetic operators''' |
| <pre>+,-,*,/,%,*</pre> | | <pre>+,-,*,/,%,*</pre> | ||
| <pre>+,-,*,/,%,*</pre> | | <pre>+,-,*,/,%,*</pre> | ||
Line 62: | Line 62: | ||
| <pre><, >, <=, >=, ==, !=</pre> | | <pre><, >, <=, >=, ==, !=</pre> | ||
| <pre><, >, <=, >=, ==, !=</pre> | | <pre><, >, <=, >=, ==, !=</pre> | ||
+ | |} | ||
+ | |||
+ | == Input / output == | ||
+ | |||
+ | {| border="1" cellpadding="20" cellspacing="0" align="bottom" | ||
+ | ! | ||
+ | !PHP | ||
+ | !Java | ||
+ | !Scala | ||
+ | !Python | ||
+ | |- | ||
+ | |'''Print to console''' | ||
+ | | <pre>echo("hello");</pre> | ||
+ | | <pre>System.out.println("Hello");</pre> | ||
+ | | <pre>TODO</pre> | ||
+ | | <pre>print "Hello"</pre> | ||
+ | |- | ||
+ | |'''Read from keyboard''' | ||
+ | | <pre>readline("")</pre> | ||
+ | | <pre>BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); | ||
+ | String kbd = in.readLine(); | ||
+ | </pre> | ||
+ | | <pre>val kbd = readLine()</pre> | ||
+ | | <pre>kbd = raw_input() # kbd is a string | ||
+ | kbd = input() # kdb may be some other type | ||
+ | </pre> | ||
|} | |} | ||
Line 151: | Line 177: | ||
!Python | !Python | ||
|- | |- | ||
− | |'''TODO''' | + | |'''for''' |
+ | | <pre>TODO</pre> | ||
+ | | <pre>TODO</pre> | ||
+ | | <pre>TODO</pre> | ||
+ | | <pre>TODO</pre> | ||
+ | |- | ||
+ | |'''foreach''' | ||
+ | | <pre>TODO</pre> | ||
+ | | <pre>TODO</pre> | ||
+ | | <pre>TODO</pre> | ||
+ | | <pre>TODO</pre> | ||
+ | |- | ||
+ | |'''while''' | ||
| <pre>TODO</pre> | | <pre>TODO</pre> | ||
| <pre>TODO</pre> | | <pre>TODO</pre> | ||
Line 158: | Line 196: | ||
|} | |} | ||
− | == | + | == Functions == |
{| border="1" cellpadding="20" cellspacing="0" align="bottom" | {| border="1" cellpadding="20" cellspacing="0" align="bottom" | ||
Line 167: | Line 205: | ||
!Python | !Python | ||
|- | |- | ||
− | |''' | + | |'''Defining functions''' |
− | | <pre> | + | | <pre>function($msg) { |
− | + | echo $msg; | |
− | | | + | } |
+ | </pre> | ||
+ | | No equivalent. Use methods instead. | ||
| <pre>TODO</pre> | | <pre>TODO</pre> | ||
+ | | <pre>def print_msg(msg): | ||
+ | print msg | ||
+ | </pre> | ||
|} | |} | ||
− | == | + | |
+ | == Compound types (arrays, lists, etc) == | ||
{| border="1" cellpadding="20" cellspacing="0" align="bottom" | {| border="1" cellpadding="20" cellspacing="0" align="bottom" | ||
Line 183: | Line 227: | ||
!Python | !Python | ||
|- | |- | ||
− | |'''TODO''' | + | |'''Fixed length array''' |
+ | | <pre>TODO</pre> | ||
+ | | <pre>TODO</pre> | ||
+ | | <pre>TODO</pre> | ||
+ | | <pre>TODO</pre> | ||
+ | |- | ||
+ | |'''Mutable array''' | ||
+ | | <pre>TODO</pre> | ||
+ | | <pre>TODO</pre> | ||
+ | | <pre>TODO</pre> | ||
+ | | <pre>TODO</pre> | ||
+ | |- | ||
+ | |'''Associative arrays / hashes''' | ||
| <pre>TODO</pre> | | <pre>TODO</pre> | ||
| <pre>TODO</pre> | | <pre>TODO</pre> | ||
Line 189: | Line 245: | ||
| <pre>TODO</pre> | | <pre>TODO</pre> | ||
|} | |} | ||
+ | |||
+ | |||
== Exception handling == | == Exception handling == | ||
Line 199: | Line 257: | ||
!Python | !Python | ||
|- | |- | ||
− | |'''TODO''' | + | |'''Raise an exception''' |
+ | | <pre>throw new Exception("Division by zero");</pre> | ||
+ | | <pre>throw new ClassNotFoundException();</pre> | ||
+ | | <pre>TODO</pre> | ||
+ | | <pre>raise ValueError()</pre> | ||
+ | |- | ||
+ | |'''Handle / catch an exception''' | ||
| <pre>TODO</pre> | | <pre>TODO</pre> | ||
| <pre>TODO</pre> | | <pre>TODO</pre> | ||
Line 215: | Line 279: | ||
!Python | !Python | ||
|- | |- | ||
− | |''' | + | |'''Convert object to different type''' |
| <pre>TODO</pre> | | <pre>TODO</pre> | ||
| <pre>TODO</pre> | | <pre>TODO</pre> |
Revision as of 19:44, 23 January 2011
Students
This is a comparison of the syntax of some popular programming languages, including Scala which we will be using on 5CS004. Where you see the word "TODO", please collaborate to fill in the blank parts of the tables. We will be adding to this page as the module progresses.
Useful language references
Basics
PHP | Java | Scala | Python | |
---|---|---|---|---|
Single line comment | // comment |
// comment |
// comment |
# comment |
Multi-line comment | /* comment */ |
/* comment */ |
/* comment */ |
Doesn't have multi-line comments. |
Null object | NULL |
null |
TODO |
None |
Arithmetic operators | +,-,*,/,%,* |
+,-,*,/,%,* |
+,-,*,/,%,* |
+,-,*,/,%,* |
True and false | TRUE, FALSE |
true, false |
TODO |
True, False |
Boolean operators | &&, ||, ! |
&&, ||, ! |
TODO |
and, or, not |
Relational operators | <, >, <=, >=, ==, != |
<, >, <=, >=, ==, != |
<, >, <=, >=, ==, != |
<, >, <=, >=, ==, != |
Input / output
PHP | Java | Scala | Python | |
---|---|---|---|---|
Print to console | echo("hello"); |
System.out.println("Hello"); |
TODO |
print "Hello" |
Read from keyboard | readline("") |
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String kbd = in.readLine(); |
val kbd = readLine() |
kbd = raw_input() # kbd is a string kbd = input() # kdb may be some other type |
Selection
PHP | Java | Scala | Python | |
---|---|---|---|---|
if/else | if ($a==$b) { echo("same"); } else { echo("different"); } |
if (a==b) { System.out.println("same"); } else { System.out.println("different"); } |
TODO |
if a==b: print 'same' else: print 'different' |
else-if | if ($a<0) { echo("negative"); } elseif ($a==0) { echo("zero"); } else { echo("positive"); } |
TODO |
TODO |
if a<0: print 'negative' elif a==0: print 'zero' else: print 'positive' |
switch | switch ($i) { case 0: echo "i equals 0"; break; case 1: echo "i equals 1"; break; case 2: echo "i equals 2"; break; } |
switch ($i) { case 0: System.out.println("i equals 0"); break; case 1: System.out.println("i equals 1"); break; case 2: System.out.println("i equals 2"); break; } |
TODO |
Python has no switch statement. Use "if" instead. |
Iteration
PHP | Java | Scala | Python | |
---|---|---|---|---|
for | TODO |
TODO |
TODO |
TODO |
foreach | TODO |
TODO |
TODO |
TODO |
while | TODO |
TODO |
TODO |
TODO |
Functions
PHP | Java | Scala | Python | |
---|---|---|---|---|
Defining functions | function($msg) { echo $msg; } |
No equivalent. Use methods instead. | TODO |
def print_msg(msg): print msg |
Compound types (arrays, lists, etc)
PHP | Java | Scala | Python | |
---|---|---|---|---|
Fixed length array | TODO |
TODO |
TODO |
TODO |
Mutable array | TODO |
TODO |
TODO |
TODO |
Associative arrays / hashes | TODO |
TODO |
TODO |
TODO |
Exception handling
PHP | Java | Scala | Python | |
---|---|---|---|---|
Raise an exception | throw new Exception("Division by zero"); |
throw new ClassNotFoundException(); |
TODO |
raise ValueError() |
Handle / catch an exception | TODO |
TODO |
TODO |
TODO |
Dealing with types
PHP | Java | Scala | Python | |
---|---|---|---|---|
Convert object to different type | TODO |
TODO |
TODO |
TODO |
Classes, objects, methods, etc
PHP | Java | Scala | Python | |
---|---|---|---|---|
TODO | TODO |
TODO |
TODO |
TODO |