Skip to main content

3. Selection Control Structures


Control Statements

In a pseudo-code, typically the instructions are performed one by one or line by line. But there may be situations when all the statements in a pseudo-code are not performed. Parts of pseudo-code which change the flow of instructions or change the flow of control are called as control structures.

The commonly used control structures in programming are:

If-else

  • The if-else statement is used to perform two operations for a single condition.
  • One operation is for the correctness of that condition and other is for the incorrectness of that condition.

Else if Ladder

  • Instead of using multiple if statements, we can also use else if along with if thus forming an if else-if else ladder.
  • It is used in the scenario where there are multiple cases to be performed for different conditions.
  • The conditions are evaluated from top of the ladder downwards. As soon as a true condition is encountered, the statement associated with it is executed. The remaining condition checks in the ladder will be skipped.
  1. Write a program to check whether a year is leap year or not.
  2. #include <stdio.h>
    int main()
    {
        int year;
        scanf("%d", &year);
        if (year % 100 == 0 && year % 400 == 0)
            printf("Leap year...");
        else if (year % 100 != 0 && year % 4 == 0)
            printf("Leap year...");
        else
            printf("Not a leap year...");
        return 0;
    }

Switch case

  • The switch-case statement is an alternate to if else-if ladder statement.
  • Switch-case statement is allows us to execute multiple operations for the different values of a single variable called switch variable.
  • The expression provided in the switch should result in a constant value otherwise it would not be valid.
  • Duplicate case values are not allowed.
  • The switch variable can be an integer, character or string type in Java, while it can be only integer or character in C.
  • The default statement is optional. Even if the switch case statement do not have a default statement, it would run without any problem.
  • We can write case statement in any order including the default case. That default case may be first case, last case or in between the any case in the switch case statement.
  • The break statement is used inside the switch to terminate a statement sequence. When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
  • The break statement is optional. If omitted, execution will continue on into the next case. The flow of control will fall through to subsequent cases until a break is reached.
  • Nesting of switch statements are allowed, which means we can have switch statements inside another switch. However nested switch statements should be avoided as it makes program more complex and less readable.
  1. Write a program to check whether a character is a vowel or not.
  2. #include <stdio.h>
    int main()
    {
        switch ('o')
        {
        case 'a':
        case 'A':
        case 'e':
        case 'E':
        case 'i':
        case 'I':
        case 'o':
        case 'O':
        case 'u':
        case 'U':
            printf("Vowel character");
            break;
        default:
            printf("Not a vowel character");
        }
        return 0;
    }
  3. What is the output of the following C code?
  4. #include <stdio.h>
    int main()
    {
        int x = 1;
        switch (x)
        {
        case 2:
            printf("Choice is 1");
            break;
        case 1 + 1:
            printf("Choice is 2");
            break;
        default:
            printf("Default choice");
        }
        return 0;
    }
  5. What is the output of the following C code?
  6. #include <stdio.h>
    int main()
    {
        int x = 1;
        switch (x)
        {
            x = x + 1;
        case 1:
            printf("Choice is 1");
            break;
        case 2:
            printf("Choice is 2");
            break;
        default:
            printf("Default choice");
        }
        return 0;
    }
  7. What is the output of the following C code?
  8. #include <stdio.h>
    int main()
    {
        float x = 1.1;
        switch (x)
        {
        case 1.1:
            printf("Choice is 1");
            break;
        default:
            printf("Invalid choice");
        }
        return 0;
    }
  9. What is the output of the following C code and also write the final value of i?
  10. #include <stdio.h>
    int main()
    {
        for (int i = 0; i < 20; i++)
        {
            switch (i)
            {
            case 0:
                i = i + 5;
            case 1:
                i = i + 2;
            case 5:
                i = i + 5;
            default:
                i = i + 4;
            }
            printf("%d ", i);
        }
        return 0;
    }
  11. What is the output of the following C code?
  12. #include <stdio.h>
    int main()
    {
        int money = 1;
        switch (money << (2 + money))
        {
        default:
            printf("Money value = can't evaluate");
        case 4:
            printf("Money = 4");
        case 5:
            printf("Money = 5");
        case 8:
            printf("Money = 8");
        }
        return 0;
    }
  13. What is the output of the following C code?
  14. #include <stdio.h>
    int main()
    {
        switch (2)
        {
        case 1L:
            printf("Brahmin boys");
        case 2L:
            printf("%s", "Flirt");
            goto Love;
        case 3L:
            printf("Satyanarayan");
        case 4L: Love:
            printf("Pooja");
        }
        return 0;
    }
Prev Next

Comments