1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
.. _statements:
=================
Statements
=================
.. index::
single: statements
A squirrel program is a simple sequence of statements.::
stats := stat [';'|'\n'] stats
Statements in squirrel are comparable to the C-Family languages (C/C++, Java, C#
etc...): assignment, function calls, program flow control structures etc.. plus some
custom statement like yield, table and array constructors (All those will be covered in detail
later in this document).
Statements can be separated with a new line or ';' (or with the keywords case or default if
inside a switch/case statement), both symbols are not required if the statement is
followed by '}'.
------
Block
------
.. index::
pair: block; statement
::
stat := '{' stats '}'
A sequence of statements delimited by curly brackets ({ }) is called block;
a block is a statement itself.
-----------------------
Control Flow Statements
-----------------------
.. index::
single: control flow statements
squirrel implements the most common control flow statements: ``if, while, do-while, switch-case, for, foreach``
^^^^^^^^^^^^^^
true and false
^^^^^^^^^^^^^^
.. index::
single: true and false
single: true
single: false
Squirrel has a boolean type(bool) however like C++ it considers null, 0(integer) and 0.0(float)
as *false*, any other value is considered *true*.
^^^^^^^^^^^^^^^^^
if/else statement
^^^^^^^^^^^^^^^^^
.. index::
pair: if/else; statement
pair: if; statement
pair: else; statement
::
stat:= 'if' '(' exp ')' stat ['else' stat]
Conditionally execute a statement depending on the result of an expression.::
if(a>b)
a=b;
else
b=a;
////
if(a==10)
{
b=a+b;
return a;
}
^^^^^^^^^^^^^^^^^
while statement
^^^^^^^^^^^^^^^^^
.. index::
pair: while; statement
::
stat:= 'while' '(' exp ')' stat
Executes a statement while the condition is true.::
function testy(n)
{
local a=0;
while(a<n) a+=1;
while(1)
{
if(a<0) break;
a-=1;
}
}
^^^^^^^^^^^^^^^^^^
do/while statement
^^^^^^^^^^^^^^^^^^
.. index::
pair: do/while; statement
::
stat:= 'do' stat 'while' '(' expression ')'
Executes a statement once, and then repeats execution of the statement until a condition
expression evaluates to false.::
local a=0;
do
{
print(a+"\n");
a+=1;
} while(a>100)
^^^^^^^^^^^^^^^^^
switch statement
^^^^^^^^^^^^^^^^^
.. index::
pair: switch; statement
::
stat := 'switch' ''( exp ')' '{'
'case' case_exp ':'
stats
['default' ':'
stats]
'}'
Switch is a control statement allows multiple selections of code by passing control to one of the
case statements within its body.
The control is transferred to the case label whose case_exp matches with exp if none of
the case match will jump to the default label (if present).
A switch statement can contain any number if case instances, if 2 case have the same
expression result the first one will be taken in account first. The default label is only
allowed once and must be the last one.
A break statement will jump outside the switch block.
-----
Loops
-----
.. index::
single: Loops
^^^^^^^^
for
^^^^^^^^
.. index::
pair: for; statement
::
stat:= 'for' '(' [initexp] ';' [condexp] ';' [incexp] ')' statement
Executes a statement as long as a condition is different than false.::
for(local a=0;a<10;a+=1)
print(a+"\n");
//or
glob <- null
for(glob=0;glob<10;glob+=1){
print(glob+"\n");
}
//or
for(;;){
print(loops forever+"\n");
}
^^^^^^^^
foreach
^^^^^^^^
.. index::
pair: foreach; statement
::
'foreach' '(' [index_id','] value_id 'in' exp ')' stat
Executes a statement for every element contained in an array, table, class, string or generator.
If exp is a generator it will be resumed every iteration as long as it is alive; the value will
be the result of 'resume' and the index the sequence number of the iteration starting
from 0.::
local a=[10,23,33,41,589,56]
foreach(idx,val in a)
print("index="+idx+" value="+val+"\n");
//or
foreach(val in a)
print("value="+val+"\n");
-------
break
-------
.. index::
pair: break; statement
::
stat := 'break'
The break statement terminates the execution of a loop (for, foreach, while or do/while)
or jumps out of switch statement;
---------
continue
---------
.. index::
pair: continue; statement
::
stat := 'continue'
The continue operator jumps to the next iteration of the loop skipping the execution of
the following statements.
---------
return
---------
.. index::
pair: return; statement
::
stat:= return [exp]
The return statement terminates the execution of the current function/generator and
optionally returns the result of an expression. If the expression is omitted the function
will return null. If the return statement is used inside a generator, the generator will not
be resumable anymore.
---------
yield
---------
.. index::
pair: yield; statement
::
stat := yield [exp]
(see :ref:`Generators <generators>`).
---------------------------
Local variables declaration
---------------------------
.. index::
pair: Local variables declaration; statement
::
initz := id [= exp][',' initz]
stat := 'local' initz
Local variables can be declared at any point in the program; they exist between their
declaration to the end of the block where they have been declared.
*EXCEPTION:* a local declaration statement is allowed as first expression in a for loop.::
for(local a=0;a<10;a+=1)
print(a);
--------------------
Function declaration
--------------------
.. index::
pair: Function declaration; statement
::
funcname := id ['::' id]
stat:= 'function' id ['::' id]+ '(' args ')' stat
creates a new function.
-----------------
Class declaration
-----------------
.. index::
pair: Class declaration; statement
::
memberdecl := id '=' exp [';'] | '[' exp ']' '=' exp [';'] | functionstat | 'constructor' functionexp
stat:= 'class' derefexp ['extends' derefexp] '{'
[memberdecl]
'}'
creates a new class.
-----------
try/catch
-----------
.. index::
pair: try/catch; statement
::
stat:= 'try' stat 'catch' '(' id ')' stat
The try statement encloses a block of code in which an exceptional condition can occur,
such as a runtime error or a throw statement. The catch clause provides the exceptionhandling
code. When a catch clause catches an exception, its id is bound to that
exception.
-----------
throw
-----------
.. index::
pair: throw; statement
::
stat:= 'throw' exp
Throws an exception. Any value can be thrown.
--------------
const
--------------
.. index::
pair: const; statement
::
stat:= 'const' id '=' 'Integer | Float | StringLiteral
Declares a constant (see :ref:`Constants & Enumerations <constants_and_enumerations>`).
--------------
enum
--------------
.. index::
pair: enum; statement
::
enumerations := ( 'id' '=' Integer | Float | StringLiteral ) [',']
stat:= 'enum' id '{' enumerations '}'
Declares an enumeration (see :ref:`Constants & Enumerations <constants_and_enumerations>`).
--------------------
Expression statement
--------------------
.. index::
pair: Expression statement; statement
::
stat := exp
In Squirrel every expression is also allowed as statement, if so, the result of the
expression is thrown away.
|