Nov 5, 2011 at 3:59pm UTC
I'm fairly new to programming, so I really only know basics. But I'm trying to make an if statement that basically says:
If this variable doesn't have a value
then assign it a value
Can anyone tell me how to do this?
Nov 5, 2011 at 4:00pm UTC
A variable always has a value.
At most, you can define some state (like 0 or -1 for an integer) that you define as "no value".
Nov 5, 2011 at 4:11pm UTC
Wait, but i thought if you declare a variable like
int i;
it wont have a value until you initialize it
Nov 5, 2011 at 4:17pm UTC
That is incorrect. It will have a value. It takes up some memory and that memory has some numbers in it. Those numbers could be anything at all. Those numbers are the value.
Nov 5, 2011 at 4:20pm UTC
It gives me a warning saying that an uninitialized variable 'i' is being used...
Nov 5, 2011 at 4:21pm UTC
As it should. i does have a value, it's just unspecified which one (it can be some random value).
Just try printing it.
Nov 5, 2011 at 4:23pm UTC
Uninitialized means you have not set a value.
int i; // No value set
See? No value has been set. It could be anything.
i = 7;
Now a value has been set. The value 7.
Nov 5, 2011 at 4:23pm UTC
Hmm... ok, thanks for the help :D