In C# it isn’t really different from languages like Java. In this case, the conditional logic is almost identical.
The first one, the If statements. An if statement consists of a boolean expression followed by one or more statements.
Here is an example:
namespace logic { class Program { static void Main(string[] args) { String franklinisawesome = "yes"; if (franklinisawesome == "yes") { Console.WriteLine("Franklin is the bomb"); } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } } }
In this case, the code will output Franklin is the bomb, since franklinisawesome is equal to yes.
The next one is If Else statement. An if statement can be followed by an optional else statement, which executes when the boolean expression is false.
Here is the example:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace logic { class Program { static void Main(string[] args) { String franklinisawesome = "No"; if (franklinisawesome == "yes") { Console.WriteLine("Franklin is the bomb"); } else { Console.WriteLine("awww. =("); } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } } }
in this case, it will output awwww. =( because franklinisawesome is not true, therefore the else statement is ran instead.
Lastly there is 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 being switched on is checked for each switch case.
Here is the example:
class SwitchTest { static void Main() { Console.WriteLine("Coffee sizes: 1=small 2=medium 3=large"); Console.Write("Please enter your selection: "); string str = Console.ReadLine(); int cost = 0; // Notice the goto statements in cases 2 and 3. The base cost of 25 // cents is added to the additional cost for the medium and large sizes. switch (str) { case "1": case "small": cost += 25; break; case "2": case "medium": cost += 25; goto case "1"; case "3": case "large": cost += 50; goto case "1"; default: Console.WriteLine("Invalid selection. Please select 1, 2, or 3."); break; } if (cost != 0) { Console.WriteLine("Please insert {0} cents.", cost); } Console.WriteLine("Thank you for your business."); } } /* Sample Input: 2 Sample Output: Coffee sizes: 1=small 2=medium 3=large Please enter your selection: 2 Please insert 50 cents. Thank you for your business. */
Of course there is nested statements, which mean you can stack multiple elses or switches inside each other, etc.
For more information, visit:
http://msdn.microsoft.com/en-us/library/676s4xab.aspx
http://www.tutorialspoint.com/csharp/csharp_decision_making.htm