Selection Or Decision Structure (Control Statements)

 

Introduction

In C all the statements are executed in a sequence as they appeared. But sometimes we may transfer control to the different parts of the program which is possible with the help of control structure i.e. decision and iteration. Control structures are used to specify the order of sequence into the different parts of the program which determines the flow of control. It defines how the flow of control is transferred into the different parts of the program. This is also known as selection or decision Structure. This is the type of program logic or structure that is used to make an alternative choice according to the given condition. The condition is used to make decisions in terms of true or false. If, If…else, switch….case, etc. statements are used to make selection logic.

 

Flow Chart of Selection Structure

Flow Chart of Selection Structure
 

Example

WAP To check odd/ even numbers using C language
 
    #include <stdio.h>
    #include <conio.h>
    void main()
    {
        int num, r;
        printf("Enter any number");
        scanf("%d", &num);
        r = num % 2;
        if (r == 0)
        {
            printf("even number");
        }
        else
        {
            printf("odd number");
        }
        getch();
    }
 

Control Statements

The statements which are used to transfer the flow of the program from one statement line to another statement within the program with certain conditions are known as control statements. The following are the common conditional statements used in C programming.
 

a. if Statement

The if statement is used to evaluate conditional expressions. The general form of if statement is;
 
        if (condition)
        {
            // statement1;
            // statement 2;
            // statement;
        }
 
Examples:
 
// WAP To find out commission using C program
#include <stdio.h>
#include <conio.h>
void main()
{
    float sales, commission;
    printf("Enter total sales made: ");
    scanf("%f", &sales);
    if (sales >= 8000)
    {
        commission = 0.08 * sales;
    }
    printf("commission is: %f", commission);
    getch();
}
 
 

b. if… …else Statement

This statement is called a bi-directional statement because when the condition is true then the control is transferred to one part of the program and when the condition is false then control is transferred to another part of the program.
 
Syntax:
 
 
    if (condition)
    {
    statement;
    …………;
    }
    else
    {
    statement;
    ……….;
    }
 
 
if... ...else Statement flowchart
 
If we declare another if…else statement in the block of if or the else block, this is known as nesting if…else statements.
 
Syntax
 
 
    if (condition)
    {
        if (condition)
            statement;
        else
            statement;
    }
    else
    {
        if (condition)
            statement;
        else
            statement;
    }
 
 
Example
 
 
    //WAP  To find largest among two numbers using C language
    #include <stdio.h>
    #include <conio.h>
    void main()
    {
    int a, b;
    printf("enter two number: ");
    scanf("%d%d", &a, &b);
    if (a > b)
    {
    printf("larger number is = %d", a);
    }
    else
    {
    printf("larger number is = %d", b);
    }
    getch();
    }
 
 
 
      // WAP to find largest number from three given numbers using C language
    #include <stdio.h>
    #include <conio.h>
    main()
    {
        int a, b, c, large;
        printf("enter three numbers: ");
        scanf("%d%d%d", &a, &b, &c);
        if (a > b)
        {
            if (a > c)
            {
                large = a;
            }
            else
            {
                large = c;
            }
        }
        else
        {
            if (b > c)
            {
                large = b;
            }
            else
            {
                large = c;
            }
        }
        printf("largest numberr is %dn", large);
        getch();
    }
 
 
 

c. if…else, else if…else Statement

This is the type of nesting in which there is an if…else statement in every else part except the last else part. This type of nesting is frequently used in programs and is known as else if ladder.
 
Syntax:
 
 
      if (condition 1)
    statement a;
else if (condition 2)
    statement b;
else if (condition 3)
    statement c;
else
    statement d;
 
if...else, else if...else Statement flowchart
 
        // WAP to find out the grade of a student when the marks of 5 subject are given, the ranges of grade are assign below.
        // Per>=85                                grade=a
        // per<85 and per>=70              grade=b
        // per<70 and per>=55              grade=c
        // per<55 and per>=40              grade=d
        #include <stdio.h>
        #include <conio.h>
        main()
        {
            float m1, m2, m3, m4, m5, total, per;
            char Grade;
            printf("Enter marks of 5 subjects: ");
            scanf("%f%f%f%f%f", &m1, &m2, &m3, &m4, &m5);
            total = m1 + m2 + m3 + m4 + m5;
            if (m1 >= 35 && m2 >= 35 && m3 >= 35 && m4 >= 35 && m5 >= 35)
            {
                printf("Student is passed");
                per = total / 5;
                if (per >= 85)
                {
                    Grade = 'a';
                }
                else if (per >= 70)
                {
                    Grade = 'b';
                }
                else if (per >= 55)
                {
                    Grade = 'c';
                }
                else if (per >= 40)
                {
                    Grade = 'd';
                }
                else
                {
                    printf("No grade");
                }
            }
            else
            {
                printf("You are failed");
            }
            printf("percentage is %fn grade is%cn", per, Grade);
            getch();
        }
 
 

d. goto Statement

This is an unconditional control statement that transfers the flow of control to another part of the program.
 
Syntax:
 
        goto label;
        ............;
        ............;
        label:
        Statements;
        ........;
        ...........;
 
 
Example:
 
 
          //WAP to find odd or even using goto statement in C programming
          #include <stdio.h>
        
          void main()
          {
              int num;
        
              printf("Enter a numbern");
              scanf("%d", &num);
      
              if (num % 2 == 0)
                  goto even;
              else
                  goto odd;
        
          even:
              printf("%d is evenn", num);
              exit(0);
          odd:
              printf("%d is oddn", num);
          }
 
 

e. break statement

The break statements are used inside loops and switch statements. In some programs, if we want to exit from the loop before terminating it we can use this statement. This statement causes an immediate exit from that loop in which this statement appears. It can be written as;
 
 
    
        #include <stdio.h>
          void main()
          {
              int n;
              for (n = 1; n <= 5; n++)
              {
                  if (n == 3)
                  {
                      printf("I break the display of number n");
                      break;
                  }
              }
              printf("number =%dn", n);
              getch();
          }
 

f. Continue Statements

The continue statements are used when we want to go to the next iteration of the loop after skipping some statements of the loop. This statement can be written as;
 
 
          #include <stdio.h>
            void main()
            {
                int n;
                for (n = 1; n <= 5; n++)
                {
                    if (n == 3)
                    {
                        printf("I break the display of number only for 3 n");
                        continue;
                    }
                }
                printf("number =%dn", n);
                getch();
            }
 
 

g. Switch Statement

This is a multi-directional conditional control statement. Sometimes there is a need in the program to make choice among a number of alternatives. For making this choice, we use the switch statements. This can be written as:
 
 
        switch (expression)
        {
        case constant1:
        statement1;
        break;
        case constant2:
        statement2;
        break;
        case constant3:
        statement3;
        break;
        default:
        statement;
        }
 
 
Example of the switch statement
 
 
          #include <stdio.h>
            int main()
            {
                 int i=2;
                 switch (i)
                 {
                      case 1:
                         printf("Case1 ");
                         break;
                      case 2:
                         printf("Case2 ");
                         break;
                      case 3:
                         printf("Case3 ");
                         break;
                      case 4:
                         printf("Case4 ");
                         break;
                      default:
                         printf("Default ");
                 }
                 return 0;
            }
 
 
 

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *