Loops in C

Loops in C

Definition:

Loops in C are control structures that allow repeated execution of a block of code. They are used when you need to perform a task multiple times, saving time and reducing redundancy in your code.

Read Also – Operators in C

Types of Loops:

  1. For Loop
  2. While Loop
  3. Do-While Loop

********************************************************************************

1. For Loop:

Description:

– Used when the number of iterations is known before entering the loop.

Syntax:

for (initialization; condition; increment/decrement)

{

// Code to be repeated

}

Example 1: For Loop

#include <stdio.h>

int main()

{

// For Loop Example

for (int i = 0; i < 5; ++i) {

// Code to be repeated

printf("Iteration %d\n", i);

}

return 0;

}

Execution Flow:

  1. The for loop is initiated with the initialization statement int i = 0;. It initializes the loop control variable i to 0.
  2. The condition i < 5 is checked before each iteration. If true, the loop continues; otherwise, it exits.
  3. Within the loop, printf prints the current iteration value of i.
  4. The increment statement ++i increases the value of i by 1 after each iteration.
  5. Steps 2-4 are repeated until the condition becomes false.

********************************************************************************

2. While Loop:

Loops in C

– Description:

– Continues as long as the specified condition is true.

– Syntax:

while (condition) {

// Code to be repeated

}

Example 2: While Loop

#include <stdio.h>

int main()

{

// While Loop Example

int i = 0;

while (i < 5) {

// Code to be repeated

printf("Iteration %d\n", i);

++i;

}

return 0;

}

Execution Flow:

  1. The loop control variable i is initialized to 0 outside the loop.
  2. The condition i < 5 is checked before each iteration. If true, the loop continues; otherwise, it exits.
  3. Within the loop, printf prints the current iteration value of i.
  4. The increment statement ++i increases the value of i by 1 after each iteration.
  5. Steps 2-4 are repeated until the condition becomes false.

********************************************************************************

3. Do-While Loop:

– Description:

– Similar to the while loop but checks the condition after the block of code is executed, ensuring the code executes at least once.

– Syntax:

do

{

// Code to be repeated

} while (condition);

Example 3: Do-While Loop

#include <stdio.h>

int main()

{

// Do-While Loop Example

int i = 0;

do {

// Code to be repeated

printf("Iteration %d\n", i);

++i;

} while (i < 5);

return 0;

}

Execution Flow:

  1. The loop control variable i is initialized to 0 outside the loop.
  2. The code within the loop is executed at least once because the condition is checked after the block of code.
  3. Within the loop, printf prints the current iteration value of i.
  4. The increment statement ++i increases the value of i by 1 after each iteration.
  5. Steps 2-4 are repeated until the condition becomes false.

********************************************************************************

Loop Control Statements:

1. Break Statement:

– Description:

– Exits the loop prematurely.

Example:

#include <stdio.h>

int main() {

// Break Statement Example

for (int i = 0; i < 10; ++i) {

if (i == 5) {

break;

}

printf("Iteration %d\n", i);

}

return 0;

}

2. Continue Statement:

– Description:

– Skips the rest of the code in the loop and proceeds to the next iteration.

Example 4: Break Statement

#include <stdio.h>

int main() {

// Break Statement Example

for (int i = 0; i < 10; ++i) {

if (i == 5) {

break; // Exit the loop when i reaches 5

}

// Code to be repeated

printf("Iteration %d\n", i);

}

return 0;

}

Execution Flow:

  1. The for loop is initiated with the initialization statement int i = 0;.
  2. The condition i < 10 is checked before each iteration. If true, the loop continues; otherwise, it exits.
  3. Within the loop, the if statement checks if i is equal to 5. If true, the break statement is executed, exiting the loop prematurely.
  4. If the break statement is not executed, the printf statement prints the current iteration value of i.
  5. Steps 2-4 are repeated until the condition becomes false or the loop is exited with break.

Example 5: Continue Statement

#include <stdio.h>

int main() {

// Continue Statement Example

for (int i = 0; i < 10; ++i) {

if (i == 5) {

continue; // Skip the rest of the loop for i = 5

}

// Code to be repeated

printf("Iteration %d\n", i);

}

return 0;

}

Execution Flow:

  1. The for loop is initiated with the initialization statement int i = 0;.
  2. The condition i < 10 is checked before each iteration. If true, the loop continues; otherwise, it exits.
  3. Within the loop, the if statement checks if i is equal to 5. If true, the continue statement is executed, skipping the rest of the loop for the current iteration.
  4. If the continue statement is not executed, the printf statement prints the current iteration value of i.
  5. Steps 2-4 are repeated until the condition becomes false or the loop is exited.
Mahesh Wabale
Latest posts by Mahesh Wabale (see all)

Leave a Comment