Local variables and If./then/else
BlitzMax Forums/BlitzMax Beginners Area/Local variables and If./then/else
| ||
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 |
| ||
no should print 5 if the name = "Bob" |
| ||
A block of code is normally a function or a method. An if/then/else statement doesn't constitute a block of code. |
| ||
...Double post?... ...Me?... ...Never! |
| ||
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] |
| ||
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? |
| ||
Should I always read the documentation as if I was using 'strict'? Your guess is as good as mine. ;o) |
| ||
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. |
| ||
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. |
| ||
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. |
| ||
See this as well:Strict Local a = 1 If True Local a = 2 Print a EndIf Print a |