Blocks - the Friends of Conditionals and Loops
Damien Cassou, Stéphane Ducasse and Luc Fabresse
http://stephane.ducasse.free.fr
Remember: A Block Definition Freezes its Body
- Evaluating a block definition does not execute its body
Remember: Block Execution
- Executing a Block is explicit
Blocks are Used to Express Conditions
Yes this is a message ifTrue:ifFalse:
sent to a Boolean
Blocks are Used to Express Loops
- Some simple loops
- Printing 10 dots
Blocks are Used to Express Loops (2)
Blocks are Used to Express Loops (3)
- A traditional
for
loop for i=1,100, i++
- The message
to:by:do:
is sent to an integer
i
will get all the computed values one by one
Blocks are Used For Iterators
- Here the message is sent to the collection itself
- See Lecture on Iterators
Yes ifTrue:ifFalse: is a message!
- Conceptually
ifTrue:ifFalse:
is a message sent to an object: a boolean!
ifTrue:ifFalse:
is in fact radically optimized by the compiler
Implementation Note
- Note that the Virtual Machine shortcuts calls to Boolean such as condition for speed reason
- But you can implement your own conditional methods and debug to see that sending a message is dispatching to the right object
- Implement your own control structure such as
siAlors:sinon:
(in French) and try it
Summary
- Blocks freeze and control computation
- Basis for
- conditionals
- loops / iterators
- exceptions (see future lectures)
- concurrence
/