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:
- while:- Entry-controlled(Checks the condition first. The body may execute zero times.)
- do-while:- Exit-controlled(Executes the body first. The body runs at least once.)
- 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;
}
1 2 3 4 5
Understand the code:
int i = 1;starts counting from 1.while (i <= 5)checks whetheriis at most 5.printf()displays the current value.i++increasesiby 1.- When
ibecomes 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;
}
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);
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;
}
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;
}
1 2 3 4 5
Execution order
- Initialization happens once.
- The condition is checked.
- If true, the body executes.
- The update expression executes.
- Control returns to the condition. If false, the loop ends.
Difference between the three loops
| Feature | while | do-while | for |
|---|---|---|---|
| Condition checked | Before the body | After the body | Before each iteration |
| Minimum executions | 0 | 1 | 0 |
| Semicolon after condition | No | Yes | No |
| Initialization and update | Usually separate | Usually separate | Commonly in the header |
| Typical use | Condition-based repetition | Actions that must run once | Counting 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;
}
* * * * * * * * * *
- 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;
}
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;
}
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;
}
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.
Important questions for B.Tech examinations
Short-answer questions (2 marks)
- Define a loop.
- List the three looping statements in C.
- What is an entry-controlled loop?
- What is an exit-controlled loop?
- Differentiate between
breakandcontinue. - What is an infinite loop?
- What is a nested loop?
Long-answer questions (5–10 marks)
- Explain
while,do-while, andforloops with syntax and examples. - Differentiate between the three looping statements with suitable programs.
- Write a C program to find the factorial of a number.
- Write a C program to find the sum of the first
nnatural numbers. - Write a C program to print a multiplication table.
- Explain nested loops with a star-pattern program.
- Explain
breakandcontinuewith examples.