Home | SCI Documentation | SCI Tutorials | SCI Tools | SCI Wiki | Community

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.

Code:

(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 Conditional Loop-If section from the SCI Studio Help File.

"switch" Statements

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

Code:

(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 Conditional Loop-If section from 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.

Code:

(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 Conditional Loop-While section from 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.

Code:

(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 Conditional Loop-Do section from 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.

Code:

(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:
Code:

(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 Conditional Loop-For section from 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.

< Previous: Chapter 12 - Methods and Procedures Next: Chapter 14 - Making The Title Screen >
 

by helping to defray some of the costs of hosting this site. If it has been of help to you, please consider contributing to help keep it online.
Thank you.
pixe
Top

© 2013 to present The Sierra Help Pages. All rights reserved. All Sierra games, artwork and music © Sierra.