LOOP STATEMENTS AND TYPES OF LOOP IN C LANGUAGE BY RIZWAN
rizwanmolla711410
72 views
19 slides
Sep 16, 2024
Slide 1 of 19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
About This Presentation
This presentation offers a thorough understanding of loop statements and their various types in the C programming language. Loops are an essential part of programming, allowing the execution of a block of code multiple times based on a condition. The presentation covers the fundamental loop structur...
This presentation offers a thorough understanding of loop statements and their various types in the C programming language. Loops are an essential part of programming, allowing the execution of a block of code multiple times based on a condition. The presentation covers the fundamental loop structures used in C, including for, while, and do-while loops. It explains how each loop functions, its syntax, and practical use cases in real-world programming scenarios. Additionally, it delves into the concept of nested loops, loop control statements like break and continue, and how to optimize loops for efficient code execution. Through clear examples and explanations, this presentation is designed to help both beginners and intermediate programmers grasp the key concepts of loop statements in C, empowering them to write more efficient and effective code. Ideal for programming students, developers, and anyone looking to enhance their understanding of C programming.
Size: 3.41 MB
Language: en
Added: Sep 16, 2024
Slides: 19 pages
Slide Content
LOOPS IN C
By -Rizwan
Understanding Iterative Control Structures
Topic:-
Loop Statements in C
Language
Table of contents
Introduction
Introduction to Loops Definition, Syntax & Example of for loop,
while loop & do-while Loop
Definition and Example of Break &
Continue Statement
Explanation and Example on Nested Loops
01
03
02
04
Types of Loop in C
Loop Control Statements Advanced Loop Concepts
05
Application in Real World
scenario
06
Conclusion
Summary
Application in Real World
Bibliography
07 08
Acknowledgment
Introduction to Loops
❑Loop Fundamentals:
Loops in C are used to repeat a block
of code until a specified condition is
met. Allowing for efficient execution of
repetitive tasks.
❑Control Statement:
A loop in C consists of a body and a
control statement, which evaluates a
condition to determine whether the
loop should continue executing.
❑Parts of Loop:
A Loop consists of Four parts –
1. Initialization of Control Variable.
2. Condition Checking.
3. Loop Body.
4. Increment/ Decrement of control variable.
01
Types of Loop in C
❑Do-While Loop
It’s used to execute the
controlled statement a
given number of times
It tests whether a condition
is true before executing
the controlled statement
It tests whether a condition
is true after executing the
controlled statement
❑For Loop ❑While Loop
There are Three kinds of loops -
Entry Controlled Loop –In an Entry
controlled loop, the test condition is
checked first, and then the loop body is
executed.
Exit Controlled Loop –In an Exit
controlled loop, the loop body is executed
first, and then the specified condition is
checked afterward.
02
Syntax:
for( initial value; condition; increment/ decrement )
{
statements;
}
For Loop
•The for loop is a compact and efficient way to implement
a loop.
•Components of the for loop include initialization,
condition and increment/ decrement.
Flow Chart
Example of for loop
#include<stdio.h>
int main(){
int n;
for( n = 1; n <= 10; n++ ){
printf(“%d\n”, n);
}
return 0;
}
Output:
1
2
3
4
5
6
7
8
9
10
Syntax:
Initial value;
while ( condition ){
statements;
increment or decrement;
}
While Loop
•The while loop is useful when the number of iterations
is not known beforehand.
•It continues to execute the block of code as long as
the specified condition is true.
Flow Chart
Example of while loop
#include<stdio.h>
int main(){
int n = 1;
while( n<=10 ){
printf(“%d\n”, n);
n++;
}
return 0;
}
Output:
1
2
3
4
5
6
7
8
9
10
Syntax:
Initial value;
do{
statements;
increment or decrement;
} while( expression );
Do-While Loop
•The do-while loop ensures that the code inside the
loop is executed at least once, as the condition is
checked after the first iteration.
•It’s suitable for situation where the loop body must
execute at east once.
Flow Chart
Example of do-while loop
#include<stdio.h>
int main(){
int n = 1;
do{
printf(“%d\n”, n);
n++;
} while( n<=10 )
return 0;
}
Output:
1
2
3
4
5
6
7
8
9
10
Loop Control Statements
Skips the rest of the loop for the
current iteration.
Exits a loop prematurely.
Continue StatementBreak Statement
03
Example :
#include <stdio.h>
int main(){
for( int n = 1; n <= 5; n++
){
if( n==3 ){
break;
}
printf(“%d\t”, n);
}
return 0;
}
Output: 1 2
Example :
#include <stdio.h>
int main(){
for( int n = 1; n <= 5; n++
){
if( n==3 ){
continue;
}
printf(“%d\t”, n);
}
return 0;
}
Output: 1 2 4 5
Advanced Loop Concepts:
Nested Loops
04
Definition :A nested loop means a loop statement inside another loop statement. That is why nested
loops are also called “loop inside loops“. We can define any number of loops inside another loop.
Syntax:
for (outer loop) {
// Outer loop code
for (inner loop) {
// Inner loop code
}
}
Flow Chart
Example of Nested loops
#include<stdio.h>
int main(){
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows); //entered 7
for (i= 1; i<= rows; ++i){
for (j = 1; j <= (rows-i); ++j){
printf(" ");
}
for (j = 1; j <= i; ++j){
printf(" *");
}
printf("\n");
}
return 0;
}
Output:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
Application in Real World
Scenario
05
Loops in C aren't just theoretical concepts; they power real-world
applications across various domains:
❑Data Processing : From analyzing stats to training AI, loops crunch
through data to extract insights and build intelligent systems.
❑Game Development: The core of most games involves a continuous loop
that handles user input, updates game elements, and renders graphics.
❑Embedded Systems: In smart devices and robots, loops continuously
read sensor data and control actuators, making the physical world
interact with the digital.
❑Network Programming: Loops manage data flow in the digital highways,
keeping information packets moving between devices and servers.
Conclusion06
In conclusion, loops are an essential tool in C programming that empower us to create efficient,
concise, and dynamic code. By mastering the different types of loops—for, while, and do-while and
their control statements, we can tackle a vast array of programming tasks.
Key takeaways:
▪Loops enable us to repeat code blocks as needed, saving time and effort.
▪Each loop type has its distinct strengths and use cases :
•For loops excel in scenarios where the number of iterations is known beforehand.
•While loops shine when the continuation of the loop depends on a specific condition.
•Do-while loops ensure at least one execution of the code block.
▪Nested loops create intricate patterns and explore multidimensional data structures.
▪Loop control statements, break and continue, offer fine-tuned control over loop behavior.
▪Best practices, such as clear formatting, meaningful variable names, and careful condition
checking, promote code readability and prevent errors.
Bibliography07
Books:
•Kernighan, B. W., & Ritchie, D. M. (1988). The C Programming Language (2nd ed.).
Prentice Hall.
•Kanetkar, Y. (2023). Let Us C (16th ed.). BPB Publications.
Website:
•GeeksforGeeks. (2022, 11 Oct). C Loops. https://www.geeksforgeeks.org/c-loops/
Accessed: December 26, 2023.
Acknowledgement08
I would like to express my gratitude to the educators, authors, and researchers whose
valuable contributions have significantly elevated the knowledge in this field. Their
dedication and insights have been pivotal in clarifying intricate topics, enriching our
knowledge, and inspiring a deeper appreciation for the subject.