Loop constructs: for, while, do – Crunch CRiSP File Editor 6 User Manual

Page 28

Advertising
background image

Page 28

binary | l -> r
binary && l -> r
binary || l -> r
ternary ?: r -> l
binary = += -= *= /= %= >>= <<= &= ^= |= r -> l
binary , l -> r
--------------------------------------------------------------
From K&R, p 49

In the above table, the following are not supported: ->, . (dot), (type), sizeof. Also, crunch does not support
structures, unions, pointers, or explicit dereferencing.

Crunch supports a fair amount of automatic type co-ercion. The following table lists the coercion rules when
used with the arithmetic operators only. (Type coercion is not performed for function arguments). The prefix
character is used to identify the type of the variable or expression - i = integer, f = float, l = list, s = string, a =
any type. sym is used to denote a symbol of the specified type; expr is used to denote an expression
evaluating to the specified type. (Note that these conversions are implicit -- the typecast on the right hand
side is not a supported feature, currently).

isym op= fexpr => iexpr op= (int) fexpr
fsym op= iexpr => fsym op= (double) iexpr
lsym += aexpr => append aexpr to end of lsym

fsym++ => fsym += 1.0
fsym-- => fsym -= 1.0 etc..

iexpr op fexpr => (double) iexpr op fexpr
fexpr op iexpr => fexpr op (double) iexpr
iexpr + sexpr => "iexpr + sexpr"
(string concatenation)
fexpr + sexpr => "fexpr + sexpr"
(string concatenation)
aexpr + lexpr => new list with aexpr at front

Basically, a string plus a number (or vice versa) converts the number to a string and performs string
concatenation. A list plus any type creates a new list by concatenating list and value. An integer and floating
point value when combined results in a floating point value. Using these rules, a value of one type can easily
be converted to a value in another type:

string s;
int ival;

ival = 99;

s = "Value: " + val + " brass monkeys";
/* s = "Value: 99 brass monkeys" */

ival = 1000000;
s = "Value: " + (0.0 + ival) + " big macs";
/* s = "Value: 1e+06 big macs" */

When converting floating point numbers to strings, the "%g" printf format specifier is used.

{button See Also, ALink(crunch,,,)}

Loop constructs: for, while, do

There are three main looping constructs, the for loop, the while, and the do loop. The for loop is a
generalised looping mechanism supporting the following syntax:

for ( init-expr ; while-expr ; post-expr )
statement

Advertising