How is the coverage factor in the "do ... at that time" cycle and vice versa?

C # Question about homework: I just added some “play again” logic using a do-while loop. This was my original code:

namespace demo
{
    class Program
    {
        static void Main(string[] args)
        {
            Info myInfo = new Info();
            myInfo.DisplayInfo("Daniel Wilson", "4 - Hi-Lo Game");
               // I moved  String playAgain = "N"; to here 
            do
            {
                DWHiLowUI theUI = new DWHiLowUI();
                theUI.Play();
                String playAgain = "N";
                Console.WriteLine("Enter 'Y' to play again, any other key to exit.");
                playAgain = Console.ReadLine();
            }
            while ((playAgain == "Y")||(playAgain =="y"));
            Console.ReadLine();
        }
    }
}

Which gave me an error:

Error   7   The name 'playAgain' does not exist in the current context

I moved String playAgain = "N";to the string ABOVE my do(see comment) and it worked fine.

I'm trying to figure out exactly what I did to fix it. This seems to be a problem with the scope, but it seems to me that defining a variable inside a loop may possibly pass it to the end of the loop. I looked through my tutorials and there is nothing about scope related to loops. This would suggest to me that the scope within the loops is not a problem, but it behaves as if it were a scope problem. I confuse myself thinking about it.

, do...while . , . , , ?

+5
3

, . # , , , (, {}), ( ).

playAgain , , while.

+7

, . do..while , .

// outer scope
do 
{
    // inner scope
} while (/*outer scope*/);
// outer scope
+5

. , , , , , , playAgain, .

+2

All Articles