C Code Snippets 4 - Do-While and Switch-Case Statements




Hello everyone, in this post I am going to share an example usage of Do-While and Switch-Case statements. 
I think the flow chart and the code are self-explanatory. If you have any questions do not hesitate to write a comment below.




/**
* Author : Berk Soysal
*/

#include <stdio.h>

int main()
{
    float entered;
    int num;

    do
    {
        printf("1-Ottawa\n");
        printf("2-Toronto\n");
        printf("3-Montreal\n");
        printf("0-None\n");

        printf("\nSelection : ");
        scanf("%f",&entered);
        num = (int) entered;

        switch (num)
        {
        case 1:
            printf("\nOttawa is the Capital of Canada\n\n");
            break;
        case 2:
            printf("\nToronto is the fourth-largest city in North America\n\n");
            break;
        case 3:
            printf("\nMontreal is the second largest French speaking city in the world\n\n");
            break;
        case 0:
            printf("\nGoodbye..\n\n");
            break;
        default :
            printf("\nPlease select a number from the list !\n\n");
            num=1;
            break;
        }
    }

    while(num<=3 && num>0);


    return 0;
} 

Result


Keywords: CCode Snippets
Author:

Software Developer, Codemio Admin

Disqus Comments Loading..