mini-C

“msharpmath”, The Simple is the Best

[100] 023 Tutorial mini-C    as mini-C

//————————————————————————————————————
//   m#math  as   mini-C
//————————————————————————————————————
m#math can be considered as a mini-C in that some keywords of C are implemented. A key difference is the use of pointer. In m#math, no pointer is defined. Also, real numbers are all treated as double in m#math. Therefore, division by an integer becomes

#> 15 / 2 ;
ans =             7.5

which is different from the result 15/2 = 7 in C. In m#math, we use a dot function  ‘.div(a,b)’  to find a quotient of a division.

//——————————-
//  ternary operation, a ? b : c
//——————————-
// b if a is true, otherwise c
#> 3 ? 10 : 20;
ans =              10

#> 0 ? 10 : 20;
ans =              20

//——————————-
//  prefix ++, – –
//——————————-
// variable is increased/decreased ‘before use’
#> a = 1;  b = ++a;
a =               1
b =               2

#> a; b;
a =               2
b =               2

//——————————-
//  postfix ++, — –
//——————————-
// variable is increased/decreased ‘after use’
#> a = 1;  b = a++;
a =               1
b =               1

#> a; b;
a =               2
b =               1

//——————————-
//  if – else
//——————————-
// if(condition) stmt;   // run if true (stmt=statement)
// if(condition) { stmt_list }

// if(condition) stmt
// else stmt

#> n = 3;
if( n == 1 ) a = 10;
else if( n == 2 ) a = 20;
else {
a = 100;
}
n =               3
a =             100

However, unlike Standard C, m#math do not allow assignment in the condition. For example

#> if( n=1 ) a = 1;  // not n == 1, but assignment n = 1 is imposed!
line 1 : error #11340: syntax : ‘)’ is expected, instead of ‘=’
; if( n ??? =1 ) a = 1;

cause an error, since condition do not accept assignment.

//——————————-
//  while
//——————————-
// while(condition) stmt;   // run if true (stmt=statement)
// while(condition) { stmt_list }
// may not run at all if condition is false

i = 0;
while( ++i < 5 ) i;
i =               0
i =               1
i =               2
i =               3
i =               4

//——————————-
//  do while
//——————————-
// do { stmt_list } while(condition);
// run at least once

#> a = 5;
a =               5

#> do { a = a*a; } while( a < -1 );
a =              25

//——————————-
//  for
//——————————-
// for(initial; condition; expr) stmt;
// for(initial; condition; expr) { stmt_list }

// for.n(a,b,c=1) stmt;

// for.n(a,b,c=1) { stmt_list }

// for[number] stmt;          // simple iteration, unique in m#math

// for[number] { stmt_list }

#> a = 0;
a =               0

#> for.i(0,4)  a += 1;  // for(i=0; i<=4; i++) a += 1;
a =               1
a =               2
a =               3
a =               4
a =               5

#> (a,b)=(1,1);  for[10] { (a,b) = (b,a+b);; b;; }  // Pibonachi sequence
a =               1
b =               1
b =               2
b =               3
b =               5
b =               8
b =              13
b =              21
b =              34
b =              55
b =              89
b =             144

//——————————-
//  break
//——————————-
// break is used to exit from ‘while’, ‘for’, ‘switch’

#> i = 0;
i =               0

#> while(1) { i; if( ++i > 5 ) break; }
i =               0
i =               1
i =               2
i =               3
i =               4
i =               5

//——————————-
//  continue
//——————————-
#> i = 0;
i =               0

#> while( ++i < 5 ) { if( i == 3 ) continue; i;  }
i =               1
i =               2
i =               4

The step at i = 3 is skipped by ‘continue’

//——————————-
//  goto
//——————————-
// syntax is
// goto name; … name: stmt
//
// target must start with a string

#> a = 1; goto skip; b = 2; skip: c = 3;
a =               1
c =               3

//——————————-
//  switch
//——————————-
// switch(n) {
//   case n1: stmt: break;
//   case n2: stmt;
//   default: stmt; // optional
// }
// case handles only integers at present

#> (a,b,c) = (0,0,0);
a =               0
b =               0
c =               0

#> n = 4;
n =               4

#{
switch(n) {
case 1: a = 10; break;
default: c = 5; // continue to the next line
case 2: b = 20; break;
case 3: c = 100;
}
#}

c =               5
b =              20

#> a; b; c;
a =               0
b =              20
c =               5

Comments are closed.