Switch Case Default In C Programming Language

In this video tutorial lets learn how switch case decision control statement works and its syntax in C Programming Language.

Related Read:
Simple Calculator Program using Switch Case: C
break Statement In C Programming Language
Continue Statement In C Programming Language

Note: The condition in switch must be a integer constant or an expression which evaluates to an integer constant.

Video Tutorial: Switch Case Default In C Programming Language


[youtube https://www.youtube.com/watch?v=XklMeJ5ha1A]

YouTube Link: https://www.youtube.com/watch?v=XklMeJ5ha1A [Watch the Video In Full Screen.]

Source Code: Switch Case Default In C Programming Language

#include<stdio.h>

int main()
{
    char choice;

    printf("Enter your choice\n");
    scanf("%c", &choice);

    switch(choice)
    {
        case 'a':
        case 'A': printf("You typed A or a\n");
                  break;

        case 'b':
        case 'B': {
                   printf("You typed B or b\n");
                   break;
                  }

        default: printf("Your selection is wrong!\n");
    }

    return 0;
}

Output 1
Enter your choice
B
You typed B or b

Output 2
Enter your choice
b
You typed B or b

Here we’ve not specified keyword break after case ‘a’ and case ‘b’, that means if user enters a or A, for both whatever is present in case ‘A’ gets executed and then control exits switch case when it encounters break statement. Similarly for ‘b’ and ‘B’.

Also note that curly braces are optional here. Infact its unnecessary to use curly braces in switch case statements.

#include<stdio.h>

int main()
{
    int choice;

    printf("Enter your choice\n");
    scanf("%d", &choice);

    switch(choice)
    {
        case 1: printf("This is first statement\n");

        case 2: printf("This is second statement\n");

        default: printf("Your selection is wrong!\n");
    }

    return 0;
}

Output 1
Enter your choice
1
This is first statement
This is second statement
Your selection is wrong!

Output 2
Enter your choice
2
This is second statement
Your selection is wrong!

Here we’re not using break after any case, so it’s printing statements present in all the cases.

#include<stdio.h>
int main()
{
    int choice;

    printf("Enter your choice\n");
    scanf("%d", &choice);

    switch(choice)
    {
        case 1: printf("This is first statement\n");
                break;

        case 2: printf("This is second statement\n");
                break;

        default: printf("Your selection is wrong!\n");
    }

    return 0;
}

Output 1
Enter your choice
1
This is first statement

Output 2
Enter your choice
2
This is second statement

Output 3
Enter your choice
a
Your selection is wrong!

#include<stdio.h>
int main()
{
    int choice;

    printf("Enter your choice\n");
    scanf("%d", &choice);

    switch(choice)
    {
        case 2: printf("Apple\n");
                break;

        case 1: printf("IBM\n");
                break;

        default: printf("Your selection is wrong!\n");
    }

    return 0;
}

Output 1
Enter your choice
1
IBM

Here order doesn’t matter. As you can see from above example, case 2 comes before case 1.

#include<stdio.h>
int main()
{
    int choice;

    printf("Enter your choice\n");
    scanf("%d", &choice);

    switch(choice)
    {
        printf("List of companies\n");
        case 2: printf("Apple\n");
                break;

        case 1: printf("IBM\n");
                break;

        default: printf("Your selection is wrong!\n");
    }

    return 0;
}

Output 1
Enter your choice
1
IBM

All the code must go inside any of the cases orelse those code will not have any effect. In above program the printf statement with “List of companies” doesn’t get printed and it doesn’t even throw any error.

Note:
1. default case is optional.
2. Value of cases in switch must always be unique.
3. switch can not be used with float, double.
4. Switch, case, default are all builtin keywords.
5. continue does not take the control back to the start of switch.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

if else statement in C

In our previous video tutorial we saw how to use the decision control instruction if. In this tutorial we shall see how we can use if else statement in division of 2 numbers example.

We illustrate the use of if else statement in this video tutorial with the help of a division of 2 numbers example program.

Division of 2 Numbers: if else construct in C

Single statement in both if and else block

 
#include < stdio.h >

int main()
{
    int a, b;

    printf("Enter value of a and b for division (a/b)\n");
    scanf("%d%d", &a, &b);

    if( b != 0)
        printf("Division of %d and %d is %d\n", a, b, a/b);
    else
        printf("You can't divide a number by 0\n");

    return 0;
}

Output 1:
Enter value of a and b for division (a/b)
10
2
Division of 10 and 2 is 5

Output 2:
Enter value of a and b for division (a/b)
50
0
You can’t divide a number by 0

Note: When condition for if is true, if block gets executed. When condition in if is false, else block code gets executed.

Multiple statements in both if and else block

 
#include < stdio.h >

int main()
{
    int a, b, c;

    printf("Enter value of a and b for division (a/b)\n");
    scanf("%d%d", &a, &b);

    if( b != 0)
    {
        c = a / b;
        printf("Division of %d and %d is %d\n", a, b, c);        
    }
    else
    {
       printf("You can't divide a number by 0\n");
       printf("Please run the program once again and give proper value\n");
    }


    return 0;
}

Output 1:
Enter value of a and b for division (a/b)
10
2
Division of 10 and 2 is 5

Output 2:
Enter value of a and b for division (a/b)
50
0
You can’t divide a number by 0
Please run the program once again and give proper value

In above example we are enclosing if and else block code in curly braces as both if and else has multiple lines of code or block of code to be executed.

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

if else Construct in C: Division of 2 numbers logic


[youtube https://www.youtube.com/watch?v=WLLJn3hcFa0]

YouTube Link: https://www.youtube.com/watch?v=WLLJn3hcFa0 [Watch the Video In Full Screen.]


This video was produced as building block for our simple calculator application.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Decision Control Instruction In C: IF

So far we’ve seen that the code in C program gets executed in sequential order i.e., as and when the code appears in the source code. But today lets see how we can change the sequence of or order of execution using decision control instruction like if statement.

General form of if statement

if( some_condition )
statement;

By default, if statement executes only the next instruction present under it. But if you want to execute some block of code, then you must enclose it inside curly braces as shown below.

if( some_condition )
{
statement_1;
statement_2;
some_logic;
function_calls;
etc;
}

in above code, some_condition can be anything from a simple true(non-zero number) or false(0) or any expression returning 0 or any non-zero number.

Statements inside if statements curly braces can be anything from multi-line statements, function calls, some arithmetic operations etc.

 
#include < stdio.h >

int main()
{
    int a;
    printf("IBM\n");
    printf("Enter value for a \n");
    scanf("%d", &a);

    if(a > 10)
    {
        printf("Microsoft\n");
        printf("Oracle\n");
    }

    printf("Amazon\n");
    printf("Apple\n");    

    return 0;
}

Output:
IBM
Enter value for a
5
Amazon
Apple

Here user entered value for variable a is less than 10. So the code inside if block doesn’t get executed. So printing of Microsoft and Oracle gets skipped.

 
#include < stdio.h >

int main()
{
    int a;
    printf("IBM\n");
    printf("Enter value for a \n");
    scanf("%d", &a);

    if(a > 10)
    {
        printf("Microsoft\n");
        printf("Oracle\n");
    }

    printf("Amazon\n");
    printf("Apple\n");    

    return 0;
}

Output:
IBM
Enter value for a
14
Microsoft
Oracle
Amazon
Apple

For the same program, user entered value for variable a as 14, which is greater than 10. So all the company names gets printed on the console window as shown in above output section.

We can even remove condition statement inside if statement

 
#include < stdio.h >

int main()
{
    int a;
    printf("IBM\n");
    printf("Enter value for a \n");
    scanf("%d", &a);

    if(a)
    {
        printf("Microsoft\n");
        printf("Oracle\n");
    }

    printf("Amazon\n");
    printf("Apple\n");    

    return 0;
}

Output 1:
IBM
Enter value for a
1
Microsoft
Oracle
Amazon
Apple

Output 2:
IBM
Enter value for a
0
Amazon
Apple

Output 3:
IBM
Enter value for a
-5
Microsoft
Oracle
Amazon
Apple

In above example, when user enters 0 the condition becomes false, so the block of code inside if statement doesn’t get executed. When the user enters any number other than 0, its considered as true, so the code inside if statement gets executed.

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

Decision Control Instruction In C: IF


[youtube https://www.youtube.com/watch?v=ih-gHCBoQ0U]

YouTube Link: https://www.youtube.com/watch?v=ih-gHCBoQ0U [Watch the Video In Full Screen.]


For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert