Apply for Zend Framework Certification Training

c

< 10 Scenario-Based Questions on Decision-Making Statements in C Last >




Loops in C
A loop in C is a programming construct that allows a particular block of statements to be executed repeatedly. Instead of writing the same statement many times, we can place it inside a loop and control how many times it should run.
For example, suppose we want to display a message three times. Without using a loop, we would have to write printf() repeatedly:
#include <stdio.h>
int main() {
    printf("Hello C\n");
    printf("Hello C\n");
    printf("Hello C\n");
    return 0;
}
Output
Hello C
Hello C
Hello C
This approach works for a few repetitions, but imagine printing the same message 100, 500, or 1000 times. Writing hundreds of identical statements would make the program unnecessarily long and difficult to maintain.
A loop provides a much better solution. We can tell the program how many times the statement needs to execute.
#include <stdio.h>
int main() {
    for (int i = 1; i <= 3; i++) {
        printf("Hello C\n");
    }
    return 0;
}
Output
Hello C
Hello C
Hello C
The same logic can easily be changed to repeat the message 100 or 1000 times by changing the loop condition.
Why Do We Use Loops?
Loops are useful whenever a task needs to be performed repeatedly.
Common examples include:
Printing numbers from 1 to 100
Reading multiple values from the user
Processing elements of an array
Generating multiplication tables
Creating patterns
Repeating calculations
Searching through data
Therefore, loops help make programs shorter, cleaner, and easier to manage.
Types of Loops in C
C provides three main looping statements:
for loop
while loop
do-while loop
The three loops perform repetition differently depending on when and how the condition is checked.
1. for Loop
The for loop is commonly used when the number of repetitions is known or can be easily controlled.
It is an entry-controlled loop, meaning the condition is evaluated before the loop body is executed.
Syntax
for (initialization; condition; update) {
    // statements to be repeated
}
A for loop normally contains four important parts:
1. Initialization
This step gives the loop variable its starting value.
int i = 1;
2. Condition
The condition determines whether another iteration should take place.
i <= 5
If the condition is true, the loop body executes. If it is false, the loop stops.
3. Update
After each iteration, the loop variable is modified.
i++
It may be increased or decreased depending on the requirement.
4. Loop Body
The statements inside { } are executed during every iteration.
Example
#include <stdio.h>
int main() {
    for (int i = 1; i <= 5; i++) {
        printf("%d ", i);
    }
    return 0;
}
Output
1 2 3 4 5
How the for loop works
Initialization
      ↓
Check Condition
      ↓
   True?
   /   \
 Yes    No
  ↓      ↓
Body    Stop
  ↓
Update
  ↓
Check Condition
2. while Loop
The while loop is useful when the number of repetitions is not necessarily known beforehand.
Like the for loop, it is an entry-controlled loop. The condition is checked before executing the loop body.
Syntax
while (condition) {
    // statements
}
Unlike the for loop, initialization and updating are normally written separately.
Example
#include <stdio.h>
int main() {
    int i = 1;
    while (i <= 6) {
        printf("%d ", i);
        i++;
    }
    return 0;
}
Output
1 2 3 4 5 6
Important Point
If the condition is false when the while loop is reached, its body will not execute even once.
For example:
int i = 10;
while (i < 5) {
    printf("Hello");
}
Here, i < 5 is false from the beginning, so the loop body is skipped.
3. do-while Loop
The do-while loop is different from the previous two loops because the condition is checked after the loop body has executed.
It is therefore called an exit-controlled loop.
Syntax
do {
    // statements
} while (condition);
Notice the semicolon ; after the while condition.
Example
#include <stdio.h>
int main() {
    int i = 1;
    do {
        printf("%d ", i);
        i++;
    } while (i <= 5);
    return 0;
}
Output
1 2 3 4 5
Main Feature of do-while
The body of a do-while loop executes at least once, even if the condition is initially false.
#include <stdio.h>
int main() {
    int i = 10;
    do {
        printf("The loop executed once.");
    } while (i < 5);
    return 0;
}
Output
The loop executed once.
This happens because the condition is checked only after the first execution.
for vs while vs do-while
Loop Condition Checked Minimum Executions Common Use
for Before execution 0 Known number of repetitions
while Before execution 0 Condition-based repetition
do-while After execution 1 Menu/input-based programs
Infinite Loop in C
An infinite loop is a loop that never terminates because its condition always remains true or there is no stopping condition.
For example:
#include <stdio.h>
int main() {
    while (1) {
        printf("Running continuously...\n");
    }
    return 0;
}
Since 1 is always considered true in C, the loop continues indefinitely.
Output
Running continuously...
Running continuously...
Running continuously...
...
An infinite loop can also be created using a for loop:
#include <stdio.h>
int main() {
    for (;;) {
        printf("Loop is running...\n");
    }
    return 0;
}
The empty expressions in for (;;) result in a loop with no terminating condition.
A do-while loop can also be made infinite:
#include <stdio.h>
int main() {
    do {
        printf("Loop is running...\n");
    } while (1);
    return 0;
}
Important Note
Infinite loops are not always errors. They can be intentionally used in programs such as:
Operating systems
Servers
Embedded systems
Game loops
Programs that continuously wait for input
Nested Loops
A nested loop is simply a loop placed inside another loop.
The outer loop controls the larger repetition, while the inner loop completes all its iterations for each execution of the outer loop.
Example
#include <stdio.h>
int main() {
    for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= 2; j++) {
            printf("i = %d, j = %d\n", i, j);
        }
    }
    return 0;
}
Output
i = 1, j = 1
i = 1, j = 2
i = 2, j = 1
i = 2, j = 2
i = 3, j = 1
i = 3, j = 2
Understanding Nested Loops
The outer loop runs 3 times.
For every one of those iterations, the inner loop runs 2 times.
Therefore:
Outer Loop
   ↓
i = 1 → Inner Loop → 2 executions
i = 2 → Inner Loop → 2 executions
i = 3 → Inner Loop → 2 executions
Nested loops are frequently used for:
Pattern printing
Matrix operations
Two-dimensional arrays
Tables
Comparing multiple sets of values
Loop Control Statements in C
Sometimes we do not want a loop to continue in its normal sequence. C provides special statements that allow us to change the flow of a loop.
The commonly used loop control statements are:
Statement                      Purpose
break                              Immediately terminates the loop
continue                          Skips the current iteration
goto                                Transfers control to a labeled statement
1. break Statement
The break statement immediately terminates the loop.
Example
#include <stdio.h>
int main() {
    for (int i = 1; i <= 5; i++) {
        if (i == 4) {
            break;
        }
        printf("%d ", i);
    }
    return 0;
}
Output
1 2 3
When i becomes 4, break stops the loop immediately.
2. continue Statement
The continue statement does not terminate the loop. Instead, it skips the remaining statements of the current iteration and moves to the next iteration.
Example
#include <stdio.h>
int main() {
    for (int i = 1; i <= 5; i++) {
        if (i == 3) {
            continue;
        }
        printf("%d ", i);
    }
    return 0;
}
Output
1 2 4 5
When i becomes 3, the printf() statement is skipped for that iteration.
3. goto Statement
The goto statement transfers program execution directly to a labeled section of code.
Example
#include <stdio.h>
int main() {
    for (int i = 1; i <= 5; i++) {
        if (i == 4) {
            goto end;
        }
        printf("%d ", i);
    }
end:
    printf("\nLoop terminated.");
    return 0;
}
Output
1 2 3
Loop terminated.
When i reaches 4, the program jumps directly to the end: label.
Note: Although goto is part of C, excessive use of it can make programs difficult to understand. In most situations, structured control statements such as if, break, continue, and loops are preferred.
Quick Summary
Loops allow a C program to repeat instructions efficiently without duplicating code.
Three main loops
for                             → useful when loop repetition is clearly controlled.
while                         → checks the condition before every iteration.
do-while                    → executes the body first and checks the condition afterward.
Other important concepts
Infinite loop               → continues without reaching a terminating condition.
Nested loop               → one loop is placed inside another.
break                        → exits the loop.
continue                    → skips the current iteration.
goto                          → jumps to a specified label.
Understanding loops is essential for solving programming problems because many real-world tasks require the same operation to be performed repeatedly.

< 10 Scenario-Based Questions on Decision-Making Statements in C Last >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top