CSharp Basic Questions and Answers

What statements are generally used for conditional branching?
if, else, switch.

True or false: an if statement's condition must evaluate to an expression.
False. In C#, an if statement's condition must evaluate to a Boolean expression.

Why should you use braces when there is only one statement following the if?
The braces make maintenance easier. If you add a second statement later, you are less likely to create a logic error because it is obvious what "block" of statements the if refers to.

What kind of expression can be placed in a switch statement?
Either a numeric value or a string.

True or false: you can never fall through in a switch statement.
False. If the statement has no body, then you can fall through. For example:

case morning:
case afternoon:
someAction( );

Name two uses of goto.
Two uses of goto are:
To go to a label in your code
To go to a different case statement in a switch statement

What is the difference between while and do...while?
do...while evaluates its condition is at the end of the statement rather than at the beginning, and thus is guaranteed to run at least once.

What does the keyword continue do?
In a loop, it causes the remainder of the body of the loop to be skipped and the next iteration of the loop to begin immediately.

What are two ways to create a loop that never ends until you hit a break statement?
Two ways of creating an infinite loop are:
for (;;)

No comments: