KEMBAR78
Loops | PDF | Control Flow | Software Development
0% found this document useful (0 votes)
10 views7 pages

Loops

The document discusses loop statements in C, focusing on the while loop, which executes a block of code repeatedly based on a condition. It includes syntax, examples, common errors, advantages, and disadvantages of using while loops. The document emphasizes the importance of updating loop variables to avoid infinite loops and provides practical examples for better understanding.

Uploaded by

kethanacse29922
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Loops

The document discusses loop statements in C, focusing on the while loop, which executes a block of code repeatedly based on a condition. It includes syntax, examples, common errors, advantages, and disadvantages of using while loops. The document emphasizes the importance of updating loop variables to avoid infinite loops and provides practical examples for better understanding.

Uploaded by

kethanacse29922
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

LOOP STATEMENTS IN C

Loops are control structures used in C programming to execute a block of code multiple times,
either based on a condition or a fixed number of iterations. Loops reduce code redundancy and
are essential for efficient programming.

1. Classification of Loop Statements

While Loop in C

The while loop is a control flow statement in C that allows code to be executed repeatedly
based on a given condition. It is an entry-controlled loop, meaning the condition is checked
before the loop body executes. If the condition evaluates to true (non-zero), the loop executes
the block of code. If it evaluates to false (zero), the loop terminates.

Syntax: while (condition)

// Code to execute repeatedly

 Condition: A boolean expression that determines whether the loop continues.


 The loop executes repeatedly until the condition becomes false.

How it Works

1. Step 1: The condition is evaluated.

2. Step 2: If the condition is true, the loop body executes.

3. Step 3: After executing the loop body, the condition is evaluated again.

4. Step 4: The process repeats until the condition is false.

Flowchart

1. Start → Check Condition → If True → Execute Loop Body → Repeat.


2. If False → Exit Loop → End.
Examples

Example 1: Simple Counter

Print numbers from 1 to 5 using a while loop.

#include <stdio.h>

int main()

int i = 1; // Initialization

while (i <= 5) // Condition

printf("%d\n", i); // Print current value of i

i++; // Increment

return 0;

Output: 1

Example 2: Sum of Natural Numbers

Calculate the sum of numbers from 1 to n.

#include <stdio.h>

int main()

int n, sum = 0, i = 1;
printf("Enter a positive integer: ");

scanf("%d", &n);

while (i <= n)

sum += i; // Add i to sum

i++; // Increment i

printf("Sum = %d\n", sum);

return 0;

Input/Output:

Enter a positive integer: 5

Sum = 15

Example 3: Reverse Digits of a Number

Reverse the digits of a given number using a while loop.

#include <stdio.h>

int main()

int num, reverse = 0, remainder;

printf("Enter an integer: ");

scanf("%d", &num);

while (num != 0)

remainder = num % 10; // Get last digit


reverse = reverse * 10 + remainder; // Build reversed number

num /= 10; // Remove last digit

printf("Reversed Number = %d\n", reverse);

return 0;

Input/output: Enter an integer: 1234

Reversed Number = 4321

Example 4: Infinite Loop

A while loop runs infinitely when the condition always evaluates to true.

#include <stdio.h>

int main()

while (1)

{ // Always true

printf("This is an infinite loop.\n");

return 0;

Note: Use break to exit an infinite loop.

Example 5: Using break in a while Loop

Exit the loop when a specific condition is met.

#include <stdio.h>
int main() {

int i = 1;

while (i <= 10) {

if (i == 5) {

b reak; // Exit the loop when i equals 5

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

i++;

return 0;

Output: 1

Example 6: Using continue in a while Loop

Skip the current iteration when a condition is met.

#include <stdio.h>

int main() {

int i = 1;

while (i <= 5) {

if (i == 3) {

i++; // Increment to avoid infinite loop

continue; // Skip the rest of the iteration


}

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

i++;

return 0;

Output: 1

Common Errors in while Loops

1. Infinite Loops:

o Forgetting to update the loop variable leads to infinite execution.

int i = 1;

while (i <= 5)

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

// Missing i++

2. Logical Errors:

 Incorrect condition can result in skipped or extra iterations.

int i = 1;

while (i = 5)

{ // Mistaken assignment instead of comparison

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

Advantages of while Loop

 Ideal for indeterminate loops, where the number of iterations is not known in advance
(e.g., reading user input until a specific value is entered).

 Simple and flexible for dynamic conditions.

Disadvantages of while Loop

 Higher chance of creating infinite loops if the loop variable is not updated properly.

 May be less concise than a for loop for cases with a known iteration count.

You might also like