Loop control statements in C

Introduction:
Loop control statements in C are used to execute a block of code repeatedly until a specific condition is met. They help reduce code repetition and make programs efficient. The main loop control structures in C are for, while, and do-while loops. These loops are essential for tasks that require iteration, such as processing arrays, calculating sums, or running repeated checks.

This notes are provided by Study Shades Classes, Latur.

Loop control statement in C

Phases of a Loop:-
Looping construct has following three phases:
1. Initialization phase
2. Condition phase
3. Increment/Decrement phase

Three types of Loops:-
1. For loop
2. While loop
3. Do-while loop

1. For loop:- It is a looping statement which repeat again & again till some condition is satisfied. It contains three parameter in a single line.
First the condition is checked & if condition is true, then the body of loop is executed.

Syntax
for (initialization; condition; increment/decrement) {
// Code to be executed repeatedly
}

Example

#include <stdio.h>

void main() {
    int i;

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

 output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

2. While loop:- The while loop is an entry-controlled loop that executes a block of code repeatedly as long as the given condition is true. The condition is checked before each iteration, so if it’s false initially, the loop body may not execute at all. It is commonly used when the number of iterations is not known in advance.

Syntax
while (condition) {
// code to be executed
}

Example

main()
{
int=i;
    while(i<=10)
    {
    printf(%d\t”,i);
    i=i+1;
    }
getch();
}

3. Do-while loop:-It is exit control loop. First the statements are executed &
then the condition is checked. The statements are executed once even if the
condition fails at the beginning

Syntax:-
do
{
Statement1;
Statement2;
}
while(condition);

void main() {
    int i = 1;

    do {
        printf("Hello\n");
        i++;
    } while (i <= 3);
}


Difference between while loop & do-while loop

While LoopDo While Loop
Entry –controlled loopExit –controlled loop
Test condition is checked before
execution of statement
Test condition is checked after
execution of statement
If test condition fails, body of loop
is not executed
Body of loop is executed once even
if test condition fails at first run.
Semicolon is not used after while
statement.
Semicolon is used after while
Syntax:
Initialize counter;
While(test counter)
{
Statement;
Statement;
Increment/decrement counter;
}
Syntax;
Do
{
Statement 1;
Statement 2;
}
While(condition);

goto Statement in C

goto statement is used to change the normal sequence of program. It takes
the control to some other part of the program.

syntax:-
goto label;
label;
statement;

we can transfer control either forward & backwards.

Forward jump:-
When we take the control forward by using goto statement. It is called
forward jump.

For example

#include <stdio.h>

void main() {
    goto forward;  // Forward jump

backward:
    printf("This is the backward jump.\n");
    return;

forward:
    printf("This is the forward jump.\n");
    goto backward;  // Backward jump
}

output:
This is the forward jump.
This is the backward jump.

Nesting loops

Nesting loops means placing one loop inside another. In C, you can nest for, while, or do-while loops. The inner loop runs completely for each iteration of the outer loop. Nested loops are commonly used in tasks like printing patterns, working with matrices, or handling multi-dimensional arrays

Example

#include <stdio.h>

void main() {
    int i, j;

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

output
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
i = 2, j = 2
i = 2, j = 3

Break statement

Break statement is used to exit from a loop. It can be used with while loop,
do-while, if statement & switch statement.

Example

#include <stdio.h>

void main() {
    int i;

    for (i = 1; i <= 5; i++) {
        if (i == 3)
            break;
        printf("i = %d\n", i);
    }
}

output:
i = 1
i = 2

Continue statement

The continue statement is used inside loops to skip the current iteration and jump to the next one. When encountered, it stops the remaining code inside the loop for that iteration and continues with the next cycle. It’s commonly used when certain conditions require skipping part of the loop body.

#include <stdio.h>

void main() {
    int i;

    for (i = 1; i <= 5; i++) {
        if (i == 3)
            continue;
        printf("i = %d\n", i);
    }
}

Output:
i = 1
i = 2
i = 4
i = 5

Conclusion:
Loop control statements in C like for, while, and do-while help execute code repeatedly with ease. They make programs efficient and reduce repetition. Understanding how and when to use loops, along with break and continue, is essential for writing clean and logical C programs.

Notes provided by STUDY SHADES CLASSES, Latur (Prof. KAZI A S M)

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top