Decision and Case

Embed Size (px)

Citation preview

  • 8/13/2019 Decision and Case

    1/8

    Decision control instructs

    Relation operators. If statement. Nested if statement. If else statement. Nested if else statement. Logical operators. Else if clause. Conditional operators.

    Relation operators: a relational operator is a programming language construct or operator that tests or

    defines some kind of relation between two entities. These include numerical equality (e.g., 5 = 5) and

    inequalities (e.g., 4 3).

    x==y x is equal to y. x != y x is not equal to y. x y x is grater than y. x=y x is grater than or equal to.

    Conditional statements: In computer science, conditional statements, are features of a programming

    language which perform different computations or actions depending on whether a programmer-specified

    boolean condition evaluates to true or false.

    If statement :

    if (condition)

    execute this statement;

    OR

  • 8/13/2019 Decision and Case

    2/8

    if (condition)

    {

    statement 1;

    statement 2;

    }

    If else statement :

    if (condition)

    statement 1;

    else

    statement 2;

    OR

    if (condition)

    {

    statement 1;

    }

    else

    {

    statement 2;

    }

    Nested if else:

    if (condition)

    {

    if (condition){

    statement 1;

    }

    else

    {

    statement 2;

    }

    }

    else

    {

    statement 2;

    }

  • 8/13/2019 Decision and Case

    3/8

    Logical operator:

    C# provides a large set of operators, which are symbols that specify which operations to

    perform in an expression. Operations on integral types such as ,

    && AND operator. || OR operator. ! NOT operator.

    Loop Control Instruction :

    While loop For loop Break statement Continue statement Do-while loop

    While loop :

  • 8/13/2019 Decision and Case

    4/8

    Example problem : Simple interest program

    static void Main (String[] args)

    {

    int p,n,count;

    float r,si;

    count=1

    while(count

  • 8/13/2019 Decision and Case

    5/8

    For loop: this looping technique is probably the most popular looping instruction. Because for allows us

    to specify three things about a loop in a single line :

    Initial values. Testing condition. Incrementation/ decrementation.

    Example for the same si problem :for(i=1;i

  • 8/13/2019 Decision and Case

    6/8

    Continue statement:

    Continue statement is used to get out of the loop with out executing the remaining statements. Continue statement is usually associated with if.

    The do-while loop:

    In While , the condition is tested before the execution . But in do-while , the statements are executedfirst and then condition is tested.

    Do{

    statement 1;

    statement 2:

    } while (condition);

  • 8/13/2019 Decision and Case

    7/8

    Case Control Statement:

    Decision Using switch: the control statement that allows us to make a decision from the number of choices is

    called a switch.

    Example:

    Static void main (string[]args)

    {

    int i= ? ;

    switch(i)

    {

    case 1:

    Console.WriteLine(I am in case 1);

    break;

    case 2:

    Console.WriteLine(I am in case 2);

    break;

    case 3:

    Console.WriteLine(I am in case 3);

    break;

    default :

    Console.WriteLine(I am default);

    break;

    } }

    Tips:

    Case statements can be put in any order. Every statement in the switch must belong to some case . The break program when used in the switch takes control out side the switch. But the use of continue will not take the control to the beginning of switch.

    Goto:

    In a difficult programming situation , it seems to so easy to use a goto to take the control where youwant. However , almost always there is a better way of writing the same program using if,for,while and

    switch. These construct are far more logical and easy to understand.

  • 8/13/2019 Decision and Case

    8/8