[Gambas-user] Why this simple program doesn't work
Werner
werda at ...1000...
Sat Sep 17 14:38:41 CEST 2005
Alonzo Flinston wrote:
>FUNCTION NumBinario(binario AS String) AS Boolean
>DIM numbin AS Boolean
>DIM i AS Integer
>DIM n AS Integer
>DIM cifra AS String
>numbin = TRUE
>n = Len(binario)
>FORMMAIN.TextBox1.TEXT = n
>i = 1
>DO WHILE (i <= n AND numbin = TRUE)
>cifra = Mid$(binario, i, 1)
>IF (cifra <> "0" OR cifra <> "1") THEN
>numbin = FALSE
>ELSE
>i = i + 1
>ENDIF
>LOOP
>RETURN numbin
>END
>
>I have a string and I want to check if every character if a binary
>digit, "0" or "1"
>
>Thanks
>
>Francesco
>
>
>
There are a number of reasons why it doesn't work.
One is that
(cifra <> "0" OR cifra <> "1")
will always return true no matter which character cifra contains.
Another problem is the way the routine is written
i = i + 1
never gets executed and you are stuck in an endless loop.
You might recode your function to something like this:
----------------
FUNCTION NumBinario(binario AS String) AS Boolean
DIM numbin AS Boolean
DIM i AS Integer
DIM n AS Integer
DIM cifra AS String
numbin = TRUE
n = Len(binario)
FORMMAIN.TextBox1.TEXT = n
For i =1 to n
cifra = Mid$(binario, i, 1)
IF NOT (cifra = "0" OR cifra = "1") THEN
numbin = FALSE
BREAK
END IF
NEXT
RETURN numbin
END
-----------------------------
The modified routine will return TRUE if the string is empty. Is that okay?
Regards,
Werner
More information about the User
mailing list