Re: 3.0.3 - type boolean
- Posted by Juergen Luethje <j.lue at gmx.de> May 12, 2007
- 581 views
Alex Caracatsanis wrote: > Juergen Luethje wrote: > > > > Alex Caracatsanis wrote: > > > > [type boolean] > > > > > A serious, newbie question: why is this a useful type to check? > > [snip] > > > Did this answer your question? > > > > Regards, > > Juergen > > I think so Juergen. I understand you to say that by adopting your routine > we will have a "standard" way of declaring a variable of type boolean (which > is already inherent in some other languages, eg Java) and ensuring the > validity of a value we assign to it; and this would make our programs easier > to read and more logically consistent (ie "boolean" implies true/false, > whereas "integer" or "constant" can mean other things as well). Yes? Yes, exactly. > I hadn't thought of doing that. I've been using: > }}} <eucode> > constant FALSE = 0 > constant TRUE = 1 > </eucode> {{{ > > and assigning those values - eg setFlag = TRUE Yes, but before we can assign something to the variable 'setFlag', we must declare it. So in this situation we have an advantage, when we previously have defined a type 'boolean', so that here we can declare boolean setFlag rather than integer setFlag. > I understand the basics of using variables as flags (but am pretty shakey > on using bits for this purpose). > > And I thank you for the particular example you used - I didn't know how to > break out of nested loops using exit! You are welcome. Unfortunately, exiting nested loops in Euphoria is not es easy for a beginner and not as elegant as in some other programming languages. For instance, in several BASIC flavours we can write exit 2 exit 3 in order to exit 2 or 3 loops, respectively. Unfortunately, Euphoria's exit statement can't take an optional argument that denotes the number of nested loops to exit. However, there is a special "trick" which sometimes is useful. It depends on the particular program, whether or not it makes sense to use it. The "trick" simply is to put all nested loops in a routine. Then you can exit _all_ nested loops with a 'return' statement:
function foo() ... for year = 2004 to 2007 do while this != that do for month = 1 to 12 do if a = 27 then return {year, month} -- exit the function, i.e. end if -- exit 3 nested loops in this case end for end while end for return 0 end function
Regards, Juergen