C# For Loop

C# For Loop

When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:

Syntax

for (statement 1; statement 2; statement 3) 
{
  // code block to be executed
}

Statement 1 is executed (one time) before the execution of the code block.

Statement 2 defines the condition for executing the code block.

Statement 3 is executed (every time) after the code block has been executed. Continue reading C# For Loop

C# While Loop

Loops

Loops can execute a block of code as long as a specified condition is reached.

Loops are handy because they save time, reduce errors, and they make code more readable.

Continue reading C# While Loop

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;
}

Continue reading C# Switch

C# Short Hand If…Else

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 is often used to replace simple if else statements:

Syntax

variable = (condition) ? expressionTrue : expressionFalse;

Instead of writing: Continue reading C# Short Hand If…Else

C# The else if Statement

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) 
{
// block of code to be executed if the condition1 is false and condition2 is True
} 
else
{
// block of code to be executed if the condition1 is false and condition2 is False
}

Example

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

 

Continue reading C# The else if Statement

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