[go: up one dir, main page]

0% found this document useful (0 votes)
9 views6 pages

Loops in c

The document provides an overview of loops in C programming, defining them as control structures that execute code repeatedly based on a condition. It details three types of loops: for loops, while loops, and do-while loops, along with their advantages and applications. Each loop type is illustrated with examples to demonstrate its usage in programming tasks.

Uploaded by

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

Loops in c

The document provides an overview of loops in C programming, defining them as control structures that execute code repeatedly based on a condition. It details three types of loops: for loops, while loops, and do-while loops, along with their advantages and applications. Each loop type is illustrated with examples to demonstrate its usage in programming tasks.

Uploaded by

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

C PROGRAMMING

LOOPS
READ MORE
DEFINITION OF
LOOP
A loop is a control structure that repeatedly
executes a block of code as long as a specified
condition is true. Loops are used to automate
repetitive tasks, making code more efficient and
reducing redundancy.
TYPES OF LOOP
FOR LOOP
Repeats a block of code a specific number of times,
commonly used when the number of iterations is
known in advance.

WHILE LOOP
Continues executing as long as a given condition
remains true, useful when the number of iterations is
not known.

DO-WHILE LOOP
Executes a block of code at least once and then
repeats as long as the condition remains true.
FOR LOOP
ADVANTAGES
•Clear Structure: Initialization, condition, and update are in one line,
making it easy to read and understand.
•Ideal for Fixed Iterations: Best suited when the number of iterations is
known beforehand.
•Efficient with Arrays: Great for iterating through arrays or lists with a
fixed number of elements.

APPLICATION
1. Array Iteration: Access each element in an array.
2. Summing Numbers: Calculate sums or averages by.
3. Multiplication Tables: Generate tables or sequences.
4. Counting Items: Count elements that meet specific criteria.

EXAMPLE
for (int i = 0; i < 5; i++)
{
printf("%d\n", i);
}
WHILE LOOP
ADVANTAGES
1. Flexible for unknown iteration counts.
2. Simple with only a condition.
3. Controlled execution based on condition.
4. Great for User Input validation.
5. Useful for Real-Time data processing.

APPLICATION
1. User Input Validation: Keep prompting until valid input is entered.
2. Processing Data Streams: Continuously process data as it arrives.
3. Search Operations: Iterate through data until a match is found.
4. Game Loops: Run game logic until the game ends.
5. Timers and Events: Track events or time-based conditions.

EXAMPLE
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
}
DO-WHILE LOOP
ADVANTAGES
1. Always Runs Once before checking the condition.
2. Great for Input Validation.
3. Simple Structure.
4. Flexible for tasks needing at least one execution.

APPLICATION
1. User Input Validation: Prompt the user until valid input is received.
2. Menu-Driven Programs: Show a menu and repeat until the user selects
an exit option.
3. Game Loops: Keep the game running until the player decides to quit.
4. Repetitive Tasks: Perform tasks at least once, like retrying an operation.

EXAMPLE
int i = 0;
do {
printf("%d\n", i);
i++;
} while (i < 5);

You might also like