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

Learn C# – If statement – Part 1

Introduction

Frequently when writing code, we need to perform specific code section only if certain condition exist.

For example, we want to call the student’s mother only if he failed the exam, or printing receipt to the client only if the payment was approved by the credit card company. Taking actions according to conditions is a very common thing to do in real life, and in particular while coding.

Boolean expressions

Here is another thing that we do all the time without realizing it – Asking ourselves questions. Every day, every hour, and probably more than once at an hour. In order to take actions under conditions we must first define the question – and more important  – find the answer – Yes or No. Questions with only Yes No answers are called boolean  statements. Lets take a look at several different boolean expressions:


bool b = true;

OK, that’s not really a boolean expression, it’s more like variable declaration and assignment. But, if you take a look more closely, you’ll be able to see that the variable “b”, after the assignment, is a boolean expression by himself. That’s because it fits into the “Answer” rule – Everything that gives us Yes or No answer (even if there is no question around) will be considered as boolean expression. Since the value of  “b” is “true”, we can consider it as a boolean expression. Let’s see some more examples:


a == 5

Assuming “a” is an integer variable, we’re using the “==” operator to ask our question:  Is “a” equal to 5. I don’t really know at the moment what is the value of  “a”, but I do know that this question has only one of two answers: Yes or No. Here are some more examples for boolean expressions, before we leap into the “if” river:


//x is an integer
x > 10
//str is a string variable
str == "my string"

We’ll talk more about operators in a different post.

If Statement

After we understood some basic things regarding boolean expressions, all we need to do is to push it into the “if” statement using the following syntax:

if ( /* condition */ )
{
   //do something
}

or more specifically:

if (a == 5)
{
   Console.WriteLine("a is Equal to 5!")
}

Notice, that the code written inside the braces will only get executed if the boolean expression of the if statement will be equal to true. Otherwise, the program will continue to execute the code right after the braces. Of course, we can execute more than one line of code inside the braces – as long as we didn’t close the braces – we can proceed with writing a code that only get executed if the boolean expression of the if statement will be true. If we only want to execute one line of code – we can avoid the braces and write this code right after the if statement, like this:

if (a == 5)
   Console.WriteLine("a is Equal to 5!")
Console.WriteLine("This line will be executed anyway!!!")

Notice that the second Console.WriteLine() will get executed anyway even if the condition inside the if will be equal to false – since “if” without braces will only affect the first line of code.

One more thing for this post – We took an example from an integer variable. Let’s take a look in a boolean variable:

bool b = true;
if (b == true)
{
   Console.WriteLine("b is Equal to true!")
}

Since the boolean variable was assigned to true, the condition exist and the Console.WriteLine line will get executed. However, as we said before, the variable “b” itself is a boolean expression (since it’s a boolean variable), so we don’t need to actually ask the question in it’s full form. We can just use this code:

bool b = true;
 if (b)
 {
 Console.WriteLine("b is Equal to true!")
}

Pay attention to the slight difference between those 2 versions, They are both true, and you can use them both, But it definitely gives you some idea about using boolean expressions inside if statements.

We’ll continue with the “if” statements in the next posts.

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: