Contents

# Programming

Programs can be created and run interactively. Programming a function is demonstrated in the following example of a function ttwo(x), which multiplies its argument by 2. After the definition it can be used like any other Jasymca function.

>> function y=ttwo(x) y=2*x; end
>> ttwo(3.123)
ans = 6.246


Following the keyword function is the prototype with a return variable y. This replaces the construct return y of other programming languages.

If functions are to be reused later, they should be written to a textfile and saved somewhere in Jasymcas searchpath. The filename must be the function name extended by ".m", in the present example ttwo.m. In subsequent sessions the function ttwo can be used without separately loading the file. Several installed functions of Jasymca are provided using this mechanism.

## Branches

"if x A end"

Depending on the condition x one or several statements A are executed. The condition x must be an arbitrary expression, which evaluates to either 0 or 1. The false-case (i.e. x=0) can lead to another branch B:

"if x A else B end"

As an example the Heavyside function:

>> function y=H(x)
>    if (x>=0)
>      y=1;
>    else
>      y=0;
>    end
>  end
>> H(-2)
y = 0
>> H(0)
y = 1


## Loops

Loops with condition x and statement(s) A: while x A end The while-loop is repeated until x becomes false (0).

>> x=1;y=1;
>> while(x<10) y=x+y; x++; end
>> y
y = 46


Loops with counter z and statement(s) A:

"for z = vector A end"

In the for-loop the counter is formally initialized by a vector. In each execution of the loop the counter takes on the value of the next element of vector.

>> x=1;y=1;
>> for(x=1:0.1:100) y=x^2+y; end
>> y
y = 3.3383E6


## Jumps

return, continue, break A function may be prt of the loop, and begins another cycle. break permanently leaves the loop.

>> x=1;
>> while( 1 )
>    if(x>1000)
>       break;
>    end
>    x++;
> end
>> x
x = 1001