Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C# Tutorial [WIP]
#11
This thread is very resourceful
[-] The following 2 users Like Xyt0's post:
  • admin, milkwalker
Reply
#12
9. Flow Control


In this post I'd like to show you a few ways to control a program's flow; for this, we're going to require some of the operators from the last post.
In C#, we're using if-statements; they have the following structure:
Code:
if (<condition>)
{
}
where "<condition>" is something like
Code:
i < 10
or anything else that is, or can be converted to a boolean value.

So, if-statements are being used to determine whether a condition is true, and allow to run code if so. Example:
Code:
int i = 5;

if (i == 5)
    Console.WriteLine("i is 5");

The above could prints "i is 5" to the console, because the condition is true, and the code is being executed.

But what if you want to run code if i is not 5?
Now you can do two things; either
Code:
if (!(i == 5)) //or i != 5
    Console.WriteLine("i is not 5");

or you can use the else branch, which has the following structure:
Code:
if (<condition>) { }
else { }

In our example the complete code would look like this:
Code:
int i = 10;

if (i == 5)
    Console.WriteLine("i is 5");

else
    Console.WriteLine("i is not 5");

Now, "is is not 5" is being written to the console.


Sometimes you'll need more than one condition, for this case C# provides else if statements. Example: you want to run appropriate code when a number is 1, 2, or something else. Using if - else if - else, we could express this like
Code:
int i = 2;

if (i == 1)
    Console.WriteLine("i is 1. Doing stuff for this case.");

else if (i == 2)
    Console.WriteLine("i is 2. Doing stuff for this case.");

else
    Console.WriteLine("Doing stuff for any other case.");

You can add as many else if branches as you need.
Seems pretty simple, doesn't it?

There's a way to make this even cleaner: the switch. For the above example, a switch statement could look like the following.

Code:
int i = 2;

switch (i)
{
    case 1: //if (i == 1)
        Console.WriteLine("i is 1. Doing stuff for this case.");
        break; //the break statement acts like a }, it terminates the current block
    case 2: //else if (i == 2)
        Console.WriteLine("i is 2. Doing stuff for this case.");
        break;
    default: //else
        Console.WriteLine("Doing stuff for any other case.");
        break;
}

But there's a gotcha: switch cases require compile time constants like 1, or in case of a string switch, "hello", because they are being compiled to a lookup, resulting in higher performance than ordinary ifs and else ifs.


You've read about the conditional operator (? :) in the previous post; now consider the following code:
Code:
int i = 0, j = 1;

if (j == 1)
    i = 10;

else i = -1;
Using the conditional operator, we can simplify this:
Code:
i = j == 1 ? 10 : -1


Note, you can nest all if, else, else if, and switch statements as deep as you want, but it will become hard to read very soon! Always try to keep your code clean.
[-] The following 1 user Likes milkwalker's post:
  • admin
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)