Why is there not 'Exit If' ?

New to FreeBASIC? Post your questions here.
Post Reply
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Why is there not 'Exit If' ?

Post by Tourist Trap »

In the below example , since one statement is true the next have not to be tested, so early exiting would be an option. Is there a reason why it is not possible with IF statement ? and with it only in fact ..

Code: Select all

	
Dim As Integer X, Y, Z, U

If  X > Z And Y > Z Then
 	 U = 0
	 'Exit if                                 '****  would not compile (err250)
    ElseIf X > Z Then
	 U = 1
	 'Exit If
    Else
	 U = 2
EndIf

Code: Select all

"error 250: Expected 'CONSTRUCTOR', 'DESTRUCTOR', 'DO', 'FOR', 'FUNCTION', 'OPERATOR', 'PROPERTY', 'SELECT', 'SUB' or 'WHILE', found 'if' in 'Exit if"
Roland Chastain
Posts: 993
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Why is there not 'Exit If' ?

Post by Roland Chastain »

Tourist Trap wrote:since one statement is true the next have not to be tested
It isn't tested! This is what "else" means. Your "exit if" wouldn't change anything to the current behaviour.
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Why is there not 'Exit If' ?

Post by dkl »

In If blocks, after entering one block's body because its condition was true, the remaining conditions/blocks are already not going to be visited anymore. It skips immediately to the End If (it should anyways, if not, that would be a bug).

Why isn't Exit If supported? Well, seems like here it's generally regarded bad practice. There typically are better ways to do it without using Exit If. That goes together with the general attitude against Goto's. There was some discussion of this here in the past, for example:
http://www.freebasic.net/forum/viewtopi ... =3&t=12660
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Why is there not 'Exit If' ?

Post by Tourist Trap »

Roland Chastain wrote:It isn't tested! This is what "else" means. Your "exit if" wouldn't change anything to the current behaviour.
Ok, I was using ElseIF , that's why I thought it was like chaining multiple If lines. Which would look like a select..case with no 'selecting variable'.

Code: Select all

If cond1 then 
   A
ElseIf cond2 then 
   B
Else 
  C
End If

Elseif Select
  Case cond1 : A
  Case cond2 : B
  Case neither cond1 nor cond2 : C
End 
dkl wrote:In If blocks, after entering one block's body because its condition was true, the remaining conditions/blocks are already not going to be visited anymore. It skips immediately to the End If (it should anyways, if not, that would be a bug).

Why isn't Exit If supported? Well, seems like here it's generally regarded bad practice. There typically are better ways to do it without using Exit If. That goes together with the general attitude against Goto's. There was some discussion of this here in the past, for example:
http://www.freebasic.net/forum/viewtopi ... =3&t=12660
Thanks . I'm reading this very interesting link by now.
Post Reply