break Statement In C Programming Language

In this video tutorial lets learn more about the working of break statement or break keyword in C programming language.

Related Read:
Nested For Loop In C Programming Language

Video Tutorial: break Statement In C Programming Language


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

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


Source Code: break Statement In C Programming Language : For Loop

#include<stdio.h>

int main()
{
    int i;

    for(i = 1; i <= 5; i++)
    {
        if(i == 3)
            break;

        printf("%d Apple\n", i);
    }
    printf("\nEnd of for loop.\n", i);
    return 0;
}

Output:
1 Apple
2 Apple

End of for loop.

In above C program we introduce break keyword when i is equal to 3. so for 3rd iteration of the for loop control exits the loop and whatever instructions present after the for loop gets executed.

Source Code: Continue Statement In C Programming Language : Nested For Loop

#include<stdio.h>

int main()
{
    int i, j;

    for(i = 1; i <= 5; i++)
    {
        printf("%d Apple\n", i);

        for(j = 1; j <= 3; j++)
        {
            if(j == 2)
                break;

            printf("\t%d Oracle\n", j);
        }

    }
    printf("\nEnd of for loop.\n", i);
    return 0;
}

Output:

1 Apple
        1 Oracle
2 Apple
        1 Oracle
3 Apple
        1 Oracle
4 Apple
        1 Oracle
5 Apple
        1 Oracle

End of for loop.

In above C program, inside inner for loop we’ve break keyword when j value is equal to 2. So once the keyword break is encountered, control exits the inner for loop. This doesn’t affect the outer for loop, as the keyword break is present inside the inner for loop.

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

Continue Statement In C Programming Language

In this video tutorial lets learn more about the working of continue statement or continue keyword in C programming language.

Related Read:
Nested For Loop In C Programming Language

Video Tutorial: Continue Statement In C Programming Language


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

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


Source Code: Continue Statement In C Programming Language : For Loop

#include<stdio.h>

int main()
{
    int i;

    for(i = 1; i <= 5; i++)
    {
        if(i == 3)
            continue;

        printf("%d Apple\n", i);
    }

    return 0;
}

Output:
1 Apple
2 Apple
4 Apple
5 Apple

In above C program we introduce continue keyword when i is equal to 3. So whatever the instructions / statements present after the keyword continue will be skipped and the control will be transferred to the next iteration of the for loop.

Source Code: Continue Statement In C Programming Language : Nested For Loop

#include<stdio.h>

int main()
{
    int i, j;

    for(i = 1; i <= 5; i++)
    {
        printf("%d Apple\n", i);

        for(j = 1; j <= 3; j++)
        {
            if(j == 2)
                continue;

            printf("\t%d Oracle\n", j);
        }

    }

    return 0;
}

Output:

1 Apple
        1 Oracle
        3 Oracle
2 Apple
        1 Oracle
        3 Oracle
3 Apple
        1 Oracle
        3 Oracle
4 Apple
        1 Oracle
        3 Oracle
5 Apple
        1 Oracle
        3 Oracle

In above C program, inside inner for loop we’ve continue keyword when j value is equal to 2. So once the keyword continue is encountered, control directly switches to or transfers to the next iteration of the for loop, skipping all the statements after the keyword continue.

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 Print Matrix using Nested For Loop

Lets write a simple C program to print/display a 3×5 matrix using nested for loop.

Note:
3×5 matrix means, a Matrix with 3 rows and 5 columns.

Related Read:
Nested For Loop In C Programming Language

Logic To Print Matrix using Nested For Loop

Outer for loop selects the rows. Inner for loop prints elements of that row. Next outer for loop selects the next row, and the inner for loop prints elements for that selected row. This continues until all the elements of the Matrix are printed.

Video Tutorial: C Program To Print Matrix using Nested For Loop


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

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


Source Code: C Program To Print Matrix using Nested For Loop

#include<stdio.h>

int main()
{
    int row, col;

    for(row = 1; row <= 3; row++)
    {
        for(col = 1; col <= 5; col++)
        {
            printf("\t%d", col);
        }

        printf("\n");
    }

    return 0;
}

Output:

        1       2       3       4       5
        1       2       3       4       5
        1       2       3       4       5

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

Nested For Loop In C Programming Language

In this video tutorial we’ll demonstrate the use of nested for loop in C programming language.

Related Read:
For Loop In C Programming Language
Nested While Loop: C Program

Note: For every single iteration of the outer while loop, the inner while loop completes its iterations.

A loop inside another loop is called a nested loop. Consider a nested loop where the outer loop runs x times and consists of another loop inside it. The inner loop runs y times. Then, the total number of times the inner loop runs during the program execution is x*y times.

Total Number of Iterations In Nested Loops
Number of iterations will be equal to the number of iterations in the outer loop multiplied by the number of iterations in the inner loop.

Video Tutorial: Nested For Loop In C Programming Language


[youtube https://www.youtube.com/watch?v=mAV6PW-snmI]

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


Source Code: Nested For Loop In C Programming Language

#include<stdio.h>

int main()
{
    int i, j;

    for(i = 1; i <= 3; i++)
    {
        printf("\n%d Apple\n", i);

        for(j = 1; j <= 2; j++)
        {
            printf("\t%d Oracle\n", j);
        }
    }

    return 0;
}

Output:

1 Apple
        1 Oracle
        2 Oracle

2 Apple
        1 Oracle
        2 Oracle

3 Apple
        1 Oracle
        2 Oracle

Explanation of above C source code
Here when i value is 1 – in first iteration. j value gets initialized to 1. And inner for loop executes until j value is equal to 2. For second iteration of outer for loop i value is 2, now again j value gets initialized to 1. Now the inner for loop executes until j value is equal to 2. For third iteration of outer for loop i value is 3, now again j value gets initialized to 1. Now again the inner for loop executes until j value is equal to 2.

Source Code: Nested For Loop In C Programming Language

#include<stdio.h>

int main()
{
    int i, j;

    for(i = 1; i <= 3; i++)
    {
        printf("\n%d Apple\n", i);

        for(j = 1; j <= i; j++)
        {
            printf("\t%d Oracle\n", j);
        }
    }

    return 0;
}

Output:

1 Apple
        1 Oracle

2 Apple
        1 Oracle
        2 Oracle

3 Apple
        1 Oracle
        2 Oracle
        3 Oracle

Explanation of above C source code
During first iteration of outer for loop, i value will be 1. Now j gets initialized to 1 and inner for loop iterates until j is equal to the value of i(that is 1). Now for second iteration of outer for loop i value is 2. Again j gets initialized to 1 and inner for loop executes until j value is equal to i. and so on ..

Source Code: Nested For Loop In C Programming Language

#include<stdio.h>

int main()
{
    int i, j, k;

    for(i = 1; i <= 5; i++)
    {
        printf("\n%d Apple\n", i);

        for(j = 1; j <= 3; j++)
        {
            printf("\t%d Oracle\n", j);

            for(k = 1; k <= 2; k++)
            {
                printf("\t\t%d IBM\n", k);
            }
        }
    }

    return 0;
}

Output:

1 Apple
        1 Oracle
                1 IBM
                2 IBM
        2 Oracle
                1 IBM
                2 IBM
        3 Oracle
                1 IBM
                2 IBM

2 Apple
        1 Oracle
                1 IBM
                2 IBM
        2 Oracle
                1 IBM
                2 IBM
        3 Oracle
                1 IBM
                2 IBM

3 Apple
        1 Oracle
                1 IBM
                2 IBM
        2 Oracle
                1 IBM
                2 IBM
        3 Oracle
                1 IBM
                2 IBM

4 Apple
        1 Oracle
                1 IBM
                2 IBM
        2 Oracle
                1 IBM
                2 IBM
        3 Oracle
                1 IBM
                2 IBM

5 Apple
        1 Oracle
                1 IBM
                2 IBM
        2 Oracle
                1 IBM
                2 IBM
        3 Oracle
                1 IBM
                2 IBM

Note: We can have while loop inside a for loop or a for loop inside a while loop. This is also called as nested looping.

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 Natural Numbers Between Range using For Loop

Lets write a C program to calculate sum of all natural numbers between the user entered range of numbers, using for loop.

We check if the user has entered smaller number first and then the larger number. If not, we swap the numbers present in the variables min and max.

Related Read:
For Loop In C Programming Language
C Program To Print Natural Numbers Between Two Numbers using for loop
Swap 2 Numbers Using a Temporary Variable: C

Video Tutorial: C Program To Calculate Sum of Natural Numbers Between Range using For Loop


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

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


Source Code: C Program To Calculate Sum of Natural Numbers Between Range using For Loop

 
#include<stdio.h>

int main()
{
    int min, max, temp, count, sum = 0;

    printf("Enter 2 positive numbers\n");
    scanf("%d%d", &min, &max);

    if(min > max)
    {
        temp = min;
        min  = max;
        max  = temp;
    }

    printf("\nNatural Numbers from %d to %d are:\n", min, max);

    for(count = min; count <= max; count++)
    {
        printf("%d\n", count);
        sum = sum + count;
    }

    printf("Sum of Natural Numbers from %d to %d is %d\n", min, max, sum);

    return 0;
}

Output 1:
Enter 2 positive numbers
5
10

Natural Numbers from 5 to 10 are:
5
6
7
8
9
10
Sum of Natural Numbers from 5 to 10 is 45

Output 2:
Enter 2 positive numbers
14
10

Natural Numbers from 10 to 14 are:
10
11
12
13
14
Sum of Natural Numbers from 10 to 14 is 60

Logic To Calculate Sum of Natural Numbers Between Range using For Loop

We ask the user to enter minimum and maximum number(i.e., the range) and we store it inside variable min and max. If value of min is greater than value of max, then we swap the values of min and max.

We initialize the variable count to min and iterate through the for loop until count value is less than or equal to value of max. We keep incrementing the value of count by 1 for each iteration of for loop.

Inside for loop we add the value of count to previous value of sum and once the control exits the for loop we display the value present in variable sum.

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