Difference between revisions of "Syntax Comparison"
From mi-linux
Jump to navigationJump to searchLine 74: | Line 74: | ||
!Python | !Python | ||
|- | |- | ||
− | |''' | + | |'''if/else''' |
+ | | <pre>if ($a==$b) { | ||
+ | echo("same"); | ||
+ | } else { | ||
+ | echo("different"); | ||
+ | } | ||
+ | </pre> | ||
+ | | <pre>if (a==b) { | ||
+ | System.out.println("same"); | ||
+ | } else { | ||
+ | System.out.println("different"); | ||
+ | } | ||
+ | </pre> | ||
| <pre>TODO</pre> | | <pre>TODO</pre> | ||
+ | | <pre>if a==b: | ||
+ | print 'same' | ||
+ | else: | ||
+ | print 'different' | ||
+ | </pre> | ||
+ | |- | ||
+ | |'''else-if''' | ||
+ | | <pre>if ($a<0) { | ||
+ | echo("negative"); | ||
+ | } elseif ($a==0) { | ||
+ | echo("zero"); | ||
+ | } else { | ||
+ | echo("positive"); | ||
+ | } | ||
+ | </pre> | ||
| <pre>TODO</pre> | | <pre>TODO</pre> | ||
| <pre>TODO</pre> | | <pre>TODO</pre> | ||
+ | | <pre>if a<0: | ||
+ | print 'negative' | ||
+ | elif a==0: | ||
+ | print 'zero' | ||
+ | else: | ||
+ | print 'positive' | ||
+ | </pre> | ||
+ | |- | ||
+ | |'''switch''' | ||
+ | | <pre>switch ($i) { | ||
+ | case 0: | ||
+ | echo "i equals 0"; | ||
+ | break; | ||
+ | case 1: | ||
+ | echo "i equals 1"; | ||
+ | break; | ||
+ | case 2: | ||
+ | echo "i equals 2"; | ||
+ | break; | ||
+ | } | ||
+ | </pre> | ||
+ | | <pre>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; | ||
+ | } | ||
+ | </pre> | ||
| <pre>TODO</pre> | | <pre>TODO</pre> | ||
+ | | Python has no switch statement. Use "if" instead. | ||
|} | |} | ||
Revision as of 19:29, 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 |
+,-,*,/,%,* | +,-,*,/,%,* |
+,-,*,/,%,* |
+,-,*,/,%,* |
+,-,*,/,%,* |
True and false | TRUE, FALSE |
true, false |
TODO |
True, False |
Boolean operators | &&, ||, ! |
&&, ||, ! |
TODO |
and, or, not |
Relational operators | <, >, <=, >=, ==, != |
<, >, <=, >=, ==, != |
<, >, <=, >=, ==, != |
<, >, <=, >=, ==, != |
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 | |
---|---|---|---|---|
TODO | TODO |
TODO |
TODO |
TODO |
Compound types (arrays, lists, etc)
PHP | Java | Scala | Python | |
---|---|---|---|---|
TODO | TODO |
TODO |
TODO |
TODO |
Input / output
PHP | Java | Scala | Python | |
---|---|---|---|---|
TODO | TODO |
TODO |
TODO |
TODO |
Exception handling
PHP | Java | Scala | Python | |
---|---|---|---|---|
TODO | TODO |
TODO |
TODO |
TODO |
Dealing with types
PHP | Java | Scala | Python | |
---|---|---|---|---|
TODO | TODO |
TODO |
TODO |
TODO |
Classes, objects, methods, etc
PHP | Java | Scala | Python | |
---|---|---|---|---|
TODO | TODO |
TODO |
TODO |
TODO |