Navigation

Related Articles

Back to Latest Articles

C# Basics – Conditional Statements – Part 5

Let's have a look at some of the logic operators in C# programming language.


Andraz Krzisnik
C# Basics – Conditional Statements...

Conditional Statements

There is a data type for logic operations, called bool. Possible values that this kind of variable can hold are true and false.

Let’s take a look at logic operators:

  • && – AND
  • || – OR
  • ! – NOT

Comparative Operations

With comparative operators we compare two values. And they always return a bool value, true or false, that is.

int i = 5;
bool b = i < 10;   //Returns b = true

If Statement

We use if statement when we want to execute some code when conditions of this if statements, are met.

int i = 5;
if (i > 0)
{
    MessageBox.Show("Condition is met.");
}



If-Else Statement

We use if else statement when we want something to happen even if conditions of if statement aren’t met.

int i = 8;
int j = 4;
if (i <= 10 && j >= 0)
{
    MessageBox.Show("Target is in range.");
}
else
{
    MessageBox.Show("Target is not in range.");
}

Nested If Statements

A nested if statement is an if statement within another if statement. We can also add more if statements that would execute perhaps some different code if there are more than one if statements paired with else.

int i = 5;
int j = 10;
if (i <= 4)
{
    MessageBox.Show("Condition is met.");
}
else if (j == 10)
{
    MessageBox.Show("Another condition is met.");
}
else
{
    MessageBox.Show("No conditions have been met.");
}
Download Examples Next Chapter
Show Comments (0)

Comments

Related Articles

C# Tutorial

C# Tutorial: How To Use Gamma Correction

Gamma correction is a process, which allows you to set the brightness of screen. You can often run into this setting in video games, where you set the brightness of your screen...

Posted on by Andraz Krzisnik
C# Basics

C# Basics – Constructors – Part 12

Constructors Sometimes we want to create an object and set some predetermined state to it. Basically, you would pass the properties’ values as an argument to an object when you...

Posted on by Andraz Krzisnik