Thursday, January 27, 2011

Conditional Statements

Conditional Statements
Compiler executes code from top to bottom and line to line. If you have to make decision for different users or you have to change control of code, what will you do? For this Control Statements are used to make decisions
  • IF-Statement
  • IF-ELSE Statement
  • IF-ELSE-IF Statement
  • Switch Statement
IF-Statement
The syntax for IF statement is 

if(expression)
{
// Statement block
}

Expression written in the brackets helps relational test to be performed and tells that whether or not to execute the statements inside the IF statement block depends on the outcome of relational test or operations. If operation is TRUE then it will execute IF statement block and if FALSE then it will skip IF statement block.

Example
Making a program using IF statement to show ODD and EVEN number.

static void Main(string[] args)
        {
            int value;
            string result = "Number is Even";
            Console.WriteLine("Type any Number");
            value = Convert.ToInt32(Console.ReadLine());
            if (value % 2 == 1)
            {
                result = "Number is Odd";
            }
            Console.WriteLine(result);
            Console.ReadKey();
        }


IF-ELSE Statement
In previous example(odd and even program) I have set result as Number is Even at the moment the result was defined. So the result variable will only changed if number is odd, but I want to make decision based on the input whether number is even or odd. The IF-Else Statement is suitable for this. 

The syntax for IF-ELSE statement is

if(expression)
{
// IF statement block : TRUE
}
else
{
// ELSE statement block : FALSE
}

If expression (relational operation) is TRUE then IF statement block is executed if expression is FALSE then it will enter the statement block following the else keyword that is ELSE statement block.


Example
Making a program using IF-ELSE statement to show ODD and EVEN number.

 static void Main(string[] args)
        {
            int value;
            string result;
            Console.WriteLine("Type any Number");
            value = Convert.ToInt32(Console.ReadLine());
            if (value % 2 == 1)
            {
                result = "Number is Odd";
            }
            else 
            {
                result = "Number is Even";
            }
            Console.WriteLine(result);
            Console.ReadKey();
        }


IF-ELSE-IF Statement
In previous example we have work on two decisions that is "Number is Even" or "Number is Odd". If we have to work on multiple decisions like days in week or months in year. For that we have IF-ELSE-IF statement. 
The syntax for IF-ELSE-IF statement is

if(expression)
{
}
else if(expression1)
{
}
else if(expression2)
{
}
.
.
.
.
.
else
{
}

Example
Make a program that will take an number input and will show you that which number you have typed.

static void Main(string[] args)
        {
            int num;
            Console.WriteLine("Type Number between 1 - 6");
            num = Convert.ToInt32(Console.ReadLine());
            if (num == 1)
                Console.WriteLine("You have Typed ONE");
            else if (num == 2)
                Console.WriteLine("You have Typed TWO");
            else if (num == 3)
                Console.WriteLine("You have Typed THREE");
            else if (num == 4)
                Console.WriteLine("You have Typed FOUR");
            else if (num == 5)
                Console.WriteLine("You have Typed FIVE");
            else if (num == 6)
                Console.WriteLine("You have Typed SIX");
            else
                Console.WriteLine("Number Out of SCOPE");
            Console.ReadKey();
        }
Switch Statement
Another type of control statement is switch statement that can handle multiple decisions by passing control to case statements which are inside switch body.
The syntax of switch statement is.
switch(expression)
{
 case1:
// case1 statement
break; // break or any statement that can take control of transferring case body
case2:
// case2 statement
break;
.
.
.
.
default:
// default statement
break;
}
In switch statement , which case has to be executed is determine by expression. If value of expression is 1 then case1 will be executed and if expression do not match any case then default case will be executed. The break statement control from falling through to the next case statement.
Example
Let make the same program of IF-ELSE-IF in Switch statement.
static void Main(string[] args)
        {
            int num;
            Console.WriteLine("Type Number between 1 - 6");
            num = Convert.ToInt32(Console.ReadLine());
            switch(num)
            {
            case 1:
                Console.WriteLine("You have Typed ONE");
                break;
            case 2:
                Console.WriteLine("You have Typed TWO");
                break;
            case 3:
                Console.WriteLine("You have Typed THREE");
                break;
            case 4:
                Console.WriteLine("You have Typed FOUR");
                break;
            case 5:
                Console.WriteLine("You have Typed FIVE");
                break;
            case 6:
                Console.WriteLine("You have Typed SIX");
                break;
            default:
                Console.WriteLine("Number Out of SCOPE");
                break;
            }
            Console.ReadKey();
        }

No comments:

Post a Comment