Start C# here

Study C#.. Go ahead ! !

Tuesday, May 20, 2008

Conditional Operators in C#

if-else constructor

if-else constructor is used to check conditions using. if-else will be always having two conditions True or False.

Let's see a simple if-else constructor.

--------------------------------------
if(Condition)
{
// true part;
}
else
{
// false part;
}
---------------------------------------

In the above set of code we are using an if-else constructor to check a condition. Now if the Condition we have typed inside the if is TRUE it will get inside the True part of the constructor and if the condition is false it will enter inside the false part. The CONDITION may Contain operators like Arithmetic, Relational and Logical for checking different types of conditions.

Now, Lets check a real problem with if-else....

I have two integer variables nameley iNum1 and iNum2. I am going to check which number is bigger.

----------------------------------------
//Assigning values for the two variables
iNum1=10;
iNum2=20;

//Starting the condition

if(iNum1>iNum2)
{
Console.WriteLine("The condition is TRUE");
}
else
{
Console.WriteLine("The condition is FALSE");
}

----------------------------------------

Here iNum1 is assigned a value of 10 and iNum2 is assigned a value of 20. Now when the program gets executed the condition will be returning false because iNum1 variable is smaller than iNum2.
So, it will print "The condition is FALSE" on the screen.

Like this if the condition gets true it will enter into the true part ie the "if" body of the program and if condition gets false it enters into the else part of the Program.

We can also have multiple nested if conditions on a single if-else constructor. Let's check another example of checking which number is bigger out of three numbers.

I have three variables namely iNum1,iNum2,iNum3.

------------------------------------------

if(iNum1>iNum2 && iNum1>iNum3)
{
Console.WriteLine("iNum1 is the biggest number");
}
else if(iNum2>iNum1 && iNum2>iNum3)
{
Console.WriteLine("iNum2 is the biggest number");
}
else
{
Console.WriteLine("iNum3 is the biggest number");
}

------------------------------------------

Here in the above code we are using Relational and Logical operators. When we have multiple conditions like this we can use operators as shown above.
We have actually three conditions here so we can use Nested if's like this. When the if condition gets false it will enter the else if condition and when and only else if condition gets false it will enter the else condition of the program.


This is how we use if-else constructor in programs.



-------------------------------------------------------------

switch-case conditions.

switch case conditional statements are used to check conditions according to the incoming values to the variables. Here we will switch a variable and inside the cases we will check for the conditions.......

Let's see and example for this...

--------------------------------------------------------------
switch(iMonth)
{
case 1:
{
Console.WriteLine("January");
break;
}
case 2:
{
Console.WriteLine("February");
break;
}
case 3:
{
Console.WriteLine("March");
break;
}
default:
{
Console.WriteLine("Please enter a Valid number");
break;
}
}
------------------------------------------------------------

Here in the above code we are switching a variable inside the switch body. Now the switch will check for cases with the value inside the switch variable. If it matches any of the value inside any case it will get into the Case body.

If we pass a value 2 into the variable iMonth the switch will get into the case 2: and print "February".

Since we are using integer Datatype we can straight away write the numbers in the cases with a single space seperated.

The "default" statement gets printed in the case if a number(value) which is not the case conditions is assigned to the switch variable.

Now if you are passing a Character Datatype you must write the cases like this...
--------

case 'A':
{
//statements
break;
}
case 'C':
{
//statements
break;
}

---------

and in the case of strings you can write the cases like this....

---------

case "C#.net"
{
Console.WriteLine("Windows programming");
break;
}
case "ASP.net"
{
Console.WriteLine("Web Programming");
}

---------

you must have checked "break" statements after each case and default statements.
We must use break statements inside this just because it may jump into next Case. In C# break statements are compulsory and if we dont use break statements it will return an error.

We can have multiple cases in the switch statement but only a single default statement in the switch statement....


That's all about switch case and If else conditions.........

1 comment:

Unknown said...

great article , .. but how can i use a conditional operators (<,>,....) inside cases of switch :
like for ex:

switch(x){
case(<2):
{// statments
break;
}
......
}

I know that this code will do error but i need to know how i make it correct... thanks alot