Flow Control C# Help

This section looks at the real nuts and bolts of the language: the statements that allow you to control the of your program rather than executing every line of code in the order it appears in the program.

Conditional Statements

Conditional statements allow you to branch your code depending on whether certain conditions are met or on the value of an expression. C# has two constructs for branching code – the if statement, which allows you to test whether a specific condition is met, and the switch statement, which allows you to compare an expression with a number of different values.

The If Statement

For conditional branching, C# inherits the C and C++ if … else construct. The syntax should be fairly intuitive for anyone who has done any programming with a procedural language:

if (condition)

If more than one statement is to be executed as part of either condition, these statements will need to be joined. together into a block using curly braces ({ … }). (This also applies to other C# constructs where statements can be joined into a block, such as the for and while loops):

The syntax here is similar to C++ and Java but once again different from Visual Basic. Visual Basic developers should note that C# does not have any statement corresponding to Visual Basic’s End If.
Instead, the rule is that each clause of an if contains just one statement. If you need more than one statement, as in the preceding example, you Should enclose the statements in braces, which will cause the whole group of statements to be treated as a single block statement.

If you want to, you can use an if statement without a final else statement. You can also combine else if clauses to test for multiple conditions:

There is no limit to how many else if s you can add to an if clause.

 You’ll notice that the previous example declares a string variable called input, gets the user to enter text at the command line, feeds this into input, and then tests the length of this string variable. The code . also shows how easy string manipulation can be in C#. To find the length of input, for example, use input .length. One point to note about if is that you don’t need to use the braces if there’s only one statement in the conditional branch:

However, for consistency, many programmers prefer to use curly braces whenever they use an if statement.

The if statements presented also illustrate some of the C# operators that compare values. Note in particular that, like C++ and Java, C# uses == to compare variables for equality. Do not use = for this purpose. A single = is used to assign values.

In C#, the expression in the if clause must evaluate to a Boolean. C++ programmers should be particularly aware of this; in C#, unlike in C++, it is not possible to test integer (returned from a function, say) directly. In C#, you have to convert the integer that is returned to a.Boolean true or false, for example, by comparing the value with zero or with null:

This restriction is there in order to prevent some common types of runtime bugs that occur in C++. In particular, in C++ it was common to mistype = when == was intended, resulting in unintentional assignments. In C# this will normally result in a compile-time error, because unless you are working with bool values, = will not return a bool.

The switch Statement

The switch … case statement is good for selecting one branch of execution from a set of mutually exclusive ones. It will be familiar to C++ and Java programmers and is similar to the Select Case statement in Visual Basic.

It takes the form of a switch argument followed by a series of case clauses. When the expression in the switch argument evaluates to one of the values and beside a case clause, the code immediately following the case clause executes. This is one example where you don’t need to use curly braces to join statements into blocks; instead, you mark the end of the code for each case using the break statement. You can also include a default case in the switch statement, which will execute if the expression evaluates to none of the other cases. The following switch statement tests the value of the integer A variable:

Note that the case values must be constant expressions; variables are not permitted.

Though the switch … case statement should be familiar to C and C++ programmers, C#’s switch … case is a bit safer than its C++ equivalent. Specifically, it prohibits fall-through conditions in almost all cases. This means that if a case clause is fired early on in the block, later clauses cannot be fired unless you use a goto statement to mark that you want them fired, too. The compiler enforces this restriction by flagging every case clause that is not equipped with a break statement as an error similar to this:

Control cannot fall through from one case label (‘case 2:’) to another

Although it is true that fall-through behavior is desirable in a limited number of situations, in the vast majority of cases, it is unintended and results in a logical error that’s hard to spot. Isn’t it better to code
for the norm rather than for the exception?

By getting creative with goto statements, however, you can duplicate fall-through functionality in your switch cases. But, if you find yourself really wanting to, you probably should reconsider your
approach. The following code illustrates both how to use goto to simulate fall-through, and how messy the resultant code can get:

There is one exception to the no-fall-through rule, however, in that you can fall through from one case to the next if that case is empty. This allows you to treat two or more cases in an identical way (without the need for goto statements):

One intriguing point about the switch statement in C# is that the order of the cases doesn’t matter – you can even put the default case first! As a result, no two cases can be the same. This includes different constants that have the same value, so you can’t, for example, do this:

The previous code also shows another way in which the switch statement is different in C# compared to C++: In C#, you are allowed to use a string as the variable being tested.

Posted on October 27, 2015 in C# Basics

Share the Story

Back to Top
Share This