📚 C Programming Tutorial

C Programming

Learn C programming concepts with simple explanations, examples and practical notes.

← Back to C Programming

Loop Statements

Imagine this: you need to print “Welcome to C Programming” 100 times. Would you write printf() 100 times? No! Write the statement once and use a loop to repeat it.

A loop is a control structure that repeatedly executes a block of statements while a condition is true, or until a stopping condition is reached.

Types of loops in C

C provides three main looping statements:

  1. while:- Entry-controlled(Checks the condition first. The body may execute zero times.)
  2. do-while:- Exit-controlled(Executes the body first. The body runs at least once.)
  3. for:- Compact counting loop(Places initialization, condition, and update in one header.)

The while loop

A while loop repeats a block as long as its condition is true. It is useful when the number of repetitions may not be known in advance.

Syntax

while (condition)
{
    /* statements to repeat */
    /* update the loop variable */
}

Example: Print numbers from 1 to 5

#include <stdio.h>

int main(void)
{
    int i = 1;

    while (i <= 5)
    {
        printf("%d ", i);
        i++;
    }

    return 0;
}
Output
1 2 3 4 5

Understand the code:

  • int i = 1; starts counting from 1.
  • while (i <= 5) checks whether i is at most 5.
  • printf() displays the current value.
  • i++ increases i by 1.
  • When i becomes 6, the condition is false and the loop ends.

Think like a programmer: What happens if i++; is removed? The value stays at 1, so the condition remains true and the program keeps printing 1. This is an infinite loop.

Example: Print even numbers from 2 to 10

#include <stdio.h>

int main(void)
{
    int i = 2;

    while (i <= 10)
    {
        printf("%d ", i);
        i = i + 2;
    }

    return 0;
}
Output
2 4 6 8 10

The do-while loop

The do-while loop executes its body first and checks the condition afterward. Therefore, the body always executes at least once.

Syntax

do
{
    /* statements to repeat */
} while (condition);
Remember: There is a semicolon after while (condition);.

Example: A condition that is initially false

#include <stdio.h>

int main(void)
{
    int i = 10;

    do
    {
        printf("%d ", i);
        i++;
    } while (i <= 5);

    return 0;
}
Output
10

The body prints 10 before the condition is checked. The condition is false, so the loop stops after one execution.

The for loop

The for loop is convenient when the number of repetitions is known, such as printing numbers from 1 to 10.

Syntax

for (initialization; condition; update)
{
    /* statements to repeat */
}
  • Initialization: Sets the starting value, for example i = 1.
  • Condition: Determines whether the loop body should execute, for example i <= 5.
  • Update: Changes the loop variable, for example i++.

Example: Print numbers from 1 to 5

#include <stdio.h>

int main(void)
{
    int i;

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

    return 0;
}
Output
1 2 3 4 5

Execution order

  1. Initialization happens once.
  2. The condition is checked.
  3. If true, the body executes.
  4. The update expression executes.
  5. Control returns to the condition. If false, the loop ends.

Difference between the three loops

Featurewhiledo-whilefor
Condition checkedBefore the bodyAfter the bodyBefore each iteration
Minimum executions010
Semicolon after conditionNoYesNo
Initialization and updateUsually separateUsually separateCommonly in the header
Typical useCondition-based repetitionActions that must run onceCounting and fixed repetitions

All three loops can be used for counting or condition-based repetition. Choose the one that makes your program easiest to understand.

Nested loops

A nested loop is a loop inside another loop. It is useful for printing patterns, working with tables, and processing rows and columns.

Example: Print a star pattern

#include <stdio.h>

int main(void)
{
    int i, j;

    for (i = 1; i <= 4; i++)
    {
        for (j = 1; j <= i; j++)
        {
            printf("* ");
        }

        printf("\n");
    }

    return 0;
}
Output
*
* *
* * *
* * * *
  • The outer loop controls the number of rows.
  • The inner loop controls how many stars appear in each row.
  • printf("\n"); moves output to the next line after each row.

For four rows, the inner loop executes 1 + 2 + 3 + 4 = 10 times in total.

Important loop programs for examinations

Program 1: Sum of the first n natural numbers

#include <stdio.h>

int main(void)
{
    int n, i, sum = 0;

    printf("Enter n: ");
    scanf("%d", &n);
    if (n < 0)
    {
        printf("Please enter a non-negative integer.\n");
        return 1;
    }

    for (i = 1; i <= n; i++)
    {
        sum = sum + i;
    }

    printf("Sum = %d\n", sum);
    return 0;
}
Sample input and output
Enter n: 5
Sum = 15

The variable sum starts at zero and accumulates each number from 1 through n.

Program 2: Multiplication table

#include <stdio.h>

int main(void)
{
    int n, i;

    printf("Enter a number: ");
    scanf("%d", &n);
    
    for (i = 1; i <= 10; i++)
    {
        printf("%d x %d = %d\n", n, i, n * i);
    }

    return 0;
}
Sample input and output
Enter a number: 3
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
...
3 x 10 = 30

Program 3: Factorial of a number

#include <stdio.h>

int main(void)
{
    int n, i;
    unsigned long long fact = 1;

    printf("Enter a number (0-20): ");
    scanf("%d", &n);
    if ( n < 0 || n > 20)
    {
        printf("Enter a number from 0 to 20.\n");
        return 1;
    }

    for (i = 1; i <= n; i++)
    {
        fact = fact * i;
    }

    printf("Factorial = %llu\n", fact);
    return 0;
}
Sample input and output
Enter a number (0-20): 5
Factorial = 120

For example, 5! = 5 × 4 × 3 × 2 × 1 = 120. The factorial of 0 is 1, so fact is initialized to 1. The range is limited to 0–20 so the result fits in a typical 64-bit unsigned long long.

Common mistakes and how to avoid them

1. Infinite loop

Forgetting to update the loop variable may keep the condition true forever. Make sure the loop progresses toward termination.

2. Off-by-one error

i < 5 excludes 5, while i <= 5 includes 5. Check the boundary carefully.

3. Extra semicolon

Writing while (i <= 5); creates an empty loop body. Do not add the semicolon unless an empty body is intentional.

4. Incorrect update

If the loop should count upward, use i++ or another suitable update. An accidental i-- may prevent the loop from ending.

Debugging tip: When a loop behaves unexpectedly, trace the starting value, condition, body, and update on paper. Check what the variable becomes after every iteration.

Important questions for B.Tech examinations

Short-answer questions (2 marks)

  1. Define a loop.
  2. List the three looping statements in C.
  3. What is an entry-controlled loop?
  4. What is an exit-controlled loop?
  5. Differentiate between break and continue.
  6. What is an infinite loop?
  7. What is a nested loop?

Long-answer questions (5–10 marks)

  1. Explain while, do-while, and for loops with syntax and examples.
  2. Differentiate between the three looping statements with suitable programs.
  3. Write a C program to find the factorial of a number.
  4. Write a C program to find the sum of the first n natural numbers.
  5. Write a C program to print a multiplication table.
  6. Explain nested loops with a star-pattern program.
  7. Explain break and continue with examples.
↑ Back to top
← Previous Topic Switch Statements Next Topic → Jump Statements