Using Statements and Loops to Control Program Flow
This article provides the information needed to make logical decisions, iteratively execute a sequence of instructions, and modify the normal flow of control in programs. Although there is much more to C#, this article provides ample tools necessary to create useful, sophisticated programs.
For C++ and Java Programmers
Many of the statements in this article contain a Boolean expression for decision-making capability. A C++ program can interpret positive integers as true values; it does not work that way in C#. In C#, the Boolean expression must return a true or false Boolean value. It does interpret an integral value as being true or false.
if Statements
if statements allow evaluation of an expression and, depending on the truth of the evaluation, the capability to branch to a specified sequence of logic. C# provides three forms of if statements: simple if, if-then-else, and if-else if-else.
Simple if
A simple if statement takes the following form:
if (Boolean expression) [{] true condition statement(s) [}]
As expected, the Boolean expression must evaluate to either true or false. When the Boolean expression is true, the program performs the following true condition statements:
if (args.Length == 1) { Console.WriteLine("What is your pleasure, {0}?", args[0] ); }
WARNING
The curly braces are optional if there's only one action. It's usually a good practice to add them anyway. Their omission has been known to cause unexpected bugs.
if-then-else
The simple if statement guarantees only that you can perform certain actions on a true condition. It's either done or it's not. To handle both the true and the false conditions, use the if-then-else statement. It has the following form:
if (Boolean expression) [{] true condition statement(s) [}] else [{] false condition statement(s) [}]
This statement behaves the same as the simple if, except when the Boolean expression evaluates to false. Then the false condition statements in the else part are executed. Here's an example:
if (args.Length == 0) { Console.WriteLine("What is your pleasure, Master?"); } else { Console.WriteLine("What is your pleasure, {0}?", args[0]); }
if-else if-else
Sometimes it's necessary to evaluate multiple conditions to determine what actions to take. In this case, use the if-else if-else statement. Here's its general form:
if (Boolean expression) [{] true condition statement(s) [}] else if (Boolean expression) [{] true condition statement(s) [}] . . . else if (Boolean expression) [{] true condition statement(s) [}] else [{] false condition statement(s) [}]
In a sequential order, each statement, beginning with if and continuing through each else if, is evaluated until one of its Boolean expressions evaluates to true. The dots indicate possible multiple else if blocks. There can be any number of else if blocks required.
Once one of the Boolean expressions evaluates to true, the true condition statements for that if or else if are executed, and then flow of control transfers to the first statement following the entire if-else if-else structure.
When none of the Boolean expressions evaluates to true, the false condition statement(s) of the else section is executed. Here's an example:
if (args.Length == 0) { Console.WriteLine("What is your pleasure, Master?"); } else if (args.Length == 1) { Console.WriteLine("What is your pleasure, {0}?", args[0]); } else { Console.WriteLine("Too many arguments!\a"); }
It's permissible to include any valid statement inside an if, else if, or else statement block. If necessary, add another if statement. Here's an example:
if (args.Length == 0) { Console.WriteLine("What is your pleasure, Master?"); } else if (args.Length == 1) { if (args[0] == "Joe") Console.WriteLine( "What is your pleasure, {0}?", "Master"); else Console.WriteLine( "What is your pleasure, {0}?", args[0]); } else { Console.WriteLine("Too many arguments!\a"); }
if statements excel at decisions involving evaluation of dynamic runtime calculations and relational expressions. The following example shows how to evaluate a wide range of values:
if (waterTemp <= 0) { Console.WriteLine("Solid"); } else if (waterTemp > 0 & waterTemp < 100) { Console.WriteLine("Liquid"); } else { Console.WriteLine("Gas"); }