C Programming · Unit 5
Loops and Iteration
Exam-focused notes for Loops and Iteration (C Programming, BIT102): what the TU syllabus asks and how it has actually been tested, with 6 solved past questions from this unit.
Loops and Iteration in short
Loops repeat a block of statements. They save effort when the same work must be done many times, such as reading marks or printing a pattern.
What this unit covers
- For loop structure and syntax
- While loop
- Do while loop
- Comparison of while and do while
- Nested loops
- Loop control statements
- Odd loops
Loop control statements
2081 · 10 marks
What is loop? Differentiate between break and continue statement.Write a program to find sum of first N natural numbers.[6+4]
Show solved answerHide answer
Loop, Break vs Continue, and Sum of N Natural Numbers
What is a Loop?
A loop is a control structure that allows repeated execution of a block of statements as long as a given condition remains true. Loops are used to avoid writing the same code multiple times.
Types of Loops in C:
| Loop Type | Description |
|---|---|
for loop | Used when the number of iterations is known |
while loop | Entry-controlled; checks condition before executing |
do-while loop | Exit-controlled; executes at least once |
Difference Between break and continue Statement
| Feature | break | continue |
|---|---|---|
| Function | Terminates the entire loop immediately | Skips the current iteration only |
| Control Transfer | Transfers control outside the loop | Transfers control to the loop's condition check |
| Effect on Loop | Loop stops completely | Loop continues with next iteration |
| Use Case | Exit loop when a condition is met | Skip specific values during iteration |
Example of break:
for(int i = 1; i <= 10; i++) {
if(i == 5)
break; // loop ends when i reaches 5
printf("%d ", i);
}
// Output: 1 2 3 4
Example of continue:
for(int i = 1; i <= 10; i++) {
if(i == 5)
continue; // skips i=5, loop keeps running
printf("%d ", i);
}
// Output: 1 2 3 4 6 7 8 9 10
Conclusion:
breakexits the loop entirely, whilecontinueonly skips the current iteration and the loop keeps running.
Program to Find Sum of First N Natural Numbers
The sum of first N natural numbers means adding all integers from 1 to N.
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter the value of N: ");
scanf("%d", &n);
for(i = 1; i <= n; i++) {
sum = sum + i; // accumulate sum
}
printf("Sum of first %d natural numbers = %d\n", n, sum);
return 0;
}
Sample Output:
Enter the value of N: 5
Sum of first 5 natural numbers = 15
Explanation (Dry Run for N = 5):
| Iteration | i | sum = sum + i |
|---|---|---|
| 1 | 1 | 0 + 1 = 1 |
| 2 | 2 | 1 + 2 = 3 |
| 3 | 3 | 3 + 3 = 6 |
| 4 | 4 | 6 + 4 = 10 |
| 5 | 5 | 10 + 5 = 15 |
The for loop runs from i = 1 to i = N, adding each value of i to the variable sum. After the loop ends, the final sum is displayed.
2081 · 5 marks
Write a program to check whether a given number is Armstrong number or not. [5]
0 · 5 marks
Define iterative statements. Write a program that using a loop to compute and prints the sum of squares of first 10 even natural numbers. [5]
For loop structure and syntax
2080 · 5 marks
Explain structure of for loop. Write a program to reverse a number entered by user. [5]
Comparison of while and do while
2079 · 10 marks
When do we use for loop and while loop? Compare the while and do while loop with suitable example.Write a program to find average, minimum and maximum age in a class of 20 students.[4+6]
Odd loops
2081.2 · 10 marks
Define odd loop. What do you mean by goto and label? Write a program to generate the following pattern.
1
2 2
3 3 3
4 4 4 4
[10]
Show solved answerHide answer
Answer: Odd Loop, Goto & Label, and Pattern Program
A. Odd Loop
An odd loop is a loop in which the number of iterations is not known in advance at compile time. The loop continues or terminates based on a condition that is evaluated at runtime, often depending on user input or some external event.
Key Characteristics:
- The exact number of repetitions is unpredictable before execution
- The loop termination depends on a runtime condition
- Common in interactive programs (e.g., "do you want to continue?")
Example:
#include <stdio.h>
int main() {
char ch = 'y';
int data;
while (ch == 'y') {
printf("Enter a number: ");
scanf("%d", &data);
printf("You entered: %d\n", data);
printf("Continue? (y/n): ");
scanf(" %c", &ch);
}
return 0;
}
Here, the loop may run 1 time or 100 times depending on user response.
B. Goto and Label
goto Statement
goto is an unconditional branching statement in C that transfers program control directly to a specified labeled statement, without evaluating any condition.
Syntax:
goto label_name;
Label
A label is a user-defined identifier followed by a colon (:) that marks a specific position in the program to which control can be transferred using goto.
Syntax:
label_name:
statement;
Key Points:
- The
gotoand its corresponding label must be in the same function - It causes an unconditional jump to the labeled statement
- Overuse of
gotoleads to spaghetti code (unstructured, hard to read/debug) - Generally avoided in modern structured programming
Example:
#include <stdio.h>
int main() {
printf("Hello\n");
goto skip;
printf("This line is skipped\n"); // never executes
skip:
printf("World\n");
return 0;
}
Output:
Hello
World
C. Program to Generate the Pattern
Required Pattern:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
(The pattern shows each number i printed exactly i times on row i)
Program in C:
#include <stdio.h>
int main() {
int i, j;
for (i = 1; i <= 5; i++) {
for (j = 1; j <= i; j++) {
printf("%d ", i);
}
printf("\n");
}
return 0;
}
Explanation:
| Component | Purpose |
|---|---|
Outer loop i (1 to 5) | Controls the row number |
Inner loop j (1 to i) | Controls how many times i is printed |
printf("%d ", i) | Prints the current row number |
printf("\n") | Moves to the next line after each row |
Dry Run:
| i | j runs | Output |
|---|---|---|
| 1 | j = 1 | 1 |
| 2 | j = 1, 2 | 2 2 |
| 3 | j = 1, 2, 3 | 3 3 3 |
| 4 | j = 1, 2, 3, 4 | 4 4 4 4 |
| 5 | j = 1, 2, 3, 4, 5 | 5 5 5 5 5 |
Final Output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Conclusion:
- An odd loop handles unknown iteration counts at runtime
- goto provides unconditional jump to a label within the same function, but should be used sparingly
- Nested for loops are the standard and efficient approach for generating number patterns like the one above
Make Unit 5 stick
Practice BIT102 with flashcards & quizzes