Purpose of this article.

The purpose of this article and the rest of this series is to help you start getting familiar with some of the KEY concepts and syntax in C# that you will need to be familiar with to get started on your own projects in C#. I will try to cover all of the concepts but if you read this article and still have problems understanding anything leave a comment and Ill see if I can help you clear it up!

One of the basic tenets of any programming language is managing control flow and looping. In this series of posts, I will be focusing on the most common types of control flow and the operators that assist them.

I hope this series proves useful to you Developing Developers out there!

 

Let’s talk about control flow in C#, specifically …  The IF statement.

 

The IF statement is a pillar of any programming language. It is used to evaluate whether a predefined condition exists and IF the condition is met the code that follows will execute.

Different programming languages have varying ways in which you designate the body of an If statement. Lucky for us though, the basic idea is the same in any language.

 

The Basic IF statement

The IF statement works like you might expect, just like when speaking it is simply:

IF <some condition> then <something happens>

In some languages you have to  use a second keyword to designate the condition that executes when the IF condition evaluates to True.  If you are exploring a different language and see something like :

not C#

If (X = 1 ) 

then (do something)

 

 

In C# though we just need to use curly braces to contain our logic

In C# the If statement looks like this:

           if (dealer.IsBusted())
            {
                DisplayBust(dealer);
            }
           else 
            {
                 player.Stand(dealer);
            }
           

It’s as simple as it looks, if the dealer is busted Display the Busted Message, else display the Player Stands message.

 

If… Else If … Else.

There are three paths that control can follow when executing an If statement and luckily they execute in a way that follows normal language standards.

If (some condition then do something )

else if (some condition then do something)

else (do something)

 

int a = 1;
int playerAge = 32;

if (a < 1)
{
    Console.Writeline("it was < 1");
}
else if (a = 2)
{
    // Condition1 is false and Condition2 is true.
}
else if (a == 1)
{
    if (a > playerAge)
    {
        // Condition1 and Condition2 are false. Condition3 and Condition4 are true.
    }
    else
    {
        // Condition1, Condition2, and Condition4 are false. Condition3 is true.
    }
}
else
{
    // Condition1, Condition2, and Condition3 are false.
}

The rules

The main things that we need to remember when using If statements:

  • The if statements are case sensitive (as are most things in C#)
  • We do not need to terminate the if statements with a semicolon (;)
  • The conditions will always be surrounded by parenthesis
  • We can have as many conditions nested between the parenthesis as we need.
  • You DO need to terminate the actions that execute after the condition with a semicolon.
  • We can have as many else if statements as we want.
  • There can only be 1 else statement for each if statement.

Equality vs Assignment

Using these statements we have a huge amount of control over the execution of our code. When you combine variables and if statements, you can accomplish a wide range of things in any programming language. One very important thing to remember which is highlighted above.. In C# we have the concept of “Equality operators” and assignment operators. Without getting off topic we have to remember that if we want to assign a value to a variable we use a single equal sign and if we want to evaluate equality between to objects then we need to use two equals signs. It is very common to confuse these two operators but they are quite different. The assignment operator will actually change the value of the variable, whereas the equality operator will just read the value of the variable and see if it’s equal to something.

Equality

if ( a == 2)

Assignment

( a = 1 )

Here is a list of Equality and Assignment operators:

Operator Meaning True expressions
== Equals a == 28
!= Does not equal a != 29 28 !== 29
> Greater than a > 25 28 > 25
>= Greater than or equal a >= 28 28 >= 28
< Less than a < 30 28 < 30
<= Less than or equal a <= 28 28 <= 28

Evaluating multiple conditions / Logical operators

We sometimes want to evaluate  multiple comparisons (if (someCondition and some other condition), and that’s why we have logical operators. These operators let us say things like “if someCondition is true and someOtherCondition is also true. We can also evaluate if things are NOT true OR if  some are true and some others aren’t. These operators are called Boolean logical operators. If we remember that boolean simply means true or false we can boil down the meanings of these operators pretty quickly. There are a few others here that I won’t go into but these will get you started.
Operator Meaning True expressions
! Not (negation) if !( a = 1)  a = 2
&& and if ( ( a = 2 ) && (a < 3) )
a = 2
|| or if ( (a = 3) || (a == 2) )
a = 2

Conclusion

Today you learned how to take control of your programs execution in C#. We talked about logical operatiors, assignments, and equality. These concepts are a important in every langauge, the syntax does vary between langauges but the concepts are pretty similar. Once you have your head around these concepts you will have taken another step toward developing as a developer! If you have any questions then leave them in the comments and give me a follow on twitter if you like my work!

 

Leave a Reply

Your email address will not be published.