Break as valid logical expression

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Break as valid logical expression

John W. Eaton-6
On 13-Nov-2002, Pascal A. Dupuis <[hidden email]> wrote:

| On Tue, 12 Nov 2002, John W. Eaton wrote:
|
| [...]
|
| >   for i = 1:10, i, i == 3 && (x = break); end, x
| >
| > but not
| >
| >   for i = 1:10, i, i == 3 && (x = break + 1); end, x
| >
| > In all cases, the value of break, continue, and return is logical
| > TRUE.
| >
| > Should the use of these new "expressions" be available for use in any
| > kind of expression (addition, subtraction, etc.) or be restricted even
| > more (say, only in && and || expressions?
|
| Hello John,
|
| thank you for taking into account my remark. As I said, with a Perl
| background, the idea is to make legal such statements as
| some_test() || do_that,
|
| whith 'do_that' something with can be _evaluated_ .
| So I think it makes sense to have break, continue, and return considered
| as valid "expression" in the context of a "&&" or a "||" operator, but,
| quite frankly, I don't understand what would mean a "x=break" statement.

Neither do I, which is why I was asking whether these expressions
(break, continue, return) should be limited to only && and ||
expressions).

| To be more precise, I would say that break and friends should be
| considered as valid *logical* expression as long as their "return" value
| is not used after the whole statement has been evaluated,

I don't know how to limit that given the current implementation of
Octave's interpreter.  As things are now, break, continue, and return
simply set a global flag which is inspected by the interpreter when
the next expression is evaluated.  In order to be valid in any
expression context (even if limited to && and || expressions), they
must return some value.

jwe


Reply | Threaded
Open this post in threaded view
|

Re: Break as valid logical expression

Pascal A. Dupuis-4
On Wed, 13 Nov 2002, John W. Eaton wrote:

> | quite frankly, I don't understand what would mean a "x=break" statement.
>
> Neither do I, which is why I was asking whether these expressions
> (break, continue, return) should be limited to only && and ||
> expressions).
The purpose is to emulate sh and Perl behaviour:
1) evaluate the left part of the logical expression
2) if the result permit to know the final result, then done
3) otherwise, evaluate the right part.

So:
test || break: break only occur if test if false. If it's true, then the
final result is true
test && break: break only occur if test is true. If it's false, then the
final result is false


> I don't know how to limit that given the current implementation of
> Octave's interpreter.  As things are now, break, continue, and return
> simply set a global flag which is inspected by the interpreter when
> the next expression is evaluated.  In order to be valid in any
> expression context (even if limited to && and || expressions), they
> must return some value.
>
I would suggest having them 'return' some value, but which is
illegal to assign to any variable, so
test || break;
is OK, while
x=(test || break) is not

Regards

Pascal Dupuis

--
Dr. ir. Pascal Dupuis
K. U. Leuven, ESAT/ELECTA (formerly ELEN):  http://www.esat.kuleuven.ac.be/
Kasteelpark Arenberg, 10; B-3001 Leuven-Heverlee, Belgium
Tel. +32-16-32 10 12 -- Fax +32-16-32 19 85