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

goto Keyword In C Programming Language

In this video tutorial lets learn how goto keyword works and its syntax in C Programming Language.

Note: As for as possible lets avoid using goto keyword and lets write programs using for loop, while loop, do while loop, continue and break statements itself. Lets learn usage of goto keyword just for the sake of knowing about it.

Related Read:
Continue Statement In C Programming Language
break Statement In C Programming Language

Video Tutorial: goto Keyword In C Programming Language


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

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

Source Code: goto Keyword In C Programming Language

#include<stdio.h>

int main()
{
    int count = 1;

    while(count <= 10)
    {
        if(count == 6)
            goto six; // six is the label

        printf("%d IBM\n", count++);
    }

    six:
        printf("This is sixth loop\n");

    return 0;
}

Output
1 IBM
2 IBM
3 IBM
4 IBM
5 IBM
This is sixth loop

Working of goto Statement in C Programming Language

goto statement should always have a label associated with it. In above program label is six. Once count value is 6, control encounters goto six; Control searches for label six inside the program. Once it gets the label six, it executes the statements associated with label six.

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

do-while Loop In C Programming Language

In this video tutorial lets learn about the general syntax and working of do-while loop in C programming language.

Related Read:
while loop in C programming
For Loop In C Programming Language
Using Scanf in C Program

Video Tutorial: do-while Loop In C Programming Language


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

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

Source Code: do-while Loop In C Programming Language

#include<stdio.h>

int main()
{
    int count = 1;

    do
    {
        printf("Apple\n");
        printf("IBM\n");

    }while(count > 5);

    return 0;
}

Output
Apple
IBM

Note: Even though the while condition is false, the code inside do block gets executed atleast once.

Source Code: do-while Loop In C Programming Language

#include<stdio.h>

int main()
{
    char ch;

    do
    {
        printf("Apple\n");
        printf("IBM\n");

        printf("Do you want to continue?(y/n)");
        scanf("%c", &ch);

        fflush(stdin);

    }while(ch == 'y');

    return 0;
}

Output
Apple
IBM
Do you want to continue?(y/n)y
Apple
IBM
Do you want to continue?(y/n)y
Apple
IBM
Do you want to continue?(y/n)y
Apple
IBM
Do you want to continue?(y/n)y
Apple
IBM
Do you want to continue?(y/n)n

Working of do-while Loop

Unlike in while and for loop, in do-while loop the statements inside do block gets executed atleast once. After executing the statements present in do block atleast once, the condition present in while is checked. If while condition is true, then the block of code in do{} gets executed once again. If condition in while is false then the control exists do-while loop.

Note: Since we might start to input information from the keyboard repeatedly inside do-while block, scanf() method keeps checking the input buffer. And often times it gets confused with the input buffer and thinks that the user has pressed the enter key. To avoid that we flush out the previous buffer present in input device(ex: keyboard) using function fflush(). fflush takes stdin as argument, so that it can clear the buffer of standard input device. fflush(stdin);

Source Code: Infinite Looping in do-while Loop – In C Programming Language

#include<stdio.h>

int main()
{
    do
    {
        printf("Apple\n");
        printf("IBM\n");

    }while(1);

    return 0;
}

Output
do-while loop gets into infinite loop as the condition in while is non-zero number, which means the condition is always true.

Apple
IBM

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

C Program To Calculate Sum of First 7 Terms of Natural Logarithm

The Natural Logarithm can be approximated by the following series:

(x – 1 / x) + 1/2 (x – 1 / x)2 + 1/2 (x – 1 / x)3 + 1/2 (x – 1 / x)4 + ..

If x is input through the keyboard, write a C program to calculate the sum of first seven terms of this series.

Related Read:
For Loop In C Programming Language
Basic Arithmetic Operations In C

Video Tutorial: C Program To Calculate Sum of First 7 Terms of Natural Logarithm


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

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

Source Code: C Program To Calculate Sum of First 7 Terms of Natural Logarithm

#include<stdio.h>
#include<math.h>

int main()
{
    int count;
    float x, result = 0.0;

    printf("Enter value of x\n");
    scanf("%f", &x);

    for(count = 1; count <= 7; count++)
    {
        if(count == 1)
        {
            result = (x - 1) / x;
        }
        else
        {
            result = result + pow( (x - 1) / x, count) * 0.5;
        }
    }

    printf("Result of first 7 terms = %0.2f\n", result);

    return 0;
}

Output 1
Enter value of x
5
Result of first 7 terms = 1.98

Output 2
Enter value of x
14
Result of first 7 terms = 3.10

Logic To Calculate Sum of First 7 Terms of Natural Logarithm

According to the problem statement it is clear that we need sum of first 7 terms of Natural Logarithm. So we initialize the loop counter variable count to 1 and iterate through the for loop until count value is less than or equal to 7.

Inside for loop we calculate the value of first 7 terms in the series and store it inside variable result.

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

C Program To Draw Pyramid of Numbers, using For Loop

Lets write a C program to draw / display / print a four row pyramid formed from numbers 1 to 10.

Related Read:
Nested For Loop In C Programming Language
C Program To Draw Pyramid of Numbers, using While Loop

Video Tutorial: C Program To Draw Pyramid of Numbers, using For Loop


[youtube https://www.youtube.com/watch?v=Z-uVxVnXqgE]

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

Source Code: C Program To Draw Pyramid of Numbers, using For Loop

#include<stdio.h>
int main()
{
    int num = 4, row, col, space, count = 1;
    float i = -7;


    for(row = 1; row <= num; row++)
    {
        for(space = i; space <= (num-row); space++)
        {
            printf(" ");
        }
        for(col = 1; col <= row; col++)
        {
            printf(" %d ", count++);
        }
        i += 0.8;

        printf("\n");
    }

    return 0;
}

Output

                    1
                  2  3
                 4  5  6
               7  8  9  10

Logic To Draw Pyramid of Numbers, using For Loop

Here we already know that the Pyramid we need to print has 4 rows and is formed of numbers 1 to 10.

Outer for loop selects the row number and the number of elements to be printed for that particular selected row. For example, 1st row has 1 element. 2nd row has 2 elements. 3rd row has 3 elements and so on. So the row number and the number of elements in that particular row are always the same.

First inner for loop prints the adequate spacing required for the pyramid. Second inner for loop prints the actual numbers.

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