1. Re: 3.0.3 - type boolean
- Posted by Juergen Luethje <j.lue at gmx.de> May 12, 2007
- 587 views
Alex Caracatsanis wrote: [type boolean] > A serious, newbie question: why is this a useful type to check? > > Thank you In programs often so called "flags" are used. The name refers to the fact, that (somewhat simplified) a real flag either is hoisted or not. Similarly, variables used as flags are either TRUE or not. The smallest possible flag is a bit, which either is set (= 1) or not set (= 0). So e.g. an integer variable in Euphoria consists of 31 bits ("flags"), which are independent of each other and can be manipulated individually. Well, this is a good subject for scaring beginners ... It is easier to think of a whole variable as a flag, which is either hoisted (TRUE) or not (FALSE). A variable that can only hold the values TRUE or FALSE is called a boolean variable -- in honor of George Boole <http://en.wikipedia.org/wiki/Boole>. Here is a simple example. In the following program we have two nested loops, and when a special condition is met, we want the program to leave _both_ loops. Euphoria's exit statement only exits the innermost loop. We can use a boolean variable to achieve our goal:
constant FALSE = 0, TRUE = not FALSE integer x, y boolean exitFlag x = 5 y = 9 exitFlag = FALSE for i = 1 to 10 do for k = 7 to 12 do if i = x and k = y then exitFlag = TRUE -- set flag to remind ourselves exit -- exit inner loop end if end for if exitFlag = TRUE then exit -- exit outer loop end if end for ? x ? y
The above program only runs if you have previously defined a "boolean" data type in your program. You can also declare integer exitFlag instead, but variables that are boolean by their "nature", from a logical point of view, should better be declared as boolean. In Euphoria this is done by a user-defined type. This has the advantage of both better readability of the code and easier debugging, see also <http://rapideuphoria.com/refman_2.htm#43>. Did this answer your question? Regards, Juergen