C# [1] Specifying one or more conditions to be evaluated or tested by the program requires decision-making structures. The proper statements must be performed if the condition is true or false.
C# provides the following types of decision-making statements (table 1):
if Statement
An if statement consists of a boolean expression followed by one or more statements.
The syntax of an if statement in C# is:
if(boolean_expression) { /* Statement(s) will execute if the boolean expression is true */ }
If the boolean expression evaluates to true, the code block inside the if statement is executed. If the boolean expression evaluates to false, the first code set after the end of the if statement (after the closing curly brace) is executed.
Example:
using System; namespace DecisionMaking { class Program { static void Main(string[] args) { /* Local variable definition */ int a = 10; /* Check the boolean condition using if statement */ if (a < 20) { /* If condition is true then print the following */ Console.WriteLine("a is less than 20"); } Console.WriteLine("value of a is : {0}", a); Console.ReadLine(); } } }
Output:
a is less than 20; value of a is : 10
if…else Statement
An if statement can be followed by an optional else statement, which executes when the boolean expression is false.
The syntax of an if…else statement in C# is:
if(boolean_expression) { /* Statement(s) will execute if the boolean expression is true */ } else { /* Statement(s) will execute if the boolean expression is false */ }
If the boolean expression evaluates to true, then the if block of code is executed; otherwise, else block of code is executed.
Example:
using System; namespace DecisionMaking { class Program { static void Main(string[] args) { /* Local variable definition */ int a = 100; /* Check the boolean condition */ if (a < 20) { /* If condition is true then print the following */ Console.WriteLine("a is less than 20"); } else { /* If condition is false then print the following */ Console.WriteLine("a is not less than 20"); } Console.WriteLine("value of a is : {0}", a); Console.ReadLine(); } } }
Output:
a is not less than 20; value of a is : 100
Nested if Statement
It is always legal in C# to nest if…else statements, which means you can use one if or else if statement inside another if or else if statement(s).
The syntax for a nested if statement is as follows:
if( boolean_expression 1) { /* Executes when the boolean expression 1 is true */ if(boolean_expression 2) { /* Executes when the boolean expression 2 is true */ } }
Example:
using System; namespace DecisionMaking { class Program { static void Main(string[] args) { //* Local variable definition */ int a = 100; int b = 200; /* Check the boolean condition */ if (a == 100) { /* If condition is true then check the following */ if (b == 200) { /* If condition is true then print the following */ Console.WriteLine("Value of a is 100 and b is 200"); } } Console.WriteLine("Exact value of a is : {0}", a); Console.WriteLine("Exact value of b is : {0}", b); Console.ReadLine(); } } }
Output:
Value of a is 100 and b is 200 Exact value of a is : 100 Exact value of b is : 200
switch Statement
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable switched on is checked for each switch case.
The syntax for a switch statement in C# is as follows:
switch(expression) { case constant-expression : statement(s); break; /* Optional */ case constant-expression : statement(s); break; /* Optional */ /* You can have any number of case statements */ default : /* Optional */ statement(s); }
The following rules apply to a switch statement.
Example:
using System; namespace DecisionMaking { class Program { static void Main(string[] args) { /* Local variable definition */ char grade = 'B'; switch (grade) { case 'A': Console.WriteLine("Excellent!"); break; case 'B': case 'C': Console.WriteLine("Well done"); break; case 'D': Console.WriteLine("You passed"); break; case 'F': Console.WriteLine("Better try again"); break; default: Console.WriteLine("Invalid grade"); break; } Console.WriteLine("Your grade is {0}", grade); Console.ReadLine(); } } }
Output:
Well done Your grade is B
Nested switch Statement
It is possible to have a switch as part of an outer switchstatement sequence. No conflicts will arise even if the case constants of the inner and outer switch contain common values.
The syntax for a nested switch statement is as follows:
switch(ch1) { case 'A': Console.WriteLine("This A is part of outer switch" ); switch(ch2) { case 'A': Console.WriteLine("This A is part of inner switch" ); break; case 'B': /* Inner B case code */ } break; case 'B': /* Outer B case code */ } Example: <code C> using System; namespace DecisionMaking { class Program { static void Main(string[] args) { int a = 100; int b = 200; switch (a) { case 100: Console.WriteLine("This is part of outer switch "); switch (b) { case 200: Console.WriteLine("This is part of inner switch "); break; } break; } Console.WriteLine("Exact value of a is : {0}", a); Console.WriteLine("Exact value of b is : {0}", b); Console.ReadLine(); } } }
Output:
This is part of outer switch This is part of inner switch Exact value of a is : 100 Exact value of b is : 200