Skip to main content

Command Palette

Search for a command to run...

Playing with the 'switch'

Understanding the Behavior of the Switch Statement in C

Updated
1 min readView as Markdown
#include<stdio.h>
int main()
{
    int i=10;

    switch(i);//does nothing
    switch(i){
        printf("Inside switch");
    }

    return 0;
}

#include<stdio.h>
int main()
{
    int i=10;

    switch(i);//does nothing

    switch(i){//does nothing
    }
    switch(i){//does nothing
        case 1:
    }
    switch(i){//does nothing
        case 1:
        printf("\n\n\t Inside case 1 of switch");
    }
    switch(i){
        default: 
        printf("\n\n\t Inside default");
    }
    switch(i){
        case 10:
        printf("\n\n\t Inside case 10 of switch");
    }
    return 0;
}

#include<stdio.h>
int main()
{
    int a;
    printf("Enter a number:");
    scanf("%d",&a);
    printf("\n");
    switch (a%7) {
        default: printf("\n\n\t It is not a multiple of 7");
        case 0: printf("\n\n\t It is a multiple of 7");
    }
}

#include<stdio.h>
int main()
{
    int a;
    printf("\n\n\t Enter a number:");
    scanf("%d",&a);
    printf("\n");
    switch (a%7) {
        default:
            printf("\n\n\t It is not a multiple of 7");
            break;
        case 0: printf("\n\n\t It is a multiple of 7");
    }
}

9 views

More from this blog

P

Programming Tutorials

89 posts