December 3, 2013

Variables, Conditionals and Switch statements

We already know how to open a project in Visual Studio. If you are not familiar with it, just go through the previous posts and come back again. We already know how to construct a simple input-output program in cSharp.

In this post, we shall go through variables, conditional statements like if-else and we shall also see switch statements. If you are familiar with other languages like C, C++ or Java, these topics are just the same, you may skip if you like.

Variables: C# is a compiled language. It means that you have to let the compiler know what types of variables you are going to use in your program. It just cannot determine it on the fly. In order to declare a variable, you have to supply two information: the variable type and its name.
//type<space>name;
int myVar;
string myString;
bool myCheck;

Just like the above, you can optionally initialize the variables. Initializing means you can declare the variables with a given value of your choice.
//type<space>name=something;
int myVar=10;
string myString="hello";
bool myCheck=false;

Having done that, you can safely use these variables wherever you like. But keep in mind, there is something called scope. You can interpret the visibility of a variable as scoping. If you declare a variable inside { } then the variables can be used just inside that region only. This specific region of curly braces { } are said to be the scope of your variable. You are not allowed to use that variable outside that scope. We shall see more about variables in future.

Conditionals: One of the best qualities a program can achieve, is to make decisions. There's no way intelligence can truly be achieved without making decisions. Decisions are done by if-else in programming languages.
We do these type of decision making using if-else statements. The if(..) takes conditional statement inside the braces. In future we shall refer to these as boolean statements. A boolean statement is referred to as either true or false. If the boolean condition evaluates to true, the program enters the if block. Otherwise, if you provide an else block, your program will enter in that block.
if (condition)
{
    //if condition evaluates to true
}
else
{ 
    //if condition evaluates to false
}
//here

The else block is an optional block. If you do not write an else block, no harm done. Depends on how you want to code. Note the last line marked as //here. This is the line where your code will continue from, either from the if block, or from the else block. It means that, it will always execute after any of the if or else block. That's the point of continuation for your code.
You can check a train of conditions one after another. Like if you do not find what you are looking for in one compartment, you go to the other and so on. This is done by writing a condition on else if statement.
if (condition1)  //checked 1st
{
    
}
else if(condition2) //checked 2nd if 1st is false
{ 
    
}
else if(condition3) //checked 3rd if 2nd is false too
{

}
//so on

The else block is optional in this case too. If your code demands to execute something in case everything else evaluate to false, you may provide an else block at the end.
You can evaluate more than one condition in an if statement. You can do the same with else if too. To do this, you will have to know about boolean truth table, precisely to say, AND and OR logic. You might have seen this before. If not, then do some Google/Bing search on these things.
Truth Table
In the code, you do it like this:
if (condition1 && condition2) 
{
    //if both are true, codes inside this block will execute
}
else if(condition3 || condition4) 
{ 
    //if any of condition3 or condition4 is true, or both, then code inside
    //this block will execute
}
//so on

The symbol && refers to the AND logic. On the other hand, two vertical bars || refers to the OR logic. You can check a lot of conditions, not just two of them. These conditionals just make your code dynamic, so your code makes decision at the run-time.

Switch: Switch statements are clean and precise. Unlike a messy bunch of if and else-if statements, switches can give you a cleaner code sequence. Again, the choice is up to you as a programmer to use any one of them. The structure of a switch statement is like the following:
switch(value)
{
    case value1:
        //do some code
        break;          
    case value2:
        //do some code
        break;
    //so on
    default: 
        //do some code
        break;
}

You pass in a value inside the braces followed by switch. This value is checked with all the values successively that you put after each case statement. If it is a match, then the code that you write below will execute. The break statement is a *must*. break is a keyword and if you are familiar with other languages, break is a common keyword used to exit certain blocks of code. Providing break statement is a good programming practice.
The default statement is optional. This part is called when none of the above cases match. In this particular case, you may do some default work, or you may choose not to do anything at all.

I hope I have made these things clear, in case you are new to programming. Play around with these things to get a clearer picture. Happy Coding!

No comments:

Post a Comment