Make your own free website on Tripod.com

Lesson 3: Decision Making And Branching.

Prev || Home || Next

Bar_line.gif (11170 bytes)

    A C program is a set of statements which are normally executed sequentially in the order in which they appear. In practice, however we have a number of situations where we have to change the order of execution of statements based on certain conditions.

    C language possesses such decision making capabilities and supports the following statements known as control or decision making statements

    1) if statement.

    2) switch statement.

    3) conditional operator statement.

    4) goto statement.

    Simple if:

        Syntax:

                   if(test condition)

                    {

                        statement block;

                    }

                       stmt-x;

                       Here if the test expression is true, the statement block is executed otherwise the statement block will be skipped and control will jump to stmt-x.

    Program: Enter quantity and unit price. Calculate amount. If amount exceeds 500, discount of Rs.50                      is given otherwise no discount is offered.

    main()

    {

        int q, dis=0;

        float price, amt, tamt;

        clrscr();

        printf("Enter unit price and quantity\n");

        scanf("%f%d",&price, &q);

        amt=price * q;

        if(amt>500)

        {

            dis=50;

        }

        tamt=amt-dis;

        printf("\nTotal amount is =Rs. %f",tamt);

        getche();

    }

   Note:- scanf is the input statement in C its syntax is ' scanf("access specifiers",&variable)' & stands                  for at the address of or in the variable.

    if --- else:

   Syntax:-

                if(test condition)

                {

                    statements block - 1;

                }

                else

                {

                    statements block - 2;

                }

                stmt-x;

    Here test expression is evaluated and if it is executed and if it is true, statements block - 1 is executed, if the expression is false the statements block - 2 is executed. In both the cases after execution stmt-x is executed.

    Program:- To print whether entered number is odd or even.

    main()

    {

        int no;

        clrscr();

        printf("Enter any integer\n");

        scanf("%d",&no);

        if(no % 2 == 0)

        {

            printf("The number entered is even\n");

        }

        else

        {

            printf("The number eneterd is odd\n");

        }

        printf("End of the program");

        getche();

    }

    Nested if --- else:

    Syntax:-

                    if(test expr1)

                    {

                        if(test expr2)

                        {

                            statements block-1;

                        }

                        else

                        {

                              statements block-2;

                         }

                    }

                    else

                    {

                        statements block-3;

                    }

                    stmt-x;

   When a series of decisions are involved, we may have to use more than one if --- else statement in nested form as in the following program.

    Program:- Enter age of bride & bridegroom and print whetehr pair is valid for marriage.

    main()

    {

        int ag1, ag2;

        clrscr();

        printf("Enter the age of bride & bridegroom");

        scanf("%d%d",&ag1,&ag2);

        if(ag1>18)

        {

            if(ag2>21)

            {

                printf("\n The pair is valid for marriage");

            }

        }

        else

        {

            printf("\nThe pair is invalid for marriage");

        }

    }

Multiple if statement[else --- if ladder]:

    Syntax:-

                if(condition-1)

                stmt-1;

                else if(condition-2)

                stmt-2;

                else if(condition-3)

                stmt-3;

                else

                default stmt;

    Here condition is evaluated from top to bottom. As soon as true condition is found, statement associated with it is executed and  control is transferred to default stmt. There is another way of putting if's together when multipath decisions are invoked. A multipath decision is a chain of if's in which the statement associated with each else is an if.

    Program:- Write a program to calculate the result grade of a student.

    main()

    {

        float per;

        clrscr();

        printf("\nEnter percentage of the student");

        scanf("%d",&per);

        if(per>=75)

        printf("\nThe grade is distinction");

        else if(per>=60)

        printf("\nThe grade is First");

        else if(per>=45)

        printf("\nThe grade is Second");

        else if(per==35);

        printf("\nThe grade is Third");

        else

        printf("\nThe candidate is failed");

    }

The switch statement:

    If the number of alternatives are more the complexity of a program using an if statement increases much. In such condition, the switch statement is useful which has a general form as above.

    Syntax:-

                switch(expression)

                {

                    case value-1:

                                        statements block-1;

                                        break;

                      case value-2:

                                        statements block-2;

                                        break;

                        default:

                                    default block;

                                    break;

                  }

                   stmt-x;

   The expression is an integer or characters value-1, value-2 are the constants and are known as case labels. When a switch statement is executed, the expression is compared against values value-1, value-2 and so on. If a case is found whose value matches then the statements of that block is executed and control passess to stmt-x. The default is an optional statement.

    Program:- Write a program to print the gross salary of an employee considering his designation                       using switch statement.

    main()

    {

        int ch;

        float basic, da, hra, gross;

        clrscr();

        printf("1.Officer");

        printf("\n2.Senior Clerk");

        printf("\n3.Cleark");

        printf("\n4.Peon");

        scanf("%d",&ch);

        switch(ch)

        {

            case 1:

                        basic=5000;

                        hra=basic*0.2;

                        da=basic*0.5;

                        gross=basic+hra+da;

                        break;

            case 2:

                        basic=3000;

                        hra=basic*0.1;

                        da=basic*0.4;

                        gross=basic+hra+da;

                        break;

            case 3:

                        basic=2000;

                        hra=basic*0.05;

                        da=basic*0.3;

                        gross=basic+hra+da;

                        break;

            case 4:

                        basic=1000;

                        hra=basic*0.02;

                        da=basic*0.2;

                        gross=basic+hra+da;

                        break;

        }

        printf("The gross salary of the given employee is=Rs.%f",gross);

    }

The goto Statement:

   So far we have seen ways of controlling the flow of execution based on certain specified conditions. C supports the goto statement to branch uncoditionally from one point to another in the program. The goto statement is occasionally used but it is useful. It can go forwrd or backward anywhere in the program.

    Program:- Print squareroot of given number, using goto statement.

    main()

    {

        double x,y;

        clrscr();

        printf("Enter any numbere");

        read:

        scanf("%f",&x);

        if(x<0) goto read;

        y=sqrt(x);

        printf("%f%f\n"x,y);

        goto read;

        getche();

    }

Bar_line.gif (11170 bytes)

 

Prev || Home || Next