I wrote this code to get a set of characters until a specific character was read. I know there’s a better way but just of curiosity.
It didn’t work. The loop continued endlessly.
Why is this ???
foreach (char myChar in monthYear)
{
do
{
whichMonth = whichMonth + myChar;
} while (myChar.ToString() == "-");
}
Here’s what you are telling the compiler to do…
So, given the string
02-2013, here is the execution path of your code.foreachcharacter in mymonthYearmyChar = '0'dowhichMonth = whichMonth + '0'while(myChar == '-')Isfalse, exitwhileloopmyChar = '2'dowhichMonth = whichMonth + '2'while(myChar == '-')Isfalse, exitwhileloopmyChar = '-'dowhichMonth = whichMonth + '-'while(myChar == '-')Istrue, continuewhileloopwhichMonth = whichMonth + '-'while(myChar == '-')Istrue, continuewhileloopwhichMonth = whichMonth + '-'while(myChar == '-')Istrue, continuewhileloopBecause of your while loop, it never actually goes to the next character when it hits ‘-‘. Instead, use a
breakstatement: