Jscript Tutorial Wolfgang Unger and Tobias Sommer



Yüklə 0,62 Mb.
Pdf görüntüsü
səhifə5/21
tarix07.11.2018
ölçüsü0,62 Mb.
#78679
1   2   3   4   5   6   7   8   9   ...   21

Here is a list of the available operators:

Operator


Description

. [] ()


member access, array access, function call

++ – -


!

unary operators

typeof new void

specify return type, object instantiation, undefined value

* / %

multiplication, division, modulo-division



+ - +

addition, subtraction, string concatenation



<< >> >>>

bit-shift



< <

= > >=


smaller as, smaller or equal, greater as, greater or equal

== !=


equality, inequality

&&

logical AND



||

logical OR

&

bit-wise AND



^

bit-wise XOR

|

bit-wise OR



?:

conditions

= += -= *= /=

assignment (also with operators)

,

multiple evaluation



6.2.1

Logical Operators

Logical operators enable you to evaluate boolean variables and expressions.

import System;

var a:

boolean = true;



var b:

boolean = false;

Console.WriteLine("a AND b = " + (a && b));

Console.WriteLine("a OR b = " + (a || b));

Output:

”a And b = false”



”a OR b = true”

6.3


Program Flow

There are many ways to structure the program flow. For example, you might want to exe-

cute a certain command only if a variable has a certain value, or you might want to repeat

a code block for a certain number of times. Now you will learn how to do that.

6.3.1

if..else-statements



The if..else statement is used to split the flow of your program in two or more branches.

Usually, the value of a variable is compared to a certain constant value, and depending on if

the comparison holds or not, different code is executed. Let’s start with a simple example:

18



import System;

var i=Math.random();

if (i<0.5) Console.WriteLine("This number " + i + " is smaller than 0.5");

else Console.WriteLine("This number " + i + " is larger than or equals 0.5");

Here, the code following the if-condition is executed if the condition is true, and the code

following the else-statement is executed if the statement is false. This is what you should

notice:

• the boolean expression has to be set in parenthesis



• the if-clause here only consists of one command. It has to be closed by a semicolon

• the else-clause also only consists of one command and has to be closed by a semicolon

• the else-command has to be the next command after the if-clause.

What if we have more than two cases? Here another example that illustrates this point:

import System;

var i=Math.random();

if (i<0.5) Console.WriteLine("This number " + i + " is smaller than 0.5");

else if (i>0.5) Console.WriteLine("This number " + i + " is larger than 0.5");

else Console.WriteLine("This number " + i + " equals 0.5");

What if you have more than one command that you want to execute in an if- or else-block?

Simply put them between braces as the next example illustrates. This can also be done in

other contexts, for example in loops, as we will see later.

import System;

var i=Math.random();

if (i==0.5)

{

Console.WriteLine("This number " + i + " equals 0.5");



Console.WriteLine("Isn’t that a nice result?");

}

else



{

Console.WriteLine("This number " + i + " is not equal to 0.5");

Console.WriteLine("Sorry, can’t tell you if < 0.5 or > 0.5.");

}

19




Warning:

Always remember that equality-comparisons are done by ”==”,

not ”=”. If you use ”=”, there will not be an error as long as the right-hand

side can be assigned to the left-hand side. So you have to be careful.

Hint:

There is a way to shorten the if..else-statements. The if can



be replaced by ”?”, the else by ”:”. This short notation allows to use

if..else-statements even within arguments. For example, all of the following

lines are equivalent:

if (i==5) Console.WriteLine("five"); else

Console.WriteLine("another number");

(i==5) ?


Console.WriteLine("five"):

Console.WriteLine("another

number");

Console.WriteLine((i==5) ?

"five" :

"another number");

6.3.2

for-Loops



The for-loops allow to repeat a command or block of code for a certain time. Usually, a

counter is counted up until a certain condition does not hold any more. Here is an example:

import System;

var i;


for (i=1;i<10;i++) Console.WriteLine("This is the " + i + ".

line.");


Our example produces 9 lines. A for-loop has the following syntax:

• After the for-command, which opens the for-loop, there is a loop-header consisting of

three statements, which are separated by semicolons.

• The first statement in the parenthesis is the initializing command. It is executed only

once, just before the loop is entered. Usually, the loop variable (here: i) is set to a

certain value.

• The second statement is a boolean expression. The loop is (re)entered only if the

boolean expression is true. Usually, the loop variable is compared to a certain value

which it shall not exceed.

• The third statement is a command that is executed every time the loop was swept.

Usually, the loop variable is incremented here.

• The loop itself, the command that is executed several times, follows the loop-header.

If you use more than one command, make a block with braces.

20



Yüklə 0,62 Mb.

Dostları ilə paylaş:
1   2   3   4   5   6   7   8   9   ...   21




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə