What is a looping structure? Explain for loop with examples.

A looping structure is used to execute a block of code repeatedly based on a certain condition. It allows you to repeat operations without needing to write the same code multiple times. Looping structures help in automating repetitive tasks efficiently.

A for loop is generally used when the number of iterations is known beforehand. It is structured with three components: initialization, condition, and update expression. The loop runs as long as the condition is true.

Syntax:

for(initialization; condition; update) {
// code to execute
}
  • Initialization: Sets the starting point (often a variable).
  • Condition: Determines whether the loop should continue.
  • Update: Alters the loop control variable after each iteration.

Example:

#include <stdio.h>
int main() {
for(int i = 0; i < 5; i++) {
printf(“This is iteration %d\n”, i);
}
return 0;
}

Output:

This is iteration 0
This is iteration 1
This is iteration 2
This is iteration 3
This is iteration 4