C# Switch
C# Switch Statements Use the switch statement to select one of many code blocks to be executed. Syntax switch(expression) { case x: // code block break; case y: // code block break; default: // code block break; }
C# Switch Statements Use the switch statement to select one of many code blocks to be executed. Syntax switch(expression) { case x: // code block break; case y: // code block break; default: // code block break; }
Short Hand If…Else (Ternary Operator) There is also a short-hand if else, which is known as the ternary operator because it consists of three operands. It can be used to replace multiple lines of code with a single line. It…
The else if Statement Use the else if statement to specify a new condition if the first condition is False. Syntax if (condition1) { // block of code to be executed if condition1 is True } else if (condition2) {…
The else Statement Use the else statement to specify a block of code to be executed if the condition is False. Syntax if (condition) { // block of code to be executed if the condition is True } else {…
C# Conditions and If Statements C# supports the usual logical conditions from mathematics: Less than: a < b Less than or equal to: a <= b Greater than: a > b Greater than or equal to: a >= b Equal…
C# Booleans Very often, in programming, you will need a data type that can only have one of two values, like: YES / NO ON / OFF TRUE / FALSE For this, C# has a bool data type, which can…
Strings – Special Characters Because strings must be written within quotes, C# will misunderstand this string, and generate an error: string txt = “We are the so-called “Vikings” from the north.”; The solution to avoid this problem, is to use…
Access Strings You can access the characters in a string by referring to its index number inside square brackets []. This example prints the first character in myString: Example string myString = “Hello”; Console.WriteLine(myString[0]); // Outputs “H” Note: String…
String Interpolation Another option of string concatenation, is string interpolation, which substitutes values of variables into placeholders in a string. Note that you do not have to worry about spaces, like with concatenation :