Compound conditional in IF statement
Compound conditional in IF statement
Why won't this compile?
If ((SrcStrChar >= "A" and SrcStrChar <= "Z") or
(SrcStrChar >= "a" and SrcStrChar <= "z")) then
ElemStr = ElemStr + SrcStrChar
Endif
I'm not interested in poor form feedback or alternative coding. I just want to know why this multi-line test won't compile. Somewhere I read a colon might be needed? SrcStrChar is 'Dim as String'.
If ((SrcStrChar >= "A" and SrcStrChar <= "Z") or
(SrcStrChar >= "a" and SrcStrChar <= "z")) then
ElemStr = ElemStr + SrcStrChar
Endif
I'm not interested in poor form feedback or alternative coding. I just want to know why this multi-line test won't compile. Somewhere I read a colon might be needed? SrcStrChar is 'Dim as String'.
-
- Posts: 8586
- Joined: May 28, 2005 3:28
- Contact:
Re: Compound conditional in IF statement
You have to use the "_" char to concatenate the splitted code line.
Joshyor write it in one line:
Joshy
Code: Select all
dim as string SrcStrChar,ElemStr
If ((SrcStrChar >= "A" and SrcStrChar <= "Z") or _
(SrcStrChar >= "a" and SrcStrChar <= "z")) then
ElemStr &= SrcStrChar
End if
Code: Select all
dim as string SrcStrChar,ElemStr
If ((SrcStrChar >= "A" and SrcStrChar <= "Z") or (SrcStrChar >= "a" and SrcStrChar <= "z")) then
ElemStr &= SrcStrChar
End if
Re: Compound conditional in IF statement
In most BASIC languages there is no character to mark the end of a statement, like for example in Pascal that uses the semicolon:In BASIC (FreeBASIC is no different):The line ending itself is considered the end of the statement. So to divide a single statement over more than one line the underscore character must be used, like D.J. Peters said:Admittedly, a closing statement character would be more comfortable, but this is just the way it is.
Code: Select all
if a > b then a := b;
Code: Select all
if a > b then a = b
Code: Select all
if a > b then _
a = b
Re: Compound conditional in IF statement
Thanks. Can't believe I didn't run across that in the doc. I thought I had originally coded it just as you showed in the 2nd example but it didn't work for me which is why I split it then got other errors. Both of these work for me now so I'm not sure what happened. I appreciate the help.D.J.Peters wrote:You have to use the "_" char to concatenate the splitted code line.
Joshyor write it in one line:Code: Select all
dim as string SrcStrChar,ElemStr If ((SrcStrChar >= "A" and SrcStrChar <= "Z") or _ (SrcStrChar >= "a" and SrcStrChar <= "z")) then ElemStr &= SrcStrChar End if
Code: Select all
dim as string SrcStrChar,ElemStr If ((SrcStrChar >= "A" and SrcStrChar <= "Z") or (SrcStrChar >= "a" and SrcStrChar <= "z")) then ElemStr &= SrcStrChar End if
Re: Compound conditional in IF statement
Thanks. I don't particularly like that underscore but was unaware of it and it works for me now. I think the need for this is for what we used to refer to as "one pass compilers" that require these leading "tells".Munair wrote:In most BASIC languages there is no character to mark the end of a statement, like for example in Pascal that uses the semicolon:In BASIC (FreeBASIC is no different):Code: Select all
if a > b then a := b;
The line ending itself is considered the end of the statement. So to divide a single statement over more than one line the underscore character must be used, like D.J. Peters said:Code: Select all
if a > b then a = b
Admittedly, a closing statement character would be more comfortable, but this is just the way it is.Code: Select all
if a > b then _ a = b
Yes, I'd actually prefer a closing statement char -- tho the semi is pretty ugly :) How about a question mark or even a user-defined char since some of us are uncertain about our code to begin with!
Re: Compound conditional in IF statement
Well, this is BASIC, which never had such a thing.Flyzone wrote:Yes, I'd actually prefer a closing statement char
You'll have to use C or Pascal, if you're insisting on a closing statement.
No go, the question mark is a synonym for 'Print' statement, in FB.Flyzone wrote:How about a question mark or ...
See FB-Manual, for more detailed information ...
-
- Posts: 862
- Joined: May 05, 2015 5:35
- Location: Germany
Re: Compound conditional in IF statement
IMHO it considerably eases the typing of the FB source code that there's no closing statement.
Re: Compound conditional in IF statement
Funny you say that. When I switch from a Pascal project to BASIC I often tend to explicitely 'close' each statement. When it's the other way around, I leave Pascal lines without. Who invented all those different programming languages ?!?!!grindstone wrote:IMHO it considerably eases the typing of the FB source code that there's no closing statement.
Re: Compound conditional in IF statement
Flyzone wrote:Thanks. Can't believe I didn't run across that in the doc. I thought I had originally coded it just as you showed in the 2nd example but it didn't work for me which is why I split it then got other errors. Both of these work for me now so I'm not sure what happened. I appreciate the help.D.J.Peters wrote:You have to use the "_" char to concatenate the splitted code line.
Joshyor write it in one line:Code: Select all
dim as string SrcStrChar,ElemStr If ((SrcStrChar >= "A" and SrcStrChar <= "Z") or _ (SrcStrChar >= "a" and SrcStrChar <= "z")) then ElemStr &= SrcStrChar End if
Code: Select all
dim as string SrcStrChar,ElemStr If ((SrcStrChar >= "A" and SrcStrChar <= "Z") or (SrcStrChar >= "a" and SrcStrChar <= "z")) then ElemStr &= SrcStrChar End if
Is the ampersand a typo in the statement?:
ElemStr &= SrcStrChar
-
- Posts: 564
- Joined: Sep 27, 2016 18:20
- Location: Valencia, Spain
Re: Compound conditional in IF statement
No, it is a valid operator.Is the ampersand a typo in the statement?:
ElemStr &= SrcStrChar
-
- Posts: 8586
- Joined: May 28, 2005 3:28
- Contact:
Re: Compound conditional in IF statement
I += 1 is the same as i = i + 1 add. two numbers
s &= "a" is the same as s = s & "a" is same as s = s + "a" concatenate two strings
Joshy
s &= "a" is the same as s = s & "a" is same as s = s + "a" concatenate two strings
Joshy
Re: Compound conditional in IF statement
Except that & converts the right hand side to a string before concatenation.s &= "a" is the same as s = s & "a" is same as s = s + "a" concatenate two strings
Code: Select all
dim as integer max = 12
dim as string s = "moo"
'print s + max ' can't do this
print s & max
-
- Posts: 8586
- Joined: May 28, 2005 3:28
- Contact:
Re: Compound conditional in IF statement
You are right I forgot it. :-)sancho3 wrote:Except that & converts the right hand side to a string before concatenation.
Joshy