home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Reference Guide / C-C++ Interactive Reference Guide.iso / c_ref / csource5 / 349_01 / sss.arc / LANGUAGE.SSS < prev    next >
Encoding:
Text File  |  1990-12-25  |  14.6 KB  |  209 lines

  1.                        MAIN LANGUAGE FEATURES EXEMPLIFIED
  2.                        ==================================
  3.  
  4.  
  5.                                  1. Line Format
  6.  
  7. ╔══════════╦══════════════╦══════════════╦═══════════════════════╦══════════════╗
  8. ║ Feature  ║     BASIC    ║      C       ║      FORTRAN          ║    Pascal    ║
  9. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  10. ║Where to  ║free          ║free          ║columns 7-72: code     ║free          ║
  11. ║write     ║              ║              ║cols 2-5: number label ║              ║
  12. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  13. ║More than ║No for        ║OK            ║Never                  ║OK            ║
  14. ║1 commands║certain       ║              ║                       ║              ║
  15. ║per line? ║instructions  ║              ║                       ║              ║
  16. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  17. ║At end of ║: if another  ║;             ║Nothing                ║;             ║
  18. ║command   ║' if remark   ║              ║                       ║              ║
  19. ║          ║follows       ║              ║                       ║              ║
  20. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  21. ║Remark    ║i=5 'initially║i=5;/* initial║  i = 5                ║i=5{initially}║
  22. ║          ║'to end of    ║    value */  ║C initially            ║;             ║
  23. ║          ║'line         ║              ║C C in 1st column      ║              ║
  24. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  25. ║Continued ║print _       ║fprintf(      ║  write(*,*)           ║writeln(      ║
  26. ║command   ║NQ(1)         ║"%d\n",       ║ +NQ(1)                ║NQ(1));       ║
  27. ║          ║              ║NQ(1));       ║C put + at 6th col     ║              ║
  28. ║          ║              ║#define Y "in\║                       ║              ║
  29. ║          ║              ║ #define only"║                       ║              ║
  30. ╚══════════╩══════════════╩══════════════╩═══════════════════════╩══════════════╝
  31.  
  32.  
  33.                               2. Program Structure
  34.  
  35. ╔══════════╦══════════════╦══════════════╦═══════════════════════╦══════════════╗
  36. ║ Feature  ║     BASIC    ║      C       ║      FORTRAN          ║    Pascal    ║
  37. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  38. ║Main      ║'No special   ║main() {      ║  program main         ║begin         ║
  39. ║program   ║'structure    ║...           ║  ...                  ║...           ║
  40. ║          ║              ║}             ║  end                  ║end.          ║
  41. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  42. ║Function  ║function x(d) ║int x(d)      ║  integer function x(d)║function x(d: ║
  43. ║body      ║ x = d        ║double d;     ║  real*8 d             ║ real):       ║
  44. ║          ║end function  ║{ int x;      ║  x = d                ║ integer;     ║
  45. ║          ║              ║  x = d;      ║  return               ║begin         ║
  46. ║          ║              ║  return(x);  ║  end                  ║ x := d;      ║
  47. ║          ║              ║}             ║                       ║end;          ║
  48. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  49. ║Subroutine║sub d(x)      ║void d(x)     ║  subroutine d(x)      ║procedure     ║
  50. ║body      ║ print x      ║int x;        ║  integer x            ║ d(x:integer) ║
  51. ║          ║end sub       ║{ printf("%d",║  write(*,*)x          ║begin         ║
  52. ║          ║              ║  x);         ║  return               ║ writeln(x);  ║
  53. ║          ║              ║}             ║  end                  ║end;          ║
  54. ╚══════════╩══════════════╩══════════════╩═══════════════════════╩══════════════╝
  55.  
  56.  
  57.  
  58.                               3. Using Subroutines
  59.  
  60. ╔══════════╦══════════════╦══════════════╦═══════════════════════╦══════════════╗
  61. ║ Feature  ║     BASIC    ║      C       ║      FORTRAN          ║    Pascal    ║
  62. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  63. ║Declaring ║declare sub _ ║/*ANSI:*/     ║C not generally        ║procedure     ║
  64. ║subroutine║ e(m, s)      ║void e(int,   ║C needed               ║e(m,s:integer)║
  65. ║          ║declare sub _ ║       int);  ║                       ║; forward;    ║
  66. ║          ║ f ()         ║void f(void); ║                       ║procedure f;  ║
  67. ║          ║              ║/*old style:*/║                       ║forward;      ║
  68. ║          ║              ║void e();     ║                       ║              ║
  69. ║          ║              ║void f();     ║                       ║              ║
  70. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  71. ║Calling   ║e m, s        ║e(m,s);       ║  call e(m,s)          ║e(m,s);       ║
  72. ║subroutine║f             ║f();          ║  call f               ║f;            ║
  73. ║(f has no ║              ║              ║                       ║              ║
  74. ║arguments)║              ║              ║                       ║              ║
  75. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  76. ║Declaring ║declare _     ║/*ANSI:*/     ║C not generally        ║function      ║
  77. ║function  ║ function _   ║int y(int;    ║C needed               ║y(m,s:integer)║
  78. ║          ║ y(m, s)      ║      int);   ║                       ║: integer;    ║
  79. ║          ║declare _     ║int z(void);  ║                       ║forward;      ║
  80. ║          ║ function _   ║/*old style:*/║                       ║function z:   ║
  81. ║          ║ z ()         ║int y();      ║                       ║integer;      ║
  82. ║          ║              ║int z();      ║                       ║forward;      ║
  83. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  84. ║Calling   ║i = y(m,s)    ║i = y(m,s);   ║  i = y(m,s)           ║i := y(m,s);  ║
  85. ║function  ║i = z         ║i = z();      ║  i = z()              ║i := z;       ║
  86. ║(z has no ║              ║              ║                       ║              ║
  87. ║arguments)║              ║              ║                       ║              ║
  88. ╚══════════╩══════════════╩══════════════╩═══════════════════════╩══════════════╝
  89.  
  90.  
  91.  
  92.                                  4. Declarations
  93.  
  94. ╔══════════╦══════════════╦══════════════╦═══════════════════════╦══════════════╗
  95. ║ Feature  ║     BASIC    ║      C       ║      FORTRAN          ║    Pascal    ║
  96. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  97. ║Declaring ║common _      ║int s, f      ║  common s,f           ║s,f:integer;  ║
  98. ║global    ║shared s, f   ║/* declare not║                       ║{ declare     ║
  99. ║variables ║              ║   inside any ║                       ║  before any  ║
  100. ║          ║              ║   routine  */║                       ║  routine }   ║
  101. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  102. ║Constants ║const a = 1   ║#define a 1   ║C not available        ║const a = 1;  ║
  103. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  104. ║Boolean   ║const FALSE=0_║#define TRUE 1║  .true.               ║true          ║
  105. ║constants ║ TRUE = not _ ║#define FALSE\║  .false.              ║false         ║
  106. ║          ║ FALSE        ║  0           ║                       ║              ║
  107. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  108. ║Basic     ║i% 'Last      ║int i;        ║  integer i            ║i:integer;    ║
  109. ║types     ║a! 'character ║float a;      ║  real a               ║a:real;       ║
  110. ║          ║d# 'indicates ║double d;     ║  real*8 d             ║d:real*8;     ║
  111. ║          ║c$ 'type      ║char c[8]=    ║  character*8 c        ║c:string[8];  ║
  112. ║          ║              ║ "Hi!";       ║  logical l            ║l:boolean;    ║
  113. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  114. ║Array     ║dim a(1 to 20)║float a[20]   ║  real a(20)           ║a:array[20] of║
  115. ║          ║              ║              ║                       ║  real;       ║
  116. ╚══════════╩══════════════╩══════════════╩═══════════════════════╩══════════════╝
  117.  
  118.  
  119.  
  120.                                5. Basic Operations
  121.  
  122. ╔══════════╦══════════════╦══════════════╦═══════════════════════╦══════════════╗
  123. ║ Feature  ║     BASIC    ║      C       ║      FORTRAN          ║    Pascal    ║
  124. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  125. ║Integer   ║i = 4 \ 2     ║i = 4 / 2     ║  i = 4 / 2            ║i = 4 div 2   ║
  126. ║division  ║              ║              ║                       ║              ║
  127. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  128. ║Raising to║i = 2 ^ 3     ║i = pow(2.0,  ║  i = 2**3             ║i = exp(3 *   ║
  129. ║power     ║              ║        3.0); ║                       ║    ln(2))    ║
  130. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  131. ║Modulo    ║i = 5 mod 2   ║i = 2.0 *     ║  i = mod(5,2)         ║i = 5 mod 2   ║
  132. ║          ║              ║ modf(5.0/2.0,║                       ║              ║
  133. ║          ║              ║ &j);         ║                       ║              ║
  134. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  135. ║Boolean   ║(i<>5 or j=6)_║(i!=5 || j==6)║  (i.ne.5.or.j.eq.6).  ║(i<>5 or j=6) ║
  136. ║expression║ and (k<=8 or_║ && (k<=8 ||  ║ +and.(k.le.8.or.      ║ and (k<=8 or ║
  137. ║          ║ a>=9 or b<0)_║ a>=9 || b<0) ║ +a.ge.9.or.b.lt.0)    ║ a>=9 or b<0) ║
  138. ║          ║ and c<-1     ║ && c<-1      ║ +.and.c.lt.-1         ║ and c<-1     ║
  139. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  140. ║Assignment║i = 3         ║i = 3;        ║  i = 3                ║i := 3;       ║
  141. ╚══════════╩══════════════╩══════════════╩═══════════════════════╩══════════════╝
  142.  
  143.  
  144.  
  145.                         6. Controlling Order of Execution
  146.  
  147. ╔══════════╦══════════════╦══════════════╦═══════════════════════╦══════════════╗
  148. ║ Feature  ║     BASIC    ║      C       ║      FORTRAN          ║    Pascal    ║
  149. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  150. ║Execution ║if i < 1 _    ║if (i<1)      ║  if (i.lt.1) then     ║if i<1 then   ║
  151. ║under a   ║then x = 1 _  ║ x = 1;       ║   x = 1               ║ x = 1 else   ║
  152. ║condition ║else x = -i   ║else x = -i;  ║  else                 ║ x = -i;      ║
  153. ║          ║              ║              ║   x = -i              ║              ║
  154. ║          ║              ║              ║  endif                ║              ║
  155. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  156. ║Repeated  ║for i=1 to 3  ║for(i=0;i<3;  ║  do 9 i=1,3           ║for i:=1 to 3 ║
  157. ║fixed     ║ j = j * 2    ║    i++)      ║   j = j * 2           ║  do          ║
  158. ║number of ║next i        ║ j = j * 2;   ║9 continue             ║  j = j * 2;  ║
  159. ║times     ║              ║              ║                       ║              ║
  160. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  161. ║Loop,     ║while i < 10  ║while (i < 10)║9 continue             ║while i<10 do ║
  162. ║condition ║ i = i + 1    ║{             ║  if (i.lt.10) then    ║begin         ║
  163. ║at top    ║wend          ║  i = i + 1;  ║    i = i + 1          ║ i := i + 1;  ║
  164. ║          ║              ║}             ║    goto 9             ║end;          ║
  165. ║          ║              ║              ║  endif                ║              ║
  166. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  167. ║Loop,     ║do            ║do            ║9 continue             ║repeat        ║
  168. ║condition ║ i = i + 1    ║{             ║    i = i + 1          ║ i = i + 1    ║
  169. ║at bottom ║loop while _  ║  i = i + 1;  ║  if (i.lt.10) goto 9  ║until i >= 10 ║
  170. ║          ║ i < 10       ║}while (i<10);║                       ║              ║
  171. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  172. ║Multiple  ║select case i ║switch(i) {   ║  goto (1,2) i         ║case i of     ║
  173. ║branch    ║ case 1       ║case 1:       ║1 j = 2                ║ 1: j = 2;    ║
  174. ║          ║  j = 2       ║ j=2; break;  ║  goto 8               ║ 2: j = j*2;  ║
  175. ║          ║ case 2       ║case 2:       ║2 j = j*2              ║end;          ║
  176. ║          ║  j = j*2     ║ j=j*2; break;║  goto 8               ║              ║
  177. ║          ║end select    ║}             ║8 continue             ║              ║
  178. ╚══════════╩══════════════╩══════════════╩═══════════════════════╩══════════════╝
  179.  
  180.  
  181.  
  182.                                 7. Input / Output
  183.  
  184. ╔══════════╦══════════════╦══════════════╦═══════════════════════╦══════════════╗
  185. ║ Feature  ║     BASIC    ║      C       ║      FORTRAN          ║    Pascal    ║
  186. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  187. ║Writing   ║print i       ║printf("%d\n",║  write(*,*) i         ║writeln(i);   ║
  188. ║to screen ║              ║ i)           ║                       ║              ║
  189. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  190. ║Writing   ║open "d.out"_ ║f = fopen(    ║  open(unit=7,file=    ║assign(f,     ║
  191. ║to disk   ║ for output _ ║ "d.out","w");║ +'d.out',mode='WRITE',║ 'd.out');    ║
  192. ║          ║ as #1        ║fprintf(f,    ║ +status='NEW')        ║rewrite(f);   ║
  193. ║          ║print #1,i,j  ║ "%d %d\n",   ║  write(7,1)i,j        ║writeln(i,    ║
  194. ║          ║close #1      ║ i,j);        ║1 format(I5,I5)        ║ ' 'j);       ║
  195. ║          ║              ║fclose(f);    ║  close(7,status=      ║close(f);     ║
  196. ║          ║              ║              ║ +'KEEP')              ║              ║
  197. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  198. ║Reading   ║input;"please_║scanf(        ║  write(*,'A\')        ║write(        ║
  199. ║from      ║ enter";i,j   ║ "%d %d",     ║ +'please enter '      ║ 'please:');  ║
  200. ║keyboard  ║              ║ i, j);       ║  read(*,*)i,j         ║readln(i,j);  ║
  201. ╠══════════╬══════════════╬══════════════╬═══════════════════════╬══════════════╣
  202. ║Reading   ║open "i.inp"_ ║f= fopen(     ║  open(unit=8,file=    ║assign(f,     ║
  203. ║from      ║ for input _  ║ "i.inp","r");║ +'i.inp',mode='READ', ║ 'i.inp');    ║
  204. ║disk      ║ as #1        ║fscanf(f,     ║ +status='OLD')        ║reset(f);     ║
  205. ║          ║input #1,a,b  ║ "%7.0f %7.0f"║  read(8,2)a,b         ║readln(f,a,b);║
  206. ║          ║close #1      ║ ,a,b);       ║2 format(F7.0,F7.0)    ║close(f);     ║
  207. ║          ║              ║fclose(f);    ║  close(8)             ║              ║
  208. ╚══════════╩══════════════╩══════════════╩═══════════════════════╩══════════════╝
  209.