Local variables and If./then/else

BlitzMax Forums/BlitzMax Beginners Area/Local variables and If./then/else

tonyg(Posted 2005) [#1]
Documentation states that Local variables are assigned to a 'block' of code which includes if/then/else statements.
Can anybody give an example of this in action?
I might have missed something but shouldn't this print '0'?
Local x = 5
Local name$="Bob"
If name$="Bob"
   Print x
Else
   x=x*2
   Print  x
EndIf



boomboommax(Posted 2005) [#2]
no should print 5 if the name = "Bob"


Shambler(Posted 2005) [#3]
A block of code is normally a function or a method.

An if/then/else statement doesn't constitute a block of code.


Yan(Posted 2005) [#4]
...Double post?...
...Me?...
...Never!


Yan(Posted 2005) [#5]
In non strict mode, local scope boundaries apply only to methods and functions.
In strict mode, local scope applies to methods, functions and any loop or decision block.
Local a=10, c=30

If a
  Local b = 20
  Print c
EndIf

Print b
strict

Local a=10, c=30

If a
  Local b = 20
  Print c
EndIf

Print b ' And...Crash!
[edit]
This only applies to variables created within the code block.
[/edit]


tonyg(Posted 2005) [#6]
Thanks TwoeyedPete, it makes sense.
Should I always read the documentation as if I was using 'strict'?
Shambler, does your documentation under 'Variables / Local Variables' state something different as mine specifically lists if/then/else as a block of code?


Yan(Posted 2005) [#7]
Should I always read the documentation as if I was using 'strict'?
Your guess is as good as mine. ;o)


Shambler(Posted 2005) [#8]
In my docs Local is described...

Rem
Local defines a variable as local to the Method or Function it is defined meaning it is automatically released when the function returns.
End Rem

Weird how strict completely changes the behaviour of the compiled program...will have to watch out for that one it's a nasty pitfall lol.


tonyg(Posted 2005) [#9]
Ahhh, OK.
Under Help / Language / Variables / Local Variables it adds if/then/else to the list along with loops (I imagine for/next, while/wend and repeat/until are included) and
case/select.


FlameDuck(Posted 2005) [#10]
An if/then/else statement doesn't constitute a block of code.
You indent it, don't you? An if..else construct does qualify as a block, and scopeing rules do in fact apply.

(I imagine for/next, while/wend and repeat/until are included) and case/select.
Yes. Anything you would normally indent is usually considered a "block" or rather compound statement, in C and Java these are usually surrounded by { and }, but most people here prefer not having such visual cues in their code.


Beaker(Posted 2005) [#11]
See this as well:
Strict
Local a = 1
If True
	Local a = 2
	Print a
EndIf
Print a