Difference between revisions of "Syntax Comparison"

From mi-linux
Jump to navigationJump to search
m
 
(18 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 +
__TOC__
 +
 +
...Back to [[5CS004]]
 +
 +
 
== Students ==
 
== 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.
  
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 ==
 
== Useful language references ==
Line 14: Line 19:
 
== Basics ==
 
== Basics ==
  
{| border="1" cellpadding="20" cellspacing="0" align="bottom"
+
{| border="1" cellspacing="0" cellpadding="20" align="bottom"
!
+
|-
!PHP
+
|
!Java
+
| PHP
!Scala
+
| Java
!Python
+
| Scala
 +
| Python
 
|-
 
|-
|'''Single line comment'''
+
| '''Single line comment'''
 
| <pre>// comment</pre>
 
| <pre>// comment</pre>
 
| <pre>// comment</pre>
 
| <pre>// comment</pre>
Line 27: Line 33:
 
| <pre># comment</pre>
 
| <pre># comment</pre>
 
|-
 
|-
|'''Multi-line comment'''
+
| '''Multi-line comment'''
 
| <pre>/* comment */</pre>
 
| <pre>/* comment */</pre>
 
| <pre>/* comment */</pre>
 
| <pre>/* comment */</pre>
Line 33: Line 39:
 
| Doesn't have multi-line comments.
 
| Doesn't have multi-line comments.
 
|-
 
|-
|'''Null object'''
+
| '''Null object'''
 
| <pre>NULL</pre>
 
| <pre>NULL</pre>
 
| <pre>null</pre>
 
| <pre>null</pre>
| <pre>TODO</pre>
+
| <pre>null</pre>
 
| <pre>None</pre>
 
| <pre>None</pre>
 
|-
 
|-
|'''+,-,*,/,%,*'''
+
| '''Arithmetic operators'''
 
| <pre>+,-,*,/,%,*</pre>
 
| <pre>+,-,*,/,%,*</pre>
 
| <pre>+,-,*,/,%,*</pre>
 
| <pre>+,-,*,/,%,*</pre>
Line 45: Line 51:
 
| <pre>+,-,*,/,%,*</pre>
 
| <pre>+,-,*,/,%,*</pre>
 
|-
 
|-
|'''True and false'''
+
| '''True and false'''
 
| <pre>TRUE, FALSE</pre>
 
| <pre>TRUE, FALSE</pre>
 
| <pre>true, false</pre>
 
| <pre>true, false</pre>
| <pre>TODO</pre>
+
| <pre>true, false</pre>
 
| <pre>True, False</pre>
 
| <pre>True, False</pre>
 
|-
 
|-
|'''Boolean operators'''
+
| '''Boolean operators'''
| <pre>&&, ||, !</pre>
+
| <pre>&amp;&amp;, &#124;&#124;,&nbsp;!</pre>
| <pre>&&, ||, !</pre>
+
| <pre>&amp;&amp;, &#124;&#124;,&nbsp;!</pre>
| <pre>TODO</pre>
+
| <pre>&amp;&amp;, &#124;&#124;,&nbsp;!</pre>
 
| <pre>and, or, not</pre>
 
| <pre>and, or, not</pre>
 
|-
 
|-
|'''Relational operators'''
+
| '''Relational operators'''
| <pre><, >, <=, >=, ==, !=</pre>
+
| <pre>&lt;, &gt;, &lt;=, &gt;=, ==,&nbsp;!=</pre>
| <pre><, >, <=, >=, ==, !=</pre>
+
| <pre>&lt;, &gt;, &lt;=, &gt;=, ==,&nbsp;!=</pre>
| <pre><, >, <=, >=, ==, !=</pre>
+
| <pre>&lt;, &gt;, &lt;=, &gt;=, ==,&nbsp;!=</pre>
| <pre><, >, <=, >=, ==, !=</pre>
+
| <pre>&lt;, &gt;, &lt;=, &gt;=, ==,&nbsp;!=</pre>
 
|}
 
|}
  
 
+
== Input / output ==
== Selection ==
 
  
 
{| border="1" cellpadding="20" cellspacing="0" align="bottom"   
 
{| border="1" cellpadding="20" cellspacing="0" align="bottom"   
Line 74: Line 79:
 
!Python
 
!Python
 
|-
 
|-
|'''TODO'''
+
|'''Print to console'''
| <pre>TODO</pre>
+
| <pre>echo("hello");</pre>
| <pre>TODO</pre>
+
| <pre>System.out.println("Hello");</pre>
| <pre>TODO</pre>
+
| <pre>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>
 
|}
 
|}
  
== Iteration ==
+
 
 +
== Selection ==
  
 
{| border="1" cellpadding="20" cellspacing="0" align="bottom"   
 
{| border="1" cellpadding="20" cellspacing="0" align="bottom"   
Line 90: Line 106:
 
!Python
 
!Python
 
|-
 
|-
|'''TODO'''
+
|'''if/else'''
| <pre>TODO</pre>
+
| <pre>if ($a==$b) {
| <pre>TODO</pre>
+
    echo("same");
| <pre>TODO</pre>
+
} else {
| <pre>TODO</pre>
+
    echo("different");
 +
}
 +
</pre>
 +
| <pre>if (a==b) {
 +
    System.out.println("same");
 +
} else {
 +
    System.out.println("different");
 +
}
 +
</pre>
 +
| <pre>if (a==b) println("same")
 +
else println("different")
 +
</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>
 +
| No equivalent.
 +
| No equivalent.
 +
| <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>i match {
 +
    case 0 => println("i equals 0")
 +
    case 1 => println("i equals 1")
 +
    case 2 => println("i equals 2")
 +
    case _ => println("i is not 0, 1, or 2")
 +
}
 +
</pre>
 +
| No equivalent. Use "if" instead.
 
|}
 
|}
  
== Compound types (arrays, lists, etc) ==  
+
 
 +
== Iteration ==
  
 
{| border="1" cellpadding="20" cellspacing="0" align="bottom"   
 
{| border="1" cellpadding="20" cellspacing="0" align="bottom"   
Line 106: Line 192:
 
!Python
 
!Python
 
|-
 
|-
|'''TODO'''
+
|'''for'''
| <pre>TODO</pre>
+
| <pre>for ($i=0; $i<10; $i++) {
| <pre>TODO</pre>
+
    echo($i);
| <pre>TODO</pre>
+
}</pre>
| <pre>TODO</pre>
+
| <pre>for(int i=0; i<10; i++) {
 +
    System.out.println(i);
 +
}</pre>
 +
| <pre>for (i <- i to 10)
 +
    println(i)
 +
</pre>
 +
| No equivalent
 +
|-
 +
|'''foreach'''
 +
| <pre>$a = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
 +
foreach ($a as $v) {
 +
    echo $v;
 +
}
 +
</pre>
 +
| <pre>
 +
for (int i : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
 +
    System.out.println(i);
 +
</pre>
 +
| <pre>
 +
mylist.foreach((msg:String) => println(msg))
 +
</pre>
 +
| <pre>for i in range(10):
 +
    print i
 +
</pre>
 +
|-
 +
|'''while'''
 +
| <pre>$i=0;
 +
while ($i<10) {
 +
    echo $i;
 +
    $i++;
 +
}
 +
</pre>
 +
| <pre>int i=0;
 +
while(i<10) {
 +
    System.out.println(i);
 +
    i++;
 +
}
 +
</pre>
 +
| <pre>
 +
var i=0;
 +
while(i<10) {
 +
    println(i)
 +
    i+=1
 +
}
 +
</pre>
 +
| <pre>i=0
 +
while i<10:
 +
    print i
 +
    i += 1
 +
</pre>
 
|}
 
|}
  
== Input / output ==
+
 
 +
== Functions ==
  
 
{| border="1" cellpadding="20" cellspacing="0" align="bottom"   
 
{| border="1" cellpadding="20" cellspacing="0" align="bottom"   
Line 122: Line 258:
 
!Python
 
!Python
 
|-
 
|-
|'''TODO'''
+
|'''Defining functions'''
| <pre>TODO</pre>
+
| <pre>function print_msg($msg) {
| <pre>TODO</pre>
+
    echo $msg;
| <pre>TODO</pre>
+
}
| <pre>TODO</pre>
+
</pre>
 +
| No equivalent. Use methods.
 +
| <pre>def print_msg(msg: String) = {
 +
    println(msg)
 +
}
 +
</pre>
 +
| <pre>def print_msg(msg):
 +
    print msg
 +
</pre>
 
|}
 
|}
  
== Exception handling ==
+
 
 +
== Compound types (arrays, lists, etc) ==  
  
 
{| border="1" cellpadding="20" cellspacing="0" align="bottom"   
 
{| border="1" cellpadding="20" cellspacing="0" align="bottom"   
Line 138: Line 283:
 
!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>
 
| <pre>TODO</pre>
 
| <pre>TODO</pre>
 
| <pre>TODO</pre>
 
| <pre>TODO</pre>
 +
|}
 +
 +
 +
== Exception handling ==
 +
 +
{| border="1" cellpadding="20" cellspacing="0" align="bottom"
 +
|-
 +
|
 +
| PHP
 +
| Java
 +
| Scala
 +
| Python
 +
|-
 +
| '''Raise an exception'''
 +
| <pre>throw new Exception("Division by zero");</pre>
 +
| <pre>throw new ClassNotFoundException();</pre>
 +
| <pre>throw new java.lang.ArithmeticException()</pre>
 +
| <pre>raise ValueError()</pre>
 +
|-
 +
| '''Handle / catch an exception'''
 +
| <pre>TODO</pre>
 +
| <pre>try {
 +
    println(1/0);
 +
}catch( Exception e ) {
 +
 +
System.out.println(""Can't divide by zero""); 
 +
}</pre>
 +
| <pre>try {
 +
    println(1/0)
 +
} catch {
 +
    case exn : java.lang.ArithmeticException => println("Can't divide by zero")
 +
} </pre>
 +
| <pre>try:
 +
    x = 1/0
 +
except ZeroDivisionError as detail:
 +
    print 'Handling run-time error:', detail</pre>
 
|}
 
|}
  
Line 154: Line 347:
 
!Python
 
!Python
 
|-
 
|-
|'''TODO'''
+
|'''Convert object to different type'''
| <pre>TODO</pre>
 
| <pre>TODO</pre>
 
| <pre>TODO</pre>
 
 
| <pre>TODO</pre>
 
| <pre>TODO</pre>
 +
| <pre>(float)1</pre>
 +
| <pre>1.asInstanceOf[Float]</pre>
 +
| <pre>(float)1</pre>
 
|}
 
|}
 +
  
 
== Classes, objects, methods, etc ==
 
== Classes, objects, methods, etc ==
  
{| border="1" cellpadding="20" cellspacing="0" align="bottom"
+
{| border="1" cellspacing="0" cellpadding="20" align="bottom"
!
+
|-
!PHP
+
|
!Java
+
| PHP
!Scala
+
| Java
!Python
+
| Scala
 +
| Python
 +
|-
 +
| '''Root object'''
 +
| <pre>TODO</pre>
 +
| <pre>java.lang.Object</pre>
 +
| <pre>scala.AnyRef</pre>
 +
| <pre>object</pre>
 +
|-
 +
| '''Create an object'''
 +
| <pre>TODO</pre>
 +
| <pre>TODO</pre>
 +
| <pre>TODO</pre>
 +
| <pre>TODO</pre>
 +
|-
 +
| '''Call methods'''
 +
| <pre>TODO</pre>
 +
| <pre>TODO</pre>
 +
| <pre>TODO</pre>
 +
| <pre>TODO</pre>
 +
|-
 +
| '''Create and update instance attribute'''
 +
| <pre>TODO</pre>
 +
| <pre>TODO</pre>
 +
| <pre>TODO</pre>
 +
| <pre>TODO</pre>
 +
|-
 +
| '''Create and update class attribute'''
 +
| <pre>TODO</pre>
 +
| <pre>TODO</pre>
 +
| <pre>TODO</pre>
 +
| <pre>TODO</pre>
 
|-
 
|-
|'''TODO'''
+
| '''Create subclass'''
 
| <pre>TODO</pre>
 
| <pre>TODO</pre>
 
| <pre>TODO</pre>
 
| <pre>TODO</pre>
Line 176: Line 401:
 
| <pre>TODO</pre>
 
| <pre>TODO</pre>
 
|}
 
|}
 +
 +
 +
...Back to [[5CS004]]

Latest revision as of 16:04, 7 March 2011

...Back to 5CS004


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
null
None
Arithmetic operators
+,-,*,/,%,*
+,-,*,/,%,*
+,-,*,/,%,*
+,-,*,/,%,*
True and false
TRUE, FALSE
true, false
true, false
True, False
Boolean operators
&&, ||, !
&&, ||, !
&&, ||, !
and, or, not
Relational operators
<, >, <=, >=, ==, !=
<, >, <=, >=, ==, !=
<, >, <=, >=, ==, !=
<, >, <=, >=, ==, !=

Input / output

PHP Java Scala Python
Print to console
echo("hello");
System.out.println("Hello");
println("Hello")
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");
}
if (a==b) println("same")
else println("different")
if a==b:
    print 'same'
else:
    print 'different'
else-if
if ($a<0) {
    echo("negative");
} elseif ($a==0) {
    echo("zero");
} else {
    echo("positive");
}
No equivalent. No equivalent.
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;
}
i match {
    case 0 => println("i equals 0")
    case 1 => println("i equals 1")
    case 2 => println("i equals 2")
    case _ => println("i is not 0, 1, or 2")
}
No equivalent. Use "if" instead.


Iteration

PHP Java Scala Python
for
for ($i=0; $i<10; $i++) {
    echo($i);
}
for(int i=0; i<10; i++) {
    System.out.println(i);
}
for (i <- i to 10)
    println(i)
No equivalent
foreach
$a = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
foreach ($a as $v) {
    echo $v;
}
for (int i : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
    System.out.println(i);
mylist.foreach((msg:String) => println(msg))
for i in range(10):
    print i
while
$i=0;
while ($i<10) {
    echo $i;
    $i++;
}
int i=0;
while(i<10) {
    System.out.println(i);
    i++;
}
var i=0;
while(i<10) {
    println(i)
    i+=1
}
i=0
while i<10:
    print i
    i += 1


Functions

PHP Java Scala Python
Defining functions
function print_msg($msg) {
    echo $msg;
}
No equivalent. Use methods.
def print_msg(msg: String) = {
    println(msg)
}
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();
throw new java.lang.ArithmeticException()
raise ValueError()
Handle / catch an exception
TODO
try {
    println(1/0);
}catch( Exception e ) { 

System.out.println(""Can't divide by zero"");   
}
try {
    println(1/0)
} catch {
    case exn : java.lang.ArithmeticException => println("Can't divide by zero")
} 
try:
    x = 1/0
except ZeroDivisionError as detail:
    print 'Handling run-time error:', detail

Dealing with types

PHP Java Scala Python
Convert object to different type
TODO
(float)1
1.asInstanceOf[Float]
(float)1


Classes, objects, methods, etc

PHP Java Scala Python
Root object
TODO
java.lang.Object
scala.AnyRef
object
Create an object
TODO
TODO
TODO
TODO
Call methods
TODO
TODO
TODO
TODO
Create and update instance attribute
TODO
TODO
TODO
TODO
Create and update class attribute
TODO
TODO
TODO
TODO
Create subclass
TODO
TODO
TODO
TODO


...Back to 5CS004