0% found this document useful (0 votes)
6 views14 pages

Shohan

The document is a presentation on understanding loops in C programming, highlighting their importance for efficient and concise code. It covers the types of loops (for, while, do-while), their differences, and control statements like break and continue. The conclusion emphasizes the necessity of mastering loops for effective programming.

Uploaded by

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

Shohan

The document is a presentation on understanding loops in C programming, highlighting their importance for efficient and concise code. It covers the types of loops (for, while, do-while), their differences, and control statements like break and continue. The conclusion emphasizes the necessity of mastering loops for effective programming.

Uploaded by

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

Group

Presenters
MD. Shohan Rony ID: 232-35-436
MD. Ashri Jamil Sazin ID: 232-35-793
Understanding Loops
in C Programming
Loops are a foundational concept in C
programming. They enable repetitive execution
of code blocks. Loops are essential for efficient
and concise code. This presentation will provide
a comprehensive guide to understanding and
using loops effectively.
What is loop?

A loop is a programming construct that repeats a block of code multiple


times. It helps in reducing redundancy and increasing efficiency.
Why Use Loops?
Efficiency

Consider printing numbers 1 to 10.


- To automate repetitive tasks.
Without loops, it's tedious. The concise
- To reduce the length of the code.
equivalent uses a simple for loop.
- To enhance code readability and maintainability.
- To process large amounts of data efficiently. - To
automate
Without Loop:
printf("1\n"); printf("2\n"); ...
printf("10\n");

With Loop:
for (int i = 1; i <= 10; i++)
{ printf("%d\n", i); }
Types of loop in c

For loop While loop Do-While loop


The for Loop
Initialization
Executed once at the beginning.

Condition
Evaluated before each iteration. The loop continues if true.

Increment/Decrement
Executed after each iteration.

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


printf("Iteration: %d\n", i);
}
The code prints 0 to 4. for loops provide definite iteration
and a clear structure for counting.
The while Loop

Initialization
Done before the while loop.
2

Condition
Evaluated before each iteration.
1

3 Increment/Decrement
Done within the loop body.

int i = 0;
while (i < 5) {
printf("Iteration: %d\n", i);
i++;
}
The do-while Loop
Condition
Evaluated after each iteration.

Guaranteed Execution
Ensures at least one execution.

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

The code prints 0 to 4. The do-while loop is a post-test loop. It


guarantees at least one execution. Use it for input validation where you
prompt the user until valid input is provided.
Differences Between Loops

For Loop: Condition check before execution, used when iterations are known.
While Loop: Condition check before execution, used when iterations are
unknown.
Do-While Loop: Condition check after execution, ensures at least one execution.
Nested Loops

One loop inside Multi-dimensional


another data

A loop inside another loop.


Example:
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= 3; j++) {
printf("(%d,%d) ", i, j);
}
printf("\n");
}
Loop Control: break
Terminates
The loop ends prematurely.

Transfers Control
Goes to the next statement.

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


if (i == 5) break;
printf("%d\n", i);
}

This code prints 0 to 4. A common use is exiting a loop based


on a condition or error.
Loop Control: continue
Skips Iteration
Skips current iteration.

Proceeds
Goes to the next iteration.

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


if (i % 2 == 0) continue;
printf("%d\n", i);
}

The code prints only odd numbers. A common use is skipping specific
cases within a loop.
Conclusion

- Loops help make code efficient and concise.


- Choosing the right loop depends on the problem.
- Understanding loops is essential for mastering programming.
Q&A

Any questions?

You might also like