R, C++, Python 語法比較

修改自以下 Python vs. C++ 網頁

http://www.unco.edu/nhs/mathsci/ClassSites/hoppercourse/CS102/S08/syntaxcompar.html

Syntax  Description Python C++ R
 including a module/library  from math import *  #include <cmath>  library(tree)
 assignment operator  =, +=, -=, *=, /=, %=  =, +=, -=, *=, /=, %=  =, <-, <<-
 type integer value  int, long  short, int, long  integer
 type decimal value  float  float, double, long double  numeric (all double)
 type boolean  bool: True/False or (not 0)/0  bool: true/false or (!0)/0  logical: TRUE/FALSE or (!0)/0
 type charactier  none  char  character
 type string  str  char mystring[50] or string  unlist(strsplit(“ABC”,NULL))
#c(“A”,”B”,”C”)
 for statement  for i in range(10):  for(i = 0; i < 10; i++)  for(i in 1:10)
 if statement  if x != 3:  if (x != 3)  if (x != 3)
 while statement  while x != 3:  while (x != 3)  while (x != 3)
 break out of a loop  break  break  break
 function definition  def myfunction():  int myfunction()  myfunction = function(){

return(…)}
 function call  myfunction()  myfunction();  myfunction()
 and operator  and  &&  && OR & (vector)
 or operator  or  ||  || OR | (vector)
 not operator  not  !  !
 comparison operators  ==, !=, <, <=, >, >=  ==, !=, <, <=, >, >=  ==, !=, <, <=, >, >=
 arithmetic operators  +, -, *, /, %  +, -, *, /, %  +, -, *, /, %/%, %%
 comments – single line  #  //  #
 comments – multiple lines  none  /*    */  none
 pre and post increment/decrement operators  none  ++x, x++, –x, x–  none
 code blocks indentation, ;  { }  { }
 statement separator  end of line  ;  end of line or ; (in same line)
 constants  none  const int interest = .018  none
 Input  raw_input, input  cin, and many others.  con = file(“test1”, “r”) … close(con)
x1 = readLines(“d:/data/bookstore.xml”)
 Output  print  cout, and many others.  cat, write, and many others.