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.

One thing to note, before describing the if and else statements is about boolean expressions in general. Boolean (true/false) expressions are handled as one might expect, with one subtlety. A value of 'true' is actually the result any expression evaluating to a non-zero value. I believe this is handled the same as most programming languages, but thought it was worth noting anyway.
To illustrate:
Code: [Select]
(var x)
 
= x 0
(if(x)    // evaluates to false
    Print("True: x is non-zero")
)(else
    Print("False: x is zero")   // this will be output
)
 
= x 1
(if(x)  // evaluates to true
    Print("True: x is non-zero") // this will be output
)(else
    Print("False: x is zero")
)
 
= x 9387
(if(x) // also evaluates to true
    Print("True: x is non-zero") // this will be output
)(else
    Print("False: x is zero")
)

Now that we have covered that, back to our regularly scheduled description of if and else statements.

Example:
Code: [Select]
(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:
Code: [Select]
(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:
Code: [Select]
(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:
Code: [Select]
(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:
Code: [Select]
(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: [Select]
(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.