Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Document preview thumbnail
Preview 2 out of 9 pages
Class notes

Question and answer of computer

Document preview thumbnail
Preview 2 out of 9 pages

Textbooks question and answer of computer chapter 4

Content preview

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() {

Document information

School year
4
Uploaded on
August 5, 2026
Number of pages
9
Written in
2026/2027
Type
Class notes
Professor(s)
Mehak
Contains
All classes
$10.99

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
0
Followers
0
Items
2
Last sold
-



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions