Very noob question...

BlitzMax Forums/BlitzMax Beginners Area/Very noob question...

Takis76(Posted 2013) [#1]
Hello,
I have one very noob question but it seems much different than other BASIC languages.

I have one if statement and I would like to exit this if statement.


If something=1 then 
do something
Endif
If something=2 then 
do something else
Endif


Usually exit,or continue or break used to escape from one if statement.
There is no break command to escape from some if statement and the continue is used for loops
Also the exit needs a label.
I try to void the goto command
Return command used in gosub.

So which command escapes from one if statement or which exits from a function immediately?


If have my code for example:

Function Draw_Backdrop()

		
	If Wallset = "02"
		If Flip_Backdrop = 1 Then
		DrawImage(Backdrop02a, 20, 73, 0)
		Flip_Backdrop = 0
		exit <== exit or or break or exit function doesn't works
		EndIf
		If Flip_Backdrop = 0 Then
		DrawImage(Backdrop02b, 20, 73, 0)
		Flip_Backdrop = 1
		exit <== exit or or break or exit function doesn't works
		EndIf
	End If

	
End Function



Thank you very much


ImaginaryHuman(Posted 2013) [#2]
Exiting the function immediately is done with 'Return'.

You can code around the need to stop processing.. In your example you don't even need an exit because as soon as the EndIf is hit the flow is going to just end and exit from the function anyway. But if there were code below it that you want to skip, then set a variable like `NeedToExit=true` and then wrap `If NeedToExit=false` around the code that come safter it.


Takis76(Posted 2013) [#3]
Exit seems was escaped from my if statement , return used to be for gosubs.
So exit is used for exit gosubs and if statements.
Thank you very much, why do they removed the break command which was the most related command to do these things?


Yasha(Posted 2013) [#4]
'Exit' is break. It doesn't escape from If statements, only from loops. (There is no way to jump out of an If block - if you need to do this it means you need to refactor, either put the code into different functions or guard it with another condition check.)

There are no Gosubs in BlitzMax at all, and there never were.


Henri(Posted 2013) [#5]
Hello,

Select-statement would suit better in your example like:

Strict

Global Wallset:String		= "02"
Global Flip_Backdrop:Int	= 1

Draw_Backdrop()

Function Draw_Backdrop()

	If Wallset = "02"
		Print "Wallset is 02"
		
		Select Flip_Backdrop
		
		Case 1
			Print "Flip_Backdrop is 1"
			'DrawImage(Backdrop02a, 20, 73, 0)
			Flip_Backdrop = 0
			
		Case 0
			Print "Flip_Backdrop is 0"
			'DrawImage(Backdrop02b, 20, 73, 0)
			Flip_Backdrop = 1
			
		Default
			Print "Flip_Backdrop default action"
		EndSelect
	Else
		Print "Wallset was not 02"
	End If
	
End Function


-Henri


xlsior(Posted 2013) [#6]
Note that there's also an ELSEIF statement:

IF a=1 then
   'a=1, do stuff
ELSE IF a=2 then
   'a=2, do stuff
ELSE IF a=3 then
   'a=3, do stuff
ELSE IF a=4 AND b=4
   ' a=4 AND b=4, do stuff
   Print "Both!"
ELSE
   print "None of the above!"
END IF



Takis76(Posted 2013) [#7]
Return command was worked in my code.
I am not using select command much.
Do select statement doesn't needs any return command? (If you want to break it immediately)


Hardcoal(Posted 2013) [#8]
Select doesnt need return its not like gosub.
You are living in the old world.
Forget gosub. For that you have functions.
Select is a set of conditions like if then but probably faster in some cases (not sure)
And much easier to the eye.

Make some trail and errors its the best way to learn


Henri(Posted 2013) [#9]
There is no "break"-command for "select"-block like in C-language (of course, one
could arque that it isn't needed as only one condition can be true and performance
gain is minimal), but when you are inside function you can return (break) from that
function by using Return. As a bonus, return can have a value which you can receive like:
Strict

Print GetNumber()
Print GetText()

Function GetNumber:Int()
	Return 10
EndFunction

Function GetText:String()
	Return "Hello there!"
EndFunction

As for "Exit"-command it is used to end the loop prematurely when some condition is met like:
Strict

Print GetNumber()

Function GetNumber:Int()
	Local i:Int=0
	
	Repeat
		If i=20 Then Exit
		i:+1
	Forever
	
	Return i
EndFunction


-Henri


Hardcoal(Posted 2013) [#10]
There is no break in select (as in c and flash) because mark is not stupid like all the rest.
Thanks for that. I would gladly let him run the world which he probably wont want.

Return having a value isnt a bonus. Its a neccesity of a function


Takis76(Posted 2013) [#11]
Guys your information were very valuable and thank you very very much.
You are helping me to create a fantastic game here.