Searching...

Translate

C# Control structures

*   2.2 C# Control structures

   There are three categories of program control statements:

1.     Selection statements, which are the if and the switch.
2.     Iteration statements, which consist of the for, while, do-while, and foreach loops.
3.     Jump statements, which include break, continue, goto, return, and throw.

*      2.2.1 Conditional structure(Selection statements)

1.    The if statements

   You can selectively execute part of a program through the use of C#’s conditional statement the if. The if statement works in C# much like the IF statement in any other language.

   For example, it is syntactically identical to the if statements in C, C++, and Java. Its simplest form is shown here:



Syntax: -
if(condition)
{
statement sequence
}
else
{
statement sequence
}

   If the conditional expression is true, the target of the if will be executed; otherwise, if it exists, the target of the else will be executed. At no time will both of them be executed. The conditional expression controlling the if must produce a Bool result. The else clause is optional.

Þ   Nested ifs

   A nested if is an if statement that is the target of another if or else. Nested ifs are very common in programming.

Þ   The if-else-if Ladder

   A common programming construct that is based upon the nested if is the if-else-if ladder. It looks like this:

if(condition)
statement        
else if(condition)
statement;
else if(condition)
statement;
...
else
statement;

   The conditional expressions are evaluated from the top downward. As soon as a true condition is found, the statement associated with it is executed, and the rest of the ladder is bypassed.

   If none of the conditions is true, then the final else clause will be executed. The final else often acts as a default condition. That is, if all other conditional tests fail, then the last else clause is executed. If there is no final else and all other conditions are false, then no action will take place.

2.    The Switch Statement

   The second of C#’s selection statements is switch. The switch provides for a multiway branch. Thus, it enables a program to select among several alternatives.

    Although a series of nested if statements can perform multiway tests, for many situations the switch is a more efficient approach. It works like this: The value of an expression is successively tested against a list of constants. When a match is found, the statement sequence associated with that match is executed. The general form of the switch statement is

switch(expression)
{
case constant1:
statement sequence
break;
case constant2:
statement sequence
break;
case constant3:
statement sequence
break;
.
.
.
default:
statement sequence
break;
}

   The default sequence is executed if no case constant matches the expression. The default is optional; if it is not present, no action takes place if all matches fail.

   When a match is found, the statements associated with that case are executed until the break is encountered. The following program demonstrates the switch:

using System;


class Program
{
    static void Main(string[] args)
        {
            Int16 a,b,add,sub,mul,div;
            char choice;

            Console.WriteLine("Enter a Value ");
            a = Convert.ToInt16(Console.ReadLine());
            Console.WriteLine("\nEnter  other value ");
            b = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("\nPress + for addition\n  - for                 subtraction\n     * for multiplication\n      / for division");
            choice=Convert.ToChar(Console.ReadLine());
            switch(choice)
            {
                case '+':         
                            add = Convert.ToInt16(a+b);
                            Console.WriteLine("\n\nAddition = {0}",add);
                            break;
                case '-':
                            sub = Convert.ToInt16(a - b);
                            Console.WriteLine("\n\nSubtraction = {0}",sub);
                            break;
                case '*':
                       
                            mul = Convert.ToInt16(a * b);
                            Console.WriteLine("\n\nMultiplication = {0}",mul);
                            break;
                case '/':

                            div = Convert.ToInt16(a / b);
                               Console.WriteLine("\n\nAddition = {0}",div);
                            break;
                default:
                            Console.WriteLine("\nWrong Choice..");
                            break;
            }
           
            Console.ReadLine();
         
        }

}

0 comments:

Post a Comment

Subscribe us