Home > C#, Learn C# > Learn C# – If statement – Part 2

Learn C# – If statement – Part 2

In the last post we discussed about simple if statements and boolean expressions.

Now, what if we want to check if some expression is equals to false? the simple way will be this:


bool b = true;

if (b == false)

{

//code will get executed only if b equlas to false.

}

Of course, our boolean expression won’t be like this always,  so we can also use this one:


int i=10;

if (i != 10)

{

//code will get executed only if i not equal to 10

}

As you can see, the operator “!=” means “not equal to”. Another relevant operator for this kind of  statements will be the “!” operator, which means NOT, take a look:


bool b = false;

if (!b)

{

//code here will get executed since b equal to false, and that's what we've looked for in the condition

}

int i = 3;

if (! (i==3))

{

//code here won't get executed since we're looking for "Not (i equal to 3)".

}

 In the next post will see how to take an alternative actions, if the condition does not exist.
Advertisements
Categories: C#, Learn C# Tags: , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: