SCI Studio Tutorial Chapter 13 - Conditional and Looping Statements

From SCI Wiki
Jump to navigationJump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

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 available")" is in the else 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 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.

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 Conditional Loop-Switch 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.

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 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.

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 Conditional and Looping: 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.

Example:
(for (= i 0) (< i 10) ( i)
   Display(" Hello ")
)

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

The following 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 Conditional and Looping: 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 ProceduresNext: Chapter 14 - Making The Title Screen >