0

The questions is a lot more specific than the title actually. I'm just going through some C# and I come across an error that I managed to fix but I don't know why.

Why does my do-while loop give me an error when the string power is defined in the loop?

It's all fine and dandy though when I define my string power before the do-whiel loop

        String power;

        do
        {
            Console.WriteLine("Enter values X & Y for X+Y");

            Console.WriteLine("Your value of X is  ");
            int numOne = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine(" Your value of Y is   ");
            int numTwo = Convert.ToInt32(Console.ReadLine());


            Console.WriteLine("Your Sum is {0}", numOne+numTwo);


            Console.Write("Turn calculator on or off?    ");
            power = Console.ReadLine();

        } while (power == "on" || power =="On" || power =="ON");
    }
}

}

This ends up running fine.

But the following example results in an error at the while statement about the name "power" not existing in the current context.

     do
        {
            Console.WriteLine("Enter values X & Y for X+Y");

            Console.WriteLine("Your value of X is  ");
            int numOne = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine(" Your value of Y is   ");
            int numTwo = Convert.ToInt32(Console.ReadLine());


            Console.WriteLine("Your Sum is {0}", numOne+numTwo);


            Console.Write("Turn calculator on or off?    ");
            String power;

            power = Console.ReadLine();

        } while (power == "on" || power =="On" || power =="ON");
    }
}
ajdawg
  • 17
  • 1
  • 1
    Consider the curly braces { and } as a scope of local variables. In the while condition in the second case power is local to the body of the while statement's scope. – PepitoSh Jul 10 '18 at 04:16
  • 3
    `while` itself is out of the scope of `do`. That's it can't access power defined inside `do`. – qxg Jul 10 '18 at 04:16
  • 1
    What about `oN`? Alternatively, you could use `power = Console.ReadLine().ToLower()` and `while (power == "on")`. – paxdiablo Jul 10 '18 at 04:21

1 Answers1

3

{ .......... } creates a scope. Defining inside of the loop defines it in a deeper scope than the while (power...) part.

Basically, you can see less deeply nested scopes (an if's scope can see the method scope, a method scope can see the class scope, a class can see the namespace scope, etc.

See here for more information on scoping.

Another example:

public void A()
{
    string power = "test";

    {
         // this works because power is defined in a less deep scope.
         Console.WriteLine(power);
    }
}

public void B()
{
    {
        string power = "test";
    }
    // this doesn't work because power is defined in a deeper scope
    Console.WriteLine(power); 
}

The "context" the error message refers to is: the current scope and everything above it.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86