[REBOL] if condition vs. while condition Re:
Hi Pihoz, it doesn't even help, to have a look at until ... help until Evaluates a block until it is TRUE. Arguments: block -- (block) where the loop-condition is the return value of the loop-block itself. Ideally they all had the same parameters, I think. regards, Ingo Those were the words of [EMAIL PROTECTED]: Hello, For both a decision-making construct like "if" and a loop construct like "while" we need to specify a condition. Based on the evaluation of that condition, we would proceed with the loop or the body of the conditional. In REBOL, the condition for a while loop is specified as a "block" but the condition for an if statement is specified as a "condition." In this context, what is the definition of a "condition" and why does REBOL treat the two conditions differently? Why not use a block for an "if" statement as we do for loops? help if If condition is TRUE, evaluates the block. Arguments: condition -- block -- (block) help while While a condition block is TRUE, evaluates another block. Arguments: cond-block -- (block) body-block -- (block) ... -- _ ._ ingo@)|_ /| _| _ We ARE all ONE www._|_o _ _ ._ _ www./_|_) |o(_|(/_ We ARE all FREE ingo@| |(_|o(_)| (_| http://www.2b1.de/Rebol/ ._| ._|
[REBOL] if condition vs. while condition Re:
Hi Piroz, In this context, what is the definition of a "condition" A condition is an expression that evaluates either to a logical false (including none, off) or something else. Like in C, everything that is not a logical false is considered true, i.e. if 1 [print {will be evaluated because 1 is not logical false}] will be evaluated because 1 is not logical false if [] [print {will be evaluated because an empty block, [], is not logical false}] will be evaluated because an empty block, [], is not logical false and why does REBOL treat the two conditions differently? The condition expression for if needs to be evaluated exactly once. First, we evaluate the condition expression and then - depending on what this expression evaluates to - we either do or do not evaluate if's body. What happens in that body cannot affect the result of having evaluated the condition expression. There is therefore no reason to pass the condition expression on to if for evaluation. The condition expression will be evaluated in the context in which if is called, and the resulting value will be passed to if. In contrast, while's condition has to be evaluated repeatedly, once on entering while and repeatedly after while's body has been evaluated. Therefore, "while" itself must evaluate its condition expression. It cannot simply receive the result of a single evaluation of the condition epxression. To protect while's condition expression from being evaluated in the context in which while is being called (i.e. the global context of the REBOL shell, or the context of a function in which while is being called), the condition expression is embedded in a block. The condition expression block can contain as many expressions as required. The condition expression itself - not the result of having evaluated it - is passed to while in a block and and while will evaluate the condition expression as often as it needs to. We expect that some expression evaluated in while's body will eventually affect the condition expression and that expression will return false. The condition expression must therefore be able to reflect modifications that occurred as a result of evaluating while's body. Why not use a block for an "if" statement as we do for loops? There are two factors that determine that we must pass the condition expression unmodified - i.e. embedded in a block - to while: the condition expression must be evaluated repeatedly, and during its repeated evaluation changes that occured within while's body must be able to affect values that are processed in the condition expression. Neither of these factors apply to if. There is no reason to complicate if's implementation by forcing it to evaluate its condition expression, when that expression can be safely evaluated before the result of that evaluation is passed to if. ;- Elan [: - )]
[REBOL] if condition vs. while condition Re:
[EMAIL PROTECTED] wrote: In this context, what is the definition of a "condition" and why does REBOL treat the two conditions differently? Why not use a block for an "if" statement as we do for loops? Because WHILE needs to evaluate the condition several times, so it needs the code of the condition, not the result of its execution; IF, on the other hand, just needs the result. If you really want an IF which uses a block, then use the following: my-if: func [ "My strange if" [throw "To make RETURN work as expected"] cond-block [block!] "Condition block" code-block [block!] "Code block" ] [ if do cond-block code-block ] Ciao, /Gabriele./ o) .-^-. (--o | Gabriele Santilli / /_/_\_\ \ Amiga Group Italia --- L'Aquila | | GIESSE on IRC \ \-\_/-/ / http://www.amyresource.it/AGI/ | o) `-v-' (--o
[REBOL] if condition vs. while condition Re:
They're not treated differently. A condition is a condition. "While" gives more options such as including more commands in the conditional block ( as long as the last is conditional ). It's the last condition in the block that determines if the loop continues. "while" and "until" are very useful. If you tried to accomplish the same just using "if"s then you'd be doing alot of unneccessary typing. Rebol is designed to let you write scripts quickly. If you wrote a c+ program to do the same as a simple rebol script, you'd be writting for hours or days. Note: The Rebol guides are getting more and more easy to understand. Rebol recently helped me understand part of the guide and decided to add the help in the next site update.:) timmy [EMAIL PROTECTED] wrote: Hello, For both a decision-making construct like "if" and a loop construct like "while" we need to specify a condition. Based on the evaluation of that condition, we would proceed with the loop or the body of the conditional. In REBOL, the condition for a while loop is specified as a "block" but the condition for an if statement is specified as a "condition." In this context, what is the definition of a "condition" and why does REBOL treat the two conditions differently? Why not use a block for an "if" statement as we do for loops? help if If condition is TRUE, evaluates the block. Arguments: condition -- block -- (block) help while While a condition block is TRUE, evaluates another block. Arguments: cond-block -- (block) body-block -- (block) Thanks. -- Piroz Mohseni [EMAIL PROTECTED] __ Do You Yahoo!? Talk to your friends online with Yahoo! Messenger. http://im.yahoo.com