Question:
C# programming question?
anonymous
2012-02-20 20:03:12 UTC
I'm having trouble understanding this code...


using System;

public class Craps
{
private static Random randomNumbers = new Random();

private enum Status { CONTINUE, WON, LOST }

private enum DiceNames
{
SNAKE_EYES = 2,
TREY = 3,
SEVEN = 7,
YO_LEVEN = 11,
BOX_CARS = 12
}

public static void Main()
{
Status gameStatus = Status.CONTINUE;
int myPoint = 0;

int sumOfDice = RollDice();

switch ((DiceNames)sumOfDice)
{
case DiceNames.SEVEN:
case DiceNames.YO_LEVEN:
gameStatus = Status.WON;
break;
case DiceNames.SNAKE_EYES:
case DiceNames.TREY:
case DiceNames.BOX_CARS:
gameStatus = Status.LOST;
break;
default:
gameStatus = Status.CONTINUE;
myPoint = sumOfDice;
Console.WriteLine("Point is {0}", myPoint);
break;
}

while (gameStatus == Status.CONTINUE)
{
sumOfDice = RollDice();

if (sumOfDice == myPoint)
gameStatus = Status.WON;
else
if (sumOfDice == (int)DiceNames.SEVEN)
gameStatus = Status.LOST;
}
if (gameStatus == Status.WON)
Console.WriteLine("Player Wins\n");
else
Console.WriteLine("Player Loses\n");
}

public static int RollDice()
{
int die1 = randomNumbers.Next(1, 7);
int die2 = randomNumbers.Next(1, 7);

int sum = die1 + die2;

Console.WriteLine("Player rolled {0} + {1} = {2}", die1, die2, sum);
return sum;
}

} // end


If myPoint is not initialized upon declaration, the compiler says "use of unassigned local variable" in "if (sumOfDice == myPoint)"...

However the while condition is only true if gameStatus == CONTINUE, which means that myPoint was given a value in the default switch statement. So my question is why does it work only when myPoint is initially given a value (doesn't matter what that value is)?


Thanks
Three answers:
Ratchetr
2012-02-20 20:56:17 UTC
Because compilers don't try all *that* hard to prove that your logic is right. They don't do deep deductive reasoning, because it takes too long. They do what they can do quickly to catch common programming errors, but they don't do in-depth analysis to verify that they are right 100% of the time.



In the bad old days (Think...C) compilers didn't even try. You could declare myPoint at the top of your function, forget to assign it a value in the default case, and use it in your while loop. The compiler was mum. Your code compiled, but the results were almost always wrong. But programmers are sloppy. They tend to make that sort of mistake. So the compiler guys decided to get clever and try to issue a warning (at first) if they could detect that scenario. Is some languages, (e.g. C# and Java) the warning isn't a warning at all, it's an error.



But it's not an easy scenario to spot *with certainty*. A compiler will go about so far as to say: Hum, myPoint isn't initialized when it is declared. There are paths through the switch statement that comes next that don't assign a value to myPoint. There is code after that in a while loop that uses myPoint. That looks wrong. myPoint might not have been assigned a value. That's about as compilers get. They don't try to go on and prove that each path through the switch statement that doesn't assign a value to myPoint has a side effect that will prevent the while loop from ever executing. That would be hard ;-). So, the compiler gives in, issues the warning or error, and assumes you will look at the code figure it out. (Which you did, with the = 0 bit.).



So..yeah, it's a bogus message. It happens. But it's much better than the bad old days when the compiler didn't even try. You had to debug those problems at run time. That's harder.
nojthebrat
2012-02-20 20:39:58 UTC
This is compiler logic, technically it could be impossible for a variable to not have a value like this



int myint;

int a = 1;

if a = 1 { myint = 1 }

if myint = 1.....



But the compiler will see that its only 'possible' under a condition that it will. therefore it generates an error. you will probably run into many similar problems you just have to account for things that will never happen sometimes.



In the case of your code mypoint would only have a value (if not given on initialization) if your default runs in the switch statement.



What you declare a variable to does not matter if its not going to get used or if it will get changed for those same reasons. I usually set them too -1, because i use 0's a lot and i can immediatly identify which is which when i see them.



For return values in functions I would use specific value for error handling as sometimes your function will return a value within a if statement, but you must have one declared out of it for the same reasons.
?
2016-12-09 13:03:14 UTC
no longer anymore, at one time C++ became only an extension of C. considering the fact that then, C has replaced, and so has C++. besides the undeniable fact that that being pronounced it is common for a similar application to gather the two C and C++. the tactic is a similar, yet there are purely some transformations int he libraries which you like the compiler to link to.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...