C# The else Statement

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 
{
// block of code to be executed if the condition is False
}

Example

int time = 20;
if (time < 18) 
{
  Console.WriteLine("Good day.");
} 
else 
{
  Console.WriteLine("Good evening.");
}
// Outputs "Good evening."

 

Continue reading C# The else Statement

C# If … 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 to a == b
  • Not Equal to: a != b

You can use these conditions to perform different actions for different decisions.

C# has the following conditional statements:

  • Use if to specify a block of code to be executed, if a specified condition is true
  • Use else to specify a block of code to be executed, if the same condition is false
  • Use else if to specify a new condition to test, if the first condition is false
  • Use switch to specify many alternative blocks of code to be executed

Continue reading C# If … Else

C# Booleans

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 take the values true or false.

Continue reading C# Booleans

C# Special Characters

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 the backslash escape character.

The backslash (\) escape character turns special characters into string characters: Continue reading C# Special Characters

C# Access Strings

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 indexes start with 0: [0] is the first character. [1] is the second character, etc.

Continue reading C# Access Strings

C# String Interpolation

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 : Continue reading C# String Interpolation

C# String Concatenation

String Concatenation

The + operator can be used between strings to combine them. This is called concatenation:

Example

string firstName = "John ";
string lastName = "Doe";
string name = firstName + lastName;
Console.WriteLine(name);

 

Note that we have added a space after “John” to create a space between firstName and lastName on print.

Continue reading C# String Concatenation

C# Strings

C# Strings

Strings are used for storing text.

A string variable contains a collection of characters surrounded by double quotes:

Example

Create a variable of type string and assign it a value:

string greeting = "Hello";

 

Continue reading C# Strings

C# Math

The C# Math class has many methods that allows you to perform mathematical tasks on numbers

Math.Max(x,y)

The Math.Max(x,y) method can be used to find the highest value of x and y:

Example

Math.Max(5, 10);

Continue reading C# Math

C# Logical Operators

Logical Operators

As with comparison operators, you can also test for True or False values with logical operators.

Logical operators are used to determine the logic between variables or values:

Operator Name Description Example Try it
&& Logical and Returns True if both statements are true x < 5 &&  x < 10
|| Logical or Returns True if one of the statements is true x < 5 || x < 4
! Logical not Reverse the result, returns False if the result is true !(x < 5 && x < 10)

Continue reading C# Logical Operators