Compound conditional in IF statement

New to FreeBASIC? Post your questions here.
Post Reply
Flyzone
Posts: 109
Joined: Nov 17, 2017 17:39

Compound conditional in IF statement

Post by Flyzone »

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'.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Compound conditional in IF statement

Post by D.J.Peters »

You have to use the "_" char to concatenate the splitted code 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
or 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
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Compound conditional in IF statement

Post by Munair »

In most BASIC languages there is no character to mark the end of a statement, like for example in Pascal that uses the semicolon:

Code: Select all

if a > b then a := b;
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.
Flyzone
Posts: 109
Joined: Nov 17, 2017 17:39

Re: Compound conditional in IF statement

Post by Flyzone »

D.J.Peters wrote:You have to use the "_" char to concatenate the splitted code 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
or 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
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.
Flyzone
Posts: 109
Joined: Nov 17, 2017 17:39

Re: Compound conditional in IF statement

Post by Flyzone »

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:

Code: Select all

if a > b then a := b;
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.
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".
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!
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Compound conditional in IF statement

Post by MrSwiss »

Flyzone wrote:Yes, I'd actually prefer a closing statement char
Well, this is BASIC, which never had such a thing.
You'll have to use C or Pascal, if you're insisting on a closing statement.
Flyzone wrote:How about a question mark or ...
No go, the question mark is a synonym for 'Print' statement, in FB.
See FB-Manual, for more detailed information ...
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Compound conditional in IF statement

Post by grindstone »

IMHO it considerably eases the typing of the FB source code that there's no closing statement.
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Compound conditional in IF statement

Post by Munair »

grindstone wrote:IMHO it considerably eases the typing of the FB source code that there's no closing 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 ?!?!!
Flyzone
Posts: 109
Joined: Nov 17, 2017 17:39

Re: Compound conditional in IF statement

Post by Flyzone »

Flyzone wrote:
D.J.Peters wrote:You have to use the "_" char to concatenate the splitted code 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
or 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
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.


Is the ampersand a typo in the statement?:
ElemStr &= SrcStrChar
Josep Roca
Posts: 564
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: Compound conditional in IF statement

Post by Josep Roca »

Is the ampersand a typo in the statement?:
ElemStr &= SrcStrChar
No, it is a valid operator.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Compound conditional in IF statement

Post by D.J.Peters »

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
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Re: Compound conditional in IF statement

Post by sancho3 »

s &= "a" is the same as s = s & "a" is same as s = s + "a" concatenate two strings
Except that & converts the right hand side to a string before concatenation.

Code: Select all

dim as integer max = 12
dim as string s = "moo"
'print s + max   	' can't do this
print s & max
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Compound conditional in IF statement

Post by D.J.Peters »

sancho3 wrote:Except that & converts the right hand side to a string before concatenation.
You are right I forgot it. :-)

Joshy
Post Reply