Get reliable answers to your questions at Westonci.ca, where our knowledgeable community is always ready to help. Get immediate answers to your questions from a wide network of experienced professionals on our Q&A platform. Our platform offers a seamless experience for finding reliable answers from a network of knowledgeable professionals.

True or false, cases must use curly braces like other test condition statements
A. true
B. false

Sagot :

Answer:

False

Explanation:

You can use curly braces to scope everything that's inside of the case body, but they are not necessary.

There are cases where you will need explicit bodies for cases:

If you have two different cases that both contain a variable called 'x', you will need to scope one or both of the cases.

What is scoping?

Scoping in a language defines where variables can be accessed, or referenced.

int i = 0;

// First scope

{

int x = i + 10;

}

// Second scope

{

int j = x + 2;

}

Console.WriteLine(j);

There are a few errors in this code.

In the second scope, we try to use the variable 'x' which was defined in the first scope, but we can't find that variable because it is defined in a scope that cannot be reached by the second scope. The same goes with the Console.WriteLine(j); line, we try to access a variable that is not defined in the current scope, and is instead defined in a nested scope.

In most languages, scoping is essentially a stack, where the lower nested scopes can access variables defined in the previous scope, but not the other way around.

So now you know a little bit about scopes; when to use them in case bodies, and that you do not have to use a body for a case if it's not necessary.