Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,890 questions

51,821 answers

573 users

How to use switch case (multiple-choice selection) in C

4 Answers

0 votes
#include <stdio.h> 

int main(int argc, char **argv) 
{
    int n;
    
    printf("Enter a number (1-3): ");
    scanf("%d", &n);
    
    switch(n)
    {
        case 1:
            puts("number = 1");
            break;
        case 2:
            puts("number = 2");
            break;
        case 3:
            puts("number = 3");
            break;
        default:
            puts("The number is not 1, 2, or 3");
    }

    return 0;
}


/*
run 1:

Enter a number (1-3): 3
number = 3

-----------------------------

run 2:

Enter a number (1-3): 5
The number is not 1, 2, or 3

*/


answered Apr 20, 2015 by avibootz
0 votes
#include <stdio.h> 

int main(int argc, char **argv) 
{
    int n;
    
    printf("Enter a number (1-3): ");
    scanf("%d", &n);
    
    // note: no break will enter to all cases from the start case
    // the start case depend on the input n, if n == 2 start case is 2 (case 2:) 
    
    switch(n)
    {
        case 1:
            puts("number = 1");
            //break;
        case 2:
            puts("number = 2");
            //break;
        case 3:
            puts("number = 3");
            //break;
        default:
            puts("The number is not 1, 2, or 3");
    }

    return 0;
}


/*
run:

Enter a number (1-3): 2
number = 2
number = 3
The number is not 1, 2, or 3

*/


answered Apr 20, 2015 by avibootz
0 votes
#include <stdio.h>

int main(int argc, char **argv) 
{ 
    char ch = 'b';
    
    switch( ch ) 
    {
        case 'a':
                printf("a");
                break;
        case 'b':
                printf("b");
                break;
        default :
                printf("default");
    }
    return 0;
}

/*
run:

b

*/

 



answered Jun 20, 2015 by avibootz
0 votes
#include <stdio.h>

int main(int argc, char **argv) 
{ 
    char ch = 'b';
    
    switch( ch ) 
    {
        case 'a':
        case 'b':
        case 'c':
                printf("a OR b OR c");
                break;
        case 'd':
                printf("b");
                break;
        default :
                printf("default");
    }
    return 0;
}

/*
run:

a OR b OR c

*/

 



answered Jun 20, 2015 by avibootz

Related questions

3 answers 283 views
5 answers 425 views
1 answer 154 views
1 answer 134 views
1 answer 171 views
...