CHAPTER – 4
INTRODUCTION TO LOOPS
TEXTUAL QUESTIONS AND ANSWERS
1. Why do we use a loop in a C program?
Ans. Looping can be defined as repeating the same process multiple times until a specific
condition is satisfied.
1) It provides code reusability.
2) Using loops, we do not need to write the same code again and again.
3) Using loops, we can traverse over the elements of data structures (array or linked lists).
2. Do we need to use only one type of loop in a C program? Justify your answer by
writing a C program.
Ans. No, in C programming, you are not limited to using just one type of loop. Depending on
the specific requirements of a program, you can choose between different loop types,
including –
for loop.
do…while loop.
while loop.
Example:
Printing 5 times using all three types of loop.
#include <stdio.h>
int main() {
int i;
printf("Using a for loop to print numbers from 1 to 5:\n");
for (i = 1; i <= 5; i++) {
printf("%d ", i);
}
printf("\n");
printf("Using a while loop to print numbers from 6 to 10:\n");
i = 6;
, while (i <= 10) {
printf("%d ", i);
i++;
}
printf("\n");
printf("Using a do-while loop to print numbers from 11 to 15:\n");
i = 11;
do {
printf("%d ", i);
i++;
} while (i <= 15);
printf("\n");
return 0;
}
OUTPUT
Using a for loop to print numbers from 1 to 5:
1 2 3 4 5
Using a while loop to print numbers from 6 to 10:
6 7 8 9 10
Using a do-while loop to print numbers from 11 to 15:
11 12 13 14 15
(3) What will happen if we write a while loop with 1 in place of condition? Try it in a
simple C program.
Ans. If you write a ‘while’ loop with ‘1’ as the condition, the loop will run indefinitely
because ‘1’ is always true in C programming. This creates an infinite loop, and the program
will not terminate on its own.
To come out from this infinite loop, we have to use conditional statement and break
statement.
Example:
#include <stdio.h>
int main() {
INTRODUCTION TO LOOPS
TEXTUAL QUESTIONS AND ANSWERS
1. Why do we use a loop in a C program?
Ans. Looping can be defined as repeating the same process multiple times until a specific
condition is satisfied.
1) It provides code reusability.
2) Using loops, we do not need to write the same code again and again.
3) Using loops, we can traverse over the elements of data structures (array or linked lists).
2. Do we need to use only one type of loop in a C program? Justify your answer by
writing a C program.
Ans. No, in C programming, you are not limited to using just one type of loop. Depending on
the specific requirements of a program, you can choose between different loop types,
including –
for loop.
do…while loop.
while loop.
Example:
Printing 5 times using all three types of loop.
#include <stdio.h>
int main() {
int i;
printf("Using a for loop to print numbers from 1 to 5:\n");
for (i = 1; i <= 5; i++) {
printf("%d ", i);
}
printf("\n");
printf("Using a while loop to print numbers from 6 to 10:\n");
i = 6;
, while (i <= 10) {
printf("%d ", i);
i++;
}
printf("\n");
printf("Using a do-while loop to print numbers from 11 to 15:\n");
i = 11;
do {
printf("%d ", i);
i++;
} while (i <= 15);
printf("\n");
return 0;
}
OUTPUT
Using a for loop to print numbers from 1 to 5:
1 2 3 4 5
Using a while loop to print numbers from 6 to 10:
6 7 8 9 10
Using a do-while loop to print numbers from 11 to 15:
11 12 13 14 15
(3) What will happen if we write a while loop with 1 in place of condition? Try it in a
simple C program.
Ans. If you write a ‘while’ loop with ‘1’ as the condition, the loop will run indefinitely
because ‘1’ is always true in C programming. This creates an infinite loop, and the program
will not terminate on its own.
To come out from this infinite loop, we have to use conditional statement and break
statement.
Example:
#include <stdio.h>
int main() {