Switch case



Switch case statements test the value of a variable and compare it with multiple cases. Once the matching case found, it executes a corresponding block of code. The switch statement is a multiway branch statement that has a different identifier. If the answers don’t match with any of the expressions then it will print the default value. The basic syntax of a switch case is given below: 

 

Syntax  

switch (condition) 

{ 

    case value1: 

    block 1; 

    break; 

    case value2: 

    block 2; 

    break;  

    .... 

    .... 

    .... 

    default: 

    default block; 

    break; 

} 

 

Flowchart 

 

 

Question:- write a program to check a vowel and consonant character.  


#include <stdio.h>
#include <conio.h>
void main()
{
    char c;
    printf("enter a character (a-z or A - Z): ");
    scanf("%c", &c);
    switch (c)
    {
    case 'a':
    case 'A':
    case 'e':
    case 'E':
    case 'i':
    case 'I':
    case 'o':
    case 'O':
    case 'u':
    case 'U':
        printf("%c is vowel character!"c);
        break;
    default:
        printf("%c is consunant character!"c);
    }
    getch();
}

output 

Enter a character (a-z or A - Z): u 

u is a vowel character! 


Follow our page for regular updates.

0 Comments