-
Notifications
You must be signed in to change notification settings - Fork 6
Control Structures
Related: Comprehensions and Iteration
if foo then bar
if foo then bar else baz
if foo
bar
else
baz
unless foo then bar else baz
# if !foo then bar else baz
If and Unless return the value returned by their blocks. If there is no else-block and the test is false-for-if or true-for-unless, then undefined
is returned.
switch a
when b then c
when d
e
else
f
switch a
when b then c
else d
NOTE: unlike coffeescript, indentation is optional.
As an alternative to if-then-else chains, you can use the switch statement this way:
switch
when testA then doA
when testB then doB
else elseC
# same as
if testA then doA
else if testB then doB
else elseC
Where testA and testB are arbitrary expressions.
Switch statements return-values work just like if-statements: If one of the switch's cases is selected, the return-value of that case's then-block is returned. If no cases are found, and there is an else-block, the else-block's return-value is returned. Finally, if no cases are found and there is no else-block, undefined
is returned.
These give you direct access to JavaScript's basic looping construct: while
.
COMING SOON: These blocks will define scopes, just like the Caffeine-Specific Comprehensions and Iteration. See also: Scopes and Variables.
while foo do bar
while foo
bar
unless foo do bar
# while !foo do bar
While and Until return the last value returned by their block. (coming soon)
if foo
1
2
# if foo(1) then 2
There are four tail-forms available for enhanced code readability:
foo if bar # if bar then foo
foo unless bar # if !bar then foo
foo while bar # while bar do foo
foo until bar # while !bar do foo
- Home
- Get Started
- Benefits
- Highlights
- Productivity by Design
- CaffeineScript Design
- What is CaffeineScript Good For?
- Get the most out of JavaScript
- Language Comparison
- CHANGELOG
- Blocks Instead of Brackets
- Binary Line-Starts
- Everything Returns a Value
- Streamlined Modules
- Scopes and Variables
- Optional Commas
- Semantics
- Ambiguities