Loading . . .

C Programming in VS Code

Printing a message in C

Beginner's Guide to Keywords, Identifiers, Constants, Variables, Datatypes in C

Operators & Expressions in C

Input & Output Operations in C

Control Statements in C

Loops in C

Array in C

Character Array & Strings in C

User Defined Functions in C

Structure and Union in C

Pointers in C


Disclaimer: Educational Purposes Only

The content on this website, including videos and code examples, is for educational purposes only. All demonstrations and designs are fictional and created to illustrate coding techniques. Any resemblance to existing websites or brands is purely coincidental.

The creators and administrators of this website do not claim ownership or affiliation with any existing websites or companies. Users are encouraged to use the information responsibly for learning purposes. Liability for any misuse of the content provided is not accepted.

Download Notes

Loops in C

Loops: Execution of the sequence of statements repeatedly until some condition for termination is satisfied.

A looping process includes the following steps:

1) Setting the initialization of the condition variable.

2) Execution of the statements in the loop.

3) Test for a specified value of the condition variable for the execution of the loop.

4) Incrementing or updating the condition variable.

Loops in C:

1) While loop: It is an entry-controlled loop that test-condition first and then executes the body of the loop if the condition is true.

2) Do while loop: It executes the body of the loop first and then evaluates the test-condition in the subsequence while statement.

3) For loop: It is a more efficient loop structure in C programming.

Nesting of loops: Nesting of loops, that is, one loop statement within another loop statement. The nesting may continue to any desired level. C allows up to 15 levels of nesting. The outer loop controls the rows while the inner loop controls the columns.

Selecting a Loop:

1) Analyse the problem and see whether it required a pre-test or post-test loop.

2) If it requires a post-test loop, then we can use only one loop, do-while.

3) If it requires a pre-test loop, then we have two choices: while or for.

4) Decide whether the loop termination requires counter-based control or sentinel-based control.

5) Use for loop if the counter-based control is necessary.

6) Use a while loop if the sentinel-based control is necessary.

7) Note that both the counter-controlled and sentinel-controlled loops can be implemented by all three looping structures.

Jumping out of a loop: Terminates the loop and takes the program control to the statement immediately following the loop.

Skipping a part of a loop: Skips the remaining parts of the loop and takes the program control to the next loop iteration.

Loops in C

While loop:

while_loop.c


#include <stdio.h>

void main() {
    int sum = 0, count = 1, number;
    printf("Enter number to get the sum upto that number: ");
    scanf("%d", &number);
    while(count <= number) {
        sum += count; // or you write [" sum = sum + count; "]
        count++;
    }
    printf("\nSum = %d", sum);
}

Do while loop:

do_while_loop.c


#include <stdio.h>

void main() {
    int row = 1, number, value;
    printf("Enter number to get table: ");
    scanf("%d", &number);
    do {
        value = row * number;
        printf("%d x %d = %d\n", number, row, value);
        row++;
    } while(row<= 10);
}

For loop:

for_loop.c


#include <stdio.h>

void main() {
    int number, result_value = 1;
    printf("Enter a number to get the exponential value of it: ");
    scanf("%d", &number);
    for(int i = 1; i < 11; i++) {
        result_value *= number; // you can write as [" result_value = result_value * number; "]
        printf("\n%d to the power %d = %d", number, i, result_value);
    }
}

Nesting of loops:

nesting_of_loops.c


#include <stdio.h>

void main() {
    printf("Table from 1 to 10\n");
    for(int i = 1; i <= 10; i++) {
        for(int j = 1; j <= 10; j++) {
            printf("%d\t", i * j);
        }
        printf("\n");
    }
}

Skipping iteration and Jumping out of loop:

continue_break_in_loop.c


#include <stdio.h>

void main() {
    int i = 0;
    for (i = 0; i < 10; i++) {
        if(i % 2 == 0) {
            continue;
        } else if(i == 9) {
            break;
        } else {
            printf("\n%d", i);
        }
        printf("\n");
    }
}