Computer Science Canada

no value variables

Author:  koolkid98 [ Mon May 29, 2006 1:08 pm ]
Post subject:  no value variables

When a variable has no value the program stops, so is there a way to output something when a variable has no value?

Author:  MysticVegeta [ Mon May 29, 2006 1:23 pm ]
Post subject: 

I know in java you can do this with "null", dont know about turing, nil has a different purpose, I dont think you can do that. Do you mean like as in:

if a = <command for no value> then
%ouput something
end if

?

Author:  Albrecd [ Mon May 29, 2006 1:26 pm ]
Post subject: 

It sounds like you're trying to figure out where the variable has no value.
Turing should highlight the line where it trys to use the variable and fails, but if you want to find where the variable becomes valueless, you could start by having Turing "put VarName" after every time the variable is changed, then, turing will find the "Variable had no value" error in the put statement after your variable error occurs.

Author:  Delos [ Mon May 29, 2006 1:28 pm ]
Post subject:  Re: no value variables

koolkid98 wrote:
When a variable has no value the program stops, so is there a way to output something when a variable has no value?


The programme stops for good reason. Uninitialized variables are a result of buggy coding. You should never have an instance where this occurs. As for MysticVegeta's suggestion of using 'null', that won't work here. Null in Turing is limited to objects of a class.
In short - check your code and ensure that all variables are initialized. If you still need the programme to stop at certain points due to variables having blank values (think var my_string : string := "") then you could use an Error.Halt().

Author:  koolkid98 [ Mon May 29, 2006 1:50 pm ]
Post subject: 

Yes mystic, i need to know that command

Author:  Albrecd [ Mon May 29, 2006 1:57 pm ]
Post subject: 

Instead of using nil variables, could you not assign the variable something at the beginning of the program (as Deloes said) and then if the variable still equals the origional value instead of something new, do whatever you would have done if the variable were nil?

Author:  koolkid98 [ Mon May 29, 2006 2:53 pm ]
Post subject: 

Albrecd wrote:
Instead of using nil variables, could you not assign the variable something at the beginning of the program (as Deloes said) and then if the variable still equals the origional value instead of something new, do whatever you would have done if the variable were nil?


but thats the problem, I dont want to do it that way, I need to make it output something when the variable has no value.

Author:  Cervantes [ Mon May 29, 2006 4:07 pm ]
Post subject: 

You can't. Declaration of a variable and initialization (assignment) of a variable are two totally distinct tasks. When you declare a variable, it is not initialized to some specific variable. It's not given a value at all. It just allocates a space in memory for that variable.

Perhaps you should detail exactly why you're trying to do this (include some code, perhaps) so we can suggest an alternative approach.

Author:  Clayton [ Mon May 29, 2006 9:19 pm ]
Post subject: 

if you want to stop your program from crashing for having no value, you could do something like this
code:

var sum,total,product:int:=0
%is not the same as
var sum,total,product:int
%for strings
var name,address:string:=""
%is not the same as
var name,address:string

declaring your variables to some neutral value (ie for strings "") is a good idea as it avoids unecessary frustration


: