5.4
The syntax
So far, we have already learned that every command has to be completed with a semicolon.
There are other syntax rules, i.e. rules that specify how a script should be written in order
to be understood by the interpreter. We will discuss them by looking at the next example:
//This is an example that illustrates the JScript Syntax
import System;
import DoasCore.Spectra;
var ABCDEFGH;
var abcdefgh;
var aBcDeFgH;
var A123;
/* the text between the stars
will be ignored */
Filename = Specbar.CurrentSpectrum.Name;
System.Console.WriteLine(Filename)
• The script starts with a so-called comment, something that the interpreter will ignore
since it appears after the comment-symbol ”//”. Comments that shall extend over
several lines, are put between ”/*” and ”*/”.
• It is important that all import-commands are put at the beginning of the script.
• JScript is a case-sensitive language. That means, it does matter if variables, objects
or commands are written in uppercase or lowercase. If two names differ in their cases,
the interpreter will distinguish between them. So only refer to variables in the way
you declared them!
14
Chapter 6
Programming in JScript
This part of the introduction into the more general aspects of programming in JScript,
concerning data types, operators, loops, for-blocks, if-blocks, and more, aims to give you
a basic idea of the structure of typical JScripts. Most of the commands are identical to
the Java or C/C++ language. If you never came in touch with one of those programming
languages, you might also want to consult other learning materials as given in the literature
below to learn about details which can not be covered here. Here, you will find the essentials
that enable you to write powerful JScripts.
6.1
Data Types and Type Conversion
So far, we only have used so-called untyped variables, i.e. those that are declared by using
the var-command. In most cases you need not to worry about data types. But sometimes
it might be better to declare explicitly the type of a variable. In the table below you can
see some examples of the declaration of typed variables. We will now discuss more detailed
what data types are, and when it is useful to use them.
In short, data types are specifications of how to handle variables. The interpreter needs to
know how much memory he shall reserve for the data, and what can be done with these.
For example, the data type integer is of size 32 bit, and you can add, subtract, multiply
and divide (with rest) integers. By doing so, another integer is produced. Different data
types differ in memory, in how they are interpreted, and in what can be done with them.
As another example, strings cannot be added like numbers, but linked, which is also done
by the +. Here is a list of data types available in JScript:
To understand the difference between typed and untyped variables, let’s compare the fol-
lowing scripts:
//A well working script
import System;
var s = 3;
s=s+5;
Console.WriteLine(s);
s = "I’m not an integer!";
Console.WriteLine(s);
15
Data Type
Description
Example
char
a single character,
var c:
char = ’a’;
enclosed in single quotation marks
String
a sequence of characters,
var s:
String = "Hello";
enclosed in double quotation marks
int
a 32-bit integer value, ranging
var i:
int = 1;
from -2.147.483.648 to 2.147.483.647
uint
a unsigned 32-bit integer,
var i:
uint = 1;
ranging from 0 to 4.294.967.295
boolean
a boolean value that is
var b:
boolean = false;
either true or false
double
floating point number of
var d:
double = 0.123;
double precision (about 15 digits)
Date
object representing
var date:
Date =
a date and time definition
"10/08/2003 15:45";
//Example of a type mismatch
import System;
var s:
int = 3;
s = s + 5;
Console.WriteLine(s);
s = "I’m not an integer!";
Console.WriteLine(s);
The first script will work perfectly, whereas the second script will produce the error ”Type
mismatch”.
Why then should it be useful to use typed variables if they just produce more errors? The
answer is: typed variables are less confusing and protect you against misuse. Look at these
examples:
import System;
var s = 5;
s = "I’m a string!";
s = s + 3;
Console.WriteLine(s);
Output: ”I’m a string!3”
16
import System;
var s:
int = 5;
s= "I’m a string!";
s = s + 3;
Console.WriteLine(s);
You will get the error message: ”type mismatch”
Warning:
If you do not initialize a variable, i.e. if you declare a variable
without setting it to a certain value, requesting the value will return NaN
(Not a Number) or ”undefined”, depending on the context in which the
variable was interpreted. For example,
import System;
var i;
Console.WriteLine( "Value of i:
" + i);
Console.WriteLine( "Value of 2*i:
" + 2*i);
will produce the output ”Value of i:
undefined” and ”Value of i:
NaN”, because in the first case we have a type conversion to string, in the
second to double and then to string.
6.2
Operators
You already know mathematical operators such as +,-,*,/. In a programming language,
they can indeed be used to add/multiply numbers (such as integer, double), but there are
also other usages. As we have already seen, you can also connect two string variables to one
string with the +. But there are some more operators that we have to learn about.
17