Chapter 13: Conditional and Looping Statements

SCI Studio's scripting lanuage offers a wide variety of conditional and looping statements: if, else, switch, while, do...while, and for.

" if " and " else " Statements

If statements are the most basic conditional expressions. They test boolean expressions and conditionally execute the code contained in the code block.

Example:

(if(HaveMouse())
  Print("Mouse is available!")
)(
else
  Print("Mouse is NOT available!")
)

(if(SomeVar or SomeOtherVar and not AnotherVar)
  // do something
)

The "Print("Mouse is available!")" is in the if's code block, and the "Print("Mouse is NOT avaliable")" is in the else's code block. If HaveMouse() returns true, the if's code block is executed. Otherwise, the else code block is.

For more information on if and else statements, have a look at the section in the SCI Studio help file.

" switch " Statements

Switch statements are used to test a single expression against many different values.

Example:

(switch(send (User:alterEgo):edgeHit())
  (case
EDGE_TOP
    = newRoomDir north
  )
  (case
EDGE_RIGHT
    = newRoomDir east
  )
  (case
EDGE_BOTTOM
    = newRoomDir south
  )
  (case
EDGE_LEFT
    = newRoomDir west
  )
  (default
    return

  )
)

For more information on switch statements, have a look at the section in the SCI Studio help file.

" while " Statements

While statements are very similar to if statements, but it's code is executed repeatedly until the boolean expression is false.

Example:

(while( not gQuitGame )
  (self:
doit())
  Wait(gSpeed)
)

In this example, if the variable gQuitGame is false (equal to zero), the doit() and Wait() calls are made. They are executed repeatedly until gQuitGame is true (not equal to zero).

For more information on while statements, have a look at the section in the SCI Studio help file.

" do...while " Statements

Do...while statements are very similar to while statements, but it's code is always executed at least once before it's condition is evaluated.

Example:

(do
  (self:
doit())
  Wait(gSpeed)
) while( not
gQuitGame )

In this example, the doit() and Wait() calls are made. Following, it checks if the variable gQuitGame is false (equal to zero). If it is, they are executed again, and repeatedly until gQuitGame is true (not equal to zero).

For more information on do...while statements, have a look at the section in the SCI Studio help file.

" for " Statements

For statements are virtually identical to while statements. Though slightly less flexible, they offer a very useful short hand way of looping.

Example:

(for (= i 0) (< i 10) (++i)
  
Display(" Hello ")
)

In this example, it prints " Hello" ten times, looping from 0 to 9.

These two statements are 100% identical:

(for (= i 0) (< i 10) (++i)
  
Display(" Hello ")
)

= i 0
(while(<
i 10)
  
Display(" Hello ")

  ++
i
)

For more information on for statements, have a look at the section in the SCI Studio help file.

You should now have a general idea on conditional and looping statements. If you don't fully understand them yet, don't worry. Continue on with the tutorial, doing the step by step examples. When done, you should have a good grasp on them. If you still do not, come back to this chapter and read it again, and look at the links to the help file.

<< Prev

Next >>