clarification of 'IF' - solved

Forum for discussion about the documentation project.
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

clarification of 'IF' - solved

Post by speedfixer »

Code: Select all

dim as boolean a
'a = true
if a then print "5" : print "again"
The [print "again"] is part of the if statement.
In the -lang fb and -lang fblite dialects, if there is a new line, a single-line comment ('), a colon (:), or a Rem statement directly after THEN, then the IF will be multi-line. Any other statement will result in a single-line IF.
(bold:mine)

The word "directly" is unambiguous. The colon is NOT directly after THEN.
Multiline IF requires an END IF to terminate.

I read this with the implication that any valid FB syntax directly behind the THEN that is NOT a statement would trigger the multiline.

Is this a poor choice of words, a misunderstanding on my part, a parser fail, an undocumented feature?

I have searched for a language syntax guide to describe the function seperator : but could not find one. Is there one?
The only reference to using a colon to 'stacking' statements on one line is in the https://www.freebasic.net/wiki/ProPgExp ... Statements page.
I found the IF tutorial by rdc, and I do find this usage in an example of his.

I'm not bothered by this. I saw someone else's use of this pattern in the forum and was puzzled. I had to test it to be sure what was happening, and the IF doc implies this should not work. I think.

I don't want to be seen attacking the wording in the manual.
I just wonder if others might see it this way, also. If so, then some tiny change in wording would be appropriate.

At least, an added SIMPLE example on the IF page to show this usage would provide a reference if someone else has the same puzzlement.


david
Last edited by speedfixer on Jan 20, 2021 19:51, edited 1 time in total.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: clarification of 'IF'

Post by fxm »

I do not see where the problem is.
The word 'directly' is pretty clear I think.

In your example, it is 'PRINT' that is 'directly' after 'THEN', so a multi-line 'IF' structure is not required according to the documentation, but on the contrary a single-line 'IF' structure.
This is indeed the case, because your example works.
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: clarification of 'IF'

Post by counting_pine »

Would the word "immediately" be clearer?

For illustration purposes, here is a multi-line IF, with a colon immediately after the THEN:

Code: Select all

dim as boolean a
'a = true
if a then : print "5" : print "again" : end if
Or as a more readable multi-line IF using linebreaks:

Code: Select all

dim as boolean a
'a = true
if a then
  print "5"
  print "again"
end if
I'd say the main reason we decided a colon should cause a multi-line IF is so that single-line macros created with #DEFINE can incorporate self-contained IF blocks:

Code: Select all

#define truth(a) if (a) then : print #a & " is true": else : print #a & " is false" : end if

dim as boolean a, b = true
if b then truth(a): print "and b is true"
(Forgive the contrived example.)

If you were to include a single-line IF into a macro, then the effects of the IF block would "leak" into other things on the line after it.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: clarification of 'IF'

Post by fxm »

Depending on the dialect, so many variations in syntax are allowed that it is difficult to be concise, precise and exhaustive at the same time in documentation.
Moreover, I think the biggest ambiguity comes rather from the vagueness of the "multi-line IF" or "single-line IF" naming, knowing that a syntax originally "multi-lines IF" can be compacted (in some cases) on a single line of code by using colons.
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: clarification of 'IF'

Post by speedfixer »

I accept your explanations. I don't have a large problem with doc page, interpretation, usage, etc.

I simply could not find something that suggested this was valid - other than the fact that it worked. When I noticed it in a user's code, I looked for validation.

As I said, just a VERY simple example added would have made clear: this is a valid syntax pattern.

david
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: clarification of 'IF'

Post by fxm »

Otherwise, OK to replace 'directly' with 'immediately'.

Rather than delving endlessly (see already viewtopic.php?f=9&t=24977) into the IF...THEN page, I would prefer your strengths to all of you to focus on the 2 remaining Programmer's Guide pages to complete / write:
Executables
and
External Graphics File Formats
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: clarification of 'IF'

Post by speedfixer »

I don't think any simple change will improve understanding.
Your reference to the past post only underscores and expands my concerns.

I see don't an easy answer.

But, as I say, though a description might be imperfect and acceptable, a SIMPLE working example can be clear.

Please - most simple possible - nothing extra added.

david
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: clarification of 'IF'

Post by speedfixer »

@ counting_pine:

A few words, almost, but not related.

'Immediately' and 'directly' both imply: 'next'. That just how we use those words.

Its like 'adjacent' and 'concurrent' and 'contemporaneous.'

All mean the same thing: very near to each other, or at the 'same' time (in whatever relative timeframe being discussed.)

While NONE of them actually state a relationship exists between them, they are typically used to imply a relationship.
Picky. By logic only, any statement with one of those words should just be ignored in an argument. We try to be concise in a 'definition' in a programming language. But, I think the terms of the contract I signed when I bought my copy of FreeBASIC are satisfied.

Words aren't perfect. Some concepts are not easy to express in just a few words. That's OK, and I think true in this case.
fxm tries hard to document FB; he does a good job; it isn't perfect; it is not his first language; I don't think I can do better unless I write a book about it. I just wanted some clarification, so: I'm happy.

david
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: clarification of 'IF'

Post by fxm »

Would it be really useful to point out with such examples as for example:

1)

Code: Select all

dim a as integer
'a = 1
if a = 1 then [:] print "1" [:] [end if]
is valid whatever the dialect

2) but:

Code: Select all

dim a as integer
'a = 1
if a = 1 then print "1"
end if
is always invalid

3)while:

Code: Select all

dim a as integer
'a = 1
if a = 1 then : print "1"
end if
is invalid only for "qb" dialect

and so on...
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: clarification of 'IF'

Post by speedfixer »

My code was not an example, just code to use for testing to display my question.

A most simple example might be:
Example of single line IF, extended with colon(:)

Code: Select all

dim as integer a, b, c
a = 1
b = 1
c = 0
if a = b then c = a + b: print "equality and assignment"
print
print "c = "; c
robert
Posts: 169
Joined: Aug 06, 2019 18:45

Re: clarification of 'IF'

Post by robert »

deleted
Last edited by robert on Jan 20, 2021 7:24, edited 1 time in total.
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: clarification of 'IF'

Post by speedfixer »

I think you are seeing something that isn't there.

fxm works hard - he does a good job - I applaude his efforts
nobody is perfect
For this case, it would be very hard to explain in any reasonable small amount of words exactly the possible syntax for this kind of pattern of usage.
What exists is OK.
His effort is better than any I could make with these few words.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: clarification of 'IF'

Post by jj2007 »

Robert,

One could interpret that phrase in a certain way, half joking maybe, but if I were fxm, I would feel honoured by David's words. Don't worry, fxm will not be offended ;-)
robert
Posts: 169
Joined: Aug 06, 2019 18:45

Re: clarification of 'IF'

Post by robert »

speedfixer wrote:I think you are seeing something that isn't there.

fxm works hard - he does a good job - I applaude his efforts
nobody is perfect
For this case, it would be very hard to explain in any reasonable small amount of words exactly the possible syntax for this kind of pattern of usage.
What exists is OK.
His effort is better than any I could make with these few words.
Hi david:

O.K. Thanks for the response. I will delete my post.

Robert
robert
Posts: 169
Joined: Aug 06, 2019 18:45

Re: clarification of 'IF'

Post by robert »

deleted
Last edited by robert on Jan 20, 2021 7:25, edited 1 time in total.
Post Reply