Wednesday, January 20, 2016

Switch Case and a simple program in C language

C Programming switch Statement

Decision making are needed when, the program encounters the situation to choose a particular statement among many statements. If a programmer has to choose one block of statement among many alternatives, nested if...else can be used but, this makes programming logic complex. This type of problem can be handled in C programming using switch statement.

A simple Program:

#include<stdio.h>                                 //header file
//Author: Humaun Kabir 

main()                                              //main function
{
    char gradeName;                                 //variable declaration
    printf("Enter your Grade:");               
    scanf("%c",&gradeName);                         //taking input from user
     
        switch(gradeName){                          //start point of switch case
            case 'A':
            case 'a':
                printf("Excellent\n");
                break;
            case 'b':
            case 'B':
                printf("Good\n");
                break;
            case 'c':
            case 'C':
                printf("Less good\n");
                break;
            case 'd':
            case 'D':
                printf("Partially good\n");
                break;
            case 'f':
            case 'F':
                printf("Very bad\n");
            default:
                printf("Keyword not found\n");
                break;
        }                                               //end of switch case
}                                                       //end of program

No comments:

Post a Comment

Thank you for commenting. Please wait for response :)