Beware that using the Parse()
method may trigger an exception, as demonstrated here:
string str = '5a';
int num = int.Parse(str); //---format exception---
This statement causes a format exception to be raised during runtime because the Parse()
method cannot perform the conversion. A safer way would be to use the TryParse()
method, which will try to perform the conversion. It returns a false
if the conversion fails, or else it returns the converted value in the out
parameter:
int num;
string str = '5a';
if (int.TryParse(str, out num)) Console.WriteLine(num);
else Console.WriteLine('Cannot convert');
Flow Control
In C#, there are two ways to determine the selection of statements for execution:
□ if-else
statement
□ switch
statement
if-else Statement
The most common flow-control statement is the if-else
statement. It evaluates a Boolean expression and uses the result to determine the block of code to execute. Here's an example:
int num = 9;
if (num % 2 == 0) Console.WriteLine('{0} is even', num);
else Console.WriteLine('{0} is odd', num);
In this example, if num
modulus 2 equals to 0, the statement '9 is even' is printed; otherwise (else
), '9 is odd' is printed.
Remember to wrap the Boolean expression in a pair of parentheses when using the if
statement.
If you have multiple statements to execute after an if-else
expression, enclose them in {}
, like this:
int num = 9;
if (num % 2 == 0)
else
Here's another example of an if-else
statement:
int num = 9;
string str = string.Empty;
if (num % 2 == 0) str = 'even';
else str = 'odd';
You can rewrite these statements using the conditional operator (?:
), like this:
str = (num % 2 == 0) ? 'even' : 'odd';
Console.WriteLine(str); //---odd---
?:
is also known as the ternary operator.
The conditional operator has the following format:
condition ? first_expression : second_expression;
If condition is true, the first expression is evaluated and becomes the result; if false, the second expression is evaluated and becomes the result.
switch Statement
You can evaluate multiple expressions and conditionally execute blocks of code by using if- else
statements. Consider the following example:
string symbol = 'YHOO';
if (symbol == 'MSFT') {
Console.WriteLine(27.96);
} else if (symbol == 'GOOG') {
Console.WriteLine(437.55);
} else if (symbol == 'YHOO') {
Console.WriteLine(27.15);
} else Console.WriteLine('Stock symbol not recognized');
One problem with this is that multiple if
and else-if
conditions make the code unwieldy — and this gets worse when you have lots of conditions to check. A better way would be to use the switch
keyword:
switch (symbol) {
case 'MSFT':
Console.WriteLine(27.96);
break;
case 'GOOG':
Console.WriteLine(437.55);
break;
case 'YHOO':
Console.WriteLine(27.15);
break;
default:
Console.WriteLine('Stock symbol not recognized');
break;
}
The switch
keyword handles multiple selections and uses the case
keyword to match the condition. Each case
statement must contain a unique value and the statement, or statements, that follow it is the block to execute. Each case
statement must end