SlideShare uma empresa Scribd logo
1 de 30
Electrical Engineering Department
Loop in C
Properties &
Applications
By Emroz Sardar
Electrical Engineering Department
03
02
01
04
Details,
History & Advantages
Introduction
Types of Loops, Flowcharts &
Practical Use
TABLE OF CONTENTS
Outputs & Conclusion
Electrical Engineering Department
What is a Loop ?
Looping Statements in C execute the sequence
of statements many times until the stated
condition becomes false. A loop in C consists of
two parts, a body of a loop and a control
statement. The control statement is a
combination of some conditions that direct the
body of the loop to execute until the specified
condition becomes false. The purpose of the C
loop is to repeat the same code a number of
times.
Electrical Engineering Department
LOOP is nothing but a language that precisely captures primitive recursive
functions. The only operations supported in the language are assignment,
addition, and looping a number of times that is fixed before loop execution
starts.
The LOOP language was introduced in 1967 by Albert R. Meyer and Dennis M.
Ritchie. Meyer and Ritchie showed the correspondence between the LOOP
language and primitive recursive functions. Dennis M. Ritchie mainly
formulated the LOOP language.
It is known that the FOR loop has been part of the C language since the early
1970s (or late 1960s) and was developed by John Backus, but the DO loop
has been a part of Fortran since it was developed in the mid-1950s by John
Backus and his team at IBM.
The name for-loop comes from the word for, which is used as the keyword in
many programming languages to introduce a for-loop. The term in English
dates to ALGOL 58 and was popularized in the influential later ALGOL 60.
The loop body is executed "for" the given values of the loop variable, though
this is more explicit in the ALGOL version of the statement, in which a list of
possible values and/or increments can be specified.
(There’s no relevant name or origin related history for while & do while loop)
History of LOOPs
Electrical Engineering Department
Advantages of LOOPs
● It provides code reusability.
● Using loops, we do not need to write the same code
again and again.
● Using loops, we can traverse over the elements of
data structures (array or linked lists).
Electrical Engineering Department
In C language, there are 3 types of LOOP ::
● FOR LOOP
● WHILE LOOP
● DO-WHILE LOOP
TYPES OF LOOPS
Electrical Engineering Department
LOOPS
Entry Controlled
Exit Controlled
For LOOP
While LOOP
Do While
Electrical Engineering Department
In programming, LOOPs are considered as controlled
statements that can regulate the flow of the program
execution. They use a conditional expression in order to
decide to what extent a particular block should be
repeated. Every loop consists of two sections, loop body
and control statement.
Based on the position of these two sections, loop
execution can be handled in two ways that are at the
entry-level and exit-level.
So, loops can be categorized into two types:
● Entry controlled loop: When a condition is evaluated
at the beginning of the loop.
● Exit controlled loop: When a condition is evaluated at
the end of the loop.
Electrical Engineering Department
Entry Controlled Loop: An entry control loop checks
condition at entry level (at beginning), that’s why it is
termed as entry control loop. It is a type of loop in which
the condition is checked first and then after the loop
body executed. For loop and while loop fall in this
category. If the condition is true, the loop body would be
executed otherwise, the loop would be terminated.
Exit Control Loop: An exit control loop checks condition
at exit level (in the end), that’s why it is termed as exit
control loop. Oppose to Entry controlled loop, it is a loop
in which condition is checked after the execution of the
loop body. Do while loop is the example. The loop body
would be executed at least once, no matter if the test
condition is true or false.
Electrical Engineering Department
FOR LOOP
● A For loop is the most efficient loop structure in C
programming. It allows us to write a loop that needs
to execute a specific number of times. The for loop in
C language is used to iterate the statements or a part
of the program several times. It is frequently used to
traverse the data structures like the array and linked
list.
● Syntax of for loop in C
The syntax of for loop in c language is given below:
for( initialization; condition; increment/decrement)
{
//code to be executed
}
Electrical Engineering Department
How for loop works?
● The initialization statement is executed only once.
● Then, the test expression is evaluated. If the test
expression is evaluated to false, the for loop is
terminated.
● However, if the test expression is evaluated to true,
statements inside the body of the for loop are
executed, and the update expression is updated.
● Again the test expression is evaluated.
*This process goes on until the test expression is false.
When the test expression is false, the loop terminates.
Electrical Engineering Department
For LOOP
FlowChart
Electrical Engineering Department
A While Loop is used to repeat a specific block of code an
unknown number of times, until a condition is met. For
example, if we want to ask a user for a number between 1
and 10, we don't know how many times the user may enter
a larger number, so we keep asking "while the number is
not between 1 and 10". If we (or the computer) knows
exactly how many times to execute a section of code
(such as shuffling a deck of cards) we use a for loop.
The syntax of while loop in c language is given below:
while (condition){
//code to be executed
}
Flowchart and Example of while loop
While LOOP
Electrical Engineering Department
Why While Loops?
● Like all loops, "while loops" execute
blocks of code over and over again.
● The advantage to a while loop is that
it will go (repeat) as often as
necessary to accomplish its goal.
Electrical Engineering Department
How while Loop works?
In while loop, condition is evaluated first
and if it returns true then the statements
inside while loop execute, this happens
repeatedly until the condition returns
false. When condition returns false, the
control comes out of loop and jumps to the
next statement in the program after while
loop.
Electrical Engineering Department
while LOOP
FlowChart
Electrical Engineering Department
The do...while statement creates a loop that executes a specified
statement until the test condition evaluates to false. The condition is
evaluated after executing the statement, resulting in the specified
statement executing at least once.
The syntax for do…while loop is stated below:
do
{
//code to be executed
}while(condition);
● Unlike for and while loops, that test the loop condition at the top of
the loop, the do...while loop checks its condition at the bottom of
the loop.
● A do...while loop is similar to a while loop, except that a do...while
loop is guaranteed to execute at least one time, where only while
loop executes the target statement, repeatedly.
Do while LOOP
Electrical Engineering Department
How Do...While loop works
The conditional expression appears at the end of the
loop, so the statement(s) in the loop executes once
before the condition is tested.
If the condition is true, the flow of control jumps back
up to do, and the statement(s) in the loop executes
again. This process repeats until the given condition
becomes false.
Electrical Engineering Department
Do while LOOP
FlowChart
Electrical Engineering Department
DIFFERENCE BETWEEN WHILE LOOP AND DO WHILE LOOP
● Condition is checked first then
statement(s) is executed.
● It might occur statement(s) is
executed zero times, If condition
is false..
● No semicolon at the end of
while.
while(condition)
● If there is a single statement,
brackets are not required.
● Variable in condition is initialized
before the execution of loop.
● while loop is entry controlled
loop.
● while (condition)
{ statement(s); }
● Statement(s) is executed at
least once, thereafter condition
is checked.
● At least once the statement(s) is
executed.
● Semicolon at the end of while.
while(condition);
● Brackets are always required.
● variable may be initialized
before or within the loop.
● do-while loop is exit controlled
loop.
● do { statement(s); }
while(condition);
While loop Do-while loop
Electrical Engineering Department
NESTed LOOP
A nested loop has one loop inside of another. These are typically
used for working with two dimensions such as printing stars in
rows and columns the syntax are shown below. When a loop is
nested inside another loop, the inner loop runs many times inside
the outer loop. In each iteration of the outer loop, the inner loop
will be re-started. The inner loop must finish all of its iterations
before the outer loop can continue to its next iteration.
Syntax of Nested loop
OuterLoop
{
InnerLoop
{
// inner loop statements.
}
// outer loop statements.
}
Electrical Engineering Department
NESTed LOOP
FlowChart
Electrical Engineering Department
Infinitive
loop
To make a for loop infinite,
we need not give any
expression in the syntax.
Instead of that, we can put
two semicolons to validate
the syntax of the for loop. It
will work as an infinite for
loop.
For example :
#include <stdio.h>
void main()
{ int i = 10;
for( ; ;)
{
printf("%dn",i);
}
}
In while loop, when the
condition passes and if it is
true , it runs infinite number
of times.
For example :
#include <stdio.h>
void main()
{ int i = 10;
while(1)
{
printf("%dt",i);
i++;
}
}
The do-while loop will run
infinite times if we pass any
non-zero value as the
conditional expression.
For example :
#include <stdio.h>
void main()
{ int i = 10;
do
{
printf("%dt",i);
i++;
} while(i);
}
Electrical Engineering Department
BREAK AND CONTINUE STATEMENTS
● When a break statement is
encountered inside a loop, the loop
is immediately terminated and the
program control resumes at the next
statement following the loop.
For the for loop, continue statement
causes the conditional test and increment
portions of the loop to execute. For the
while and do...while loops, continue
statement causes the program control to
pass to the conditional tests.
true
Electrical Engineering Department
Practical Use
Real World Examples of Loop
● Software of the ATM machine is in a loop to process
transaction after transaction until you acknowledge that
you have no more to do.
● Software program in a mobile device allows user to unlock
the mobile with 5 password attempts. After that it resets
mobile device.
● You put your favorite song on a repeat mode. It is also a
loop.
● You want to run a particular analysis on each column of
your data set.
Electrical Engineering Department
FlowChart for
Digital Clock
For LOOP
Our Example
Electrical Engineering Department
*Fast Forwarded
Electrical Engineering Department
CONCLUSION
Looping in C or in any programming language is one of
the key concepts. There are generally two types that are
entry controlled and exit controlled loop. The loops or
statement blocks execute a number of times until the
condition becomes false.
❖ Advantages
● Using loops, we do not need to write the same code
again and again.
● Using loops, we can traverse over the elements of
data structures (array or linked lists).
● It provides code reusability.
Electrical Engineering Department
REFERENCE
❖ https://www.guru99.com/c-loop-statement.html
❖ https://www.programiz.com/c-programming/c-for-loop
❖ https://www.cs.utah.edu/~germain/PPS/Topics/while_loops.html
❖ https://developer.mozilla.org/en-
US/docs/Web/JavaScript/Reference/Statements/do...while
❖ https://www.geeksforgeeks.org/difference-between-while-and-do-while-loop-in-
c-c-java/
❖ https://www.tutorialspoint.com/cprogramming/c_break_statement.htm
Electrical Engineering Department
Do you have any questions?
supermanemroz@gmail.com
+91 9804226160
AeroSoft.in
THANKS!

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Loops in c++ programming language
Loops in c++ programming language Loops in c++ programming language
Loops in c++ programming language
 
Control structures repetition
Control structures   repetitionControl structures   repetition
Control structures repetition
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Loops c++
Loops c++Loops c++
Loops c++
 
Combinational Circuits & Sequential Circuits
Combinational Circuits & Sequential CircuitsCombinational Circuits & Sequential Circuits
Combinational Circuits & Sequential Circuits
 
Type Conversion, Precedence and Associativity
Type Conversion, Precedence and AssociativityType Conversion, Precedence and Associativity
Type Conversion, Precedence and Associativity
 
Operators
OperatorsOperators
Operators
 
Union in c language
Union  in c languageUnion  in c language
Union in c language
 
C++ oop
C++ oopC++ oop
C++ oop
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
 
The Loops
The LoopsThe Loops
The Loops
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Fundamental of C Programming (Data Types)
Fundamental of C Programming (Data Types)Fundamental of C Programming (Data Types)
Fundamental of C Programming (Data Types)
 
Basic Data Types in C++
Basic Data Types in C++ Basic Data Types in C++
Basic Data Types in C++
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 
Loop(for, while, do while) condition Presentation
Loop(for, while, do while) condition PresentationLoop(for, while, do while) condition Presentation
Loop(for, while, do while) condition Presentation
 
Pre defined Functions in C
Pre defined Functions in CPre defined Functions in C
Pre defined Functions in C
 
Structure of a C program
Structure of a C programStructure of a C program
Structure of a C program
 
While loop
While loopWhile loop
While loop
 

Semelhante a Loop in C Properties & Applications

Semelhante a Loop in C Properties & Applications (20)

Cse lecture-7-c loop
Cse lecture-7-c loopCse lecture-7-c loop
Cse lecture-7-c loop
 
Loops
LoopsLoops
Loops
 
Loops
LoopsLoops
Loops
 
C language 2
C language 2C language 2
C language 2
 
Loop structures
Loop structuresLoop structures
Loop structures
 
Cpp loop types
Cpp loop typesCpp loop types
Cpp loop types
 
R loops
R   loopsR   loops
R loops
 
Computer programming 2 Lesson 8
Computer programming 2  Lesson 8Computer programming 2  Lesson 8
Computer programming 2 Lesson 8
 
C language (Part 2)
C language (Part 2)C language (Part 2)
C language (Part 2)
 
DECISION MAKING.pptx
DECISION MAKING.pptxDECISION MAKING.pptx
DECISION MAKING.pptx
 
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KRLoops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
 
LOOPING IN C- PROGRAMMING.pptx
LOOPING IN C- PROGRAMMING.pptxLOOPING IN C- PROGRAMMING.pptx
LOOPING IN C- PROGRAMMING.pptx
 
Loops Basics
Loops BasicsLoops Basics
Loops Basics
 
python.pptx
python.pptxpython.pptx
python.pptx
 
Loops in c
Loops in cLoops in c
Loops in c
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
2nd year computer science chapter 12 notes
2nd year computer science chapter 12 notes2nd year computer science chapter 12 notes
2nd year computer science chapter 12 notes
 
prt123.pptx
prt123.pptxprt123.pptx
prt123.pptx
 
Learn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & LoopsLearn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & Loops
 
Do While Repetition Structure
Do While Repetition StructureDo While Repetition Structure
Do While Repetition Structure
 

Último

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Último (20)

HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 

Loop in C Properties & Applications

  • 1. Electrical Engineering Department Loop in C Properties & Applications By Emroz Sardar
  • 2. Electrical Engineering Department 03 02 01 04 Details, History & Advantages Introduction Types of Loops, Flowcharts & Practical Use TABLE OF CONTENTS Outputs & Conclusion
  • 3. Electrical Engineering Department What is a Loop ? Looping Statements in C execute the sequence of statements many times until the stated condition becomes false. A loop in C consists of two parts, a body of a loop and a control statement. The control statement is a combination of some conditions that direct the body of the loop to execute until the specified condition becomes false. The purpose of the C loop is to repeat the same code a number of times.
  • 4. Electrical Engineering Department LOOP is nothing but a language that precisely captures primitive recursive functions. The only operations supported in the language are assignment, addition, and looping a number of times that is fixed before loop execution starts. The LOOP language was introduced in 1967 by Albert R. Meyer and Dennis M. Ritchie. Meyer and Ritchie showed the correspondence between the LOOP language and primitive recursive functions. Dennis M. Ritchie mainly formulated the LOOP language. It is known that the FOR loop has been part of the C language since the early 1970s (or late 1960s) and was developed by John Backus, but the DO loop has been a part of Fortran since it was developed in the mid-1950s by John Backus and his team at IBM. The name for-loop comes from the word for, which is used as the keyword in many programming languages to introduce a for-loop. The term in English dates to ALGOL 58 and was popularized in the influential later ALGOL 60. The loop body is executed "for" the given values of the loop variable, though this is more explicit in the ALGOL version of the statement, in which a list of possible values and/or increments can be specified. (There’s no relevant name or origin related history for while & do while loop) History of LOOPs
  • 5. Electrical Engineering Department Advantages of LOOPs ● It provides code reusability. ● Using loops, we do not need to write the same code again and again. ● Using loops, we can traverse over the elements of data structures (array or linked lists).
  • 6. Electrical Engineering Department In C language, there are 3 types of LOOP :: ● FOR LOOP ● WHILE LOOP ● DO-WHILE LOOP TYPES OF LOOPS
  • 7. Electrical Engineering Department LOOPS Entry Controlled Exit Controlled For LOOP While LOOP Do While
  • 8. Electrical Engineering Department In programming, LOOPs are considered as controlled statements that can regulate the flow of the program execution. They use a conditional expression in order to decide to what extent a particular block should be repeated. Every loop consists of two sections, loop body and control statement. Based on the position of these two sections, loop execution can be handled in two ways that are at the entry-level and exit-level. So, loops can be categorized into two types: ● Entry controlled loop: When a condition is evaluated at the beginning of the loop. ● Exit controlled loop: When a condition is evaluated at the end of the loop.
  • 9. Electrical Engineering Department Entry Controlled Loop: An entry control loop checks condition at entry level (at beginning), that’s why it is termed as entry control loop. It is a type of loop in which the condition is checked first and then after the loop body executed. For loop and while loop fall in this category. If the condition is true, the loop body would be executed otherwise, the loop would be terminated. Exit Control Loop: An exit control loop checks condition at exit level (in the end), that’s why it is termed as exit control loop. Oppose to Entry controlled loop, it is a loop in which condition is checked after the execution of the loop body. Do while loop is the example. The loop body would be executed at least once, no matter if the test condition is true or false.
  • 10. Electrical Engineering Department FOR LOOP ● A For loop is the most efficient loop structure in C programming. It allows us to write a loop that needs to execute a specific number of times. The for loop in C language is used to iterate the statements or a part of the program several times. It is frequently used to traverse the data structures like the array and linked list. ● Syntax of for loop in C The syntax of for loop in c language is given below: for( initialization; condition; increment/decrement) { //code to be executed }
  • 11. Electrical Engineering Department How for loop works? ● The initialization statement is executed only once. ● Then, the test expression is evaluated. If the test expression is evaluated to false, the for loop is terminated. ● However, if the test expression is evaluated to true, statements inside the body of the for loop are executed, and the update expression is updated. ● Again the test expression is evaluated. *This process goes on until the test expression is false. When the test expression is false, the loop terminates.
  • 13. Electrical Engineering Department A While Loop is used to repeat a specific block of code an unknown number of times, until a condition is met. For example, if we want to ask a user for a number between 1 and 10, we don't know how many times the user may enter a larger number, so we keep asking "while the number is not between 1 and 10". If we (or the computer) knows exactly how many times to execute a section of code (such as shuffling a deck of cards) we use a for loop. The syntax of while loop in c language is given below: while (condition){ //code to be executed } Flowchart and Example of while loop While LOOP
  • 14. Electrical Engineering Department Why While Loops? ● Like all loops, "while loops" execute blocks of code over and over again. ● The advantage to a while loop is that it will go (repeat) as often as necessary to accomplish its goal.
  • 15. Electrical Engineering Department How while Loop works? In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop.
  • 17. Electrical Engineering Department The do...while statement creates a loop that executes a specified statement until the test condition evaluates to false. The condition is evaluated after executing the statement, resulting in the specified statement executing at least once. The syntax for do…while loop is stated below: do { //code to be executed }while(condition); ● Unlike for and while loops, that test the loop condition at the top of the loop, the do...while loop checks its condition at the bottom of the loop. ● A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time, where only while loop executes the target statement, repeatedly. Do while LOOP
  • 18. Electrical Engineering Department How Do...While loop works The conditional expression appears at the end of the loop, so the statement(s) in the loop executes once before the condition is tested. If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop executes again. This process repeats until the given condition becomes false.
  • 19. Electrical Engineering Department Do while LOOP FlowChart
  • 20. Electrical Engineering Department DIFFERENCE BETWEEN WHILE LOOP AND DO WHILE LOOP ● Condition is checked first then statement(s) is executed. ● It might occur statement(s) is executed zero times, If condition is false.. ● No semicolon at the end of while. while(condition) ● If there is a single statement, brackets are not required. ● Variable in condition is initialized before the execution of loop. ● while loop is entry controlled loop. ● while (condition) { statement(s); } ● Statement(s) is executed at least once, thereafter condition is checked. ● At least once the statement(s) is executed. ● Semicolon at the end of while. while(condition); ● Brackets are always required. ● variable may be initialized before or within the loop. ● do-while loop is exit controlled loop. ● do { statement(s); } while(condition); While loop Do-while loop
  • 21. Electrical Engineering Department NESTed LOOP A nested loop has one loop inside of another. These are typically used for working with two dimensions such as printing stars in rows and columns the syntax are shown below. When a loop is nested inside another loop, the inner loop runs many times inside the outer loop. In each iteration of the outer loop, the inner loop will be re-started. The inner loop must finish all of its iterations before the outer loop can continue to its next iteration. Syntax of Nested loop OuterLoop { InnerLoop { // inner loop statements. } // outer loop statements. }
  • 23. Electrical Engineering Department Infinitive loop To make a for loop infinite, we need not give any expression in the syntax. Instead of that, we can put two semicolons to validate the syntax of the for loop. It will work as an infinite for loop. For example : #include <stdio.h> void main() { int i = 10; for( ; ;) { printf("%dn",i); } } In while loop, when the condition passes and if it is true , it runs infinite number of times. For example : #include <stdio.h> void main() { int i = 10; while(1) { printf("%dt",i); i++; } } The do-while loop will run infinite times if we pass any non-zero value as the conditional expression. For example : #include <stdio.h> void main() { int i = 10; do { printf("%dt",i); i++; } while(i); }
  • 24. Electrical Engineering Department BREAK AND CONTINUE STATEMENTS ● When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop. For the for loop, continue statement causes the conditional test and increment portions of the loop to execute. For the while and do...while loops, continue statement causes the program control to pass to the conditional tests. true
  • 25. Electrical Engineering Department Practical Use Real World Examples of Loop ● Software of the ATM machine is in a loop to process transaction after transaction until you acknowledge that you have no more to do. ● Software program in a mobile device allows user to unlock the mobile with 5 password attempts. After that it resets mobile device. ● You put your favorite song on a repeat mode. It is also a loop. ● You want to run a particular analysis on each column of your data set.
  • 26. Electrical Engineering Department FlowChart for Digital Clock For LOOP Our Example
  • 28. Electrical Engineering Department CONCLUSION Looping in C or in any programming language is one of the key concepts. There are generally two types that are entry controlled and exit controlled loop. The loops or statement blocks execute a number of times until the condition becomes false. ❖ Advantages ● Using loops, we do not need to write the same code again and again. ● Using loops, we can traverse over the elements of data structures (array or linked lists). ● It provides code reusability.
  • 29. Electrical Engineering Department REFERENCE ❖ https://www.guru99.com/c-loop-statement.html ❖ https://www.programiz.com/c-programming/c-for-loop ❖ https://www.cs.utah.edu/~germain/PPS/Topics/while_loops.html ❖ https://developer.mozilla.org/en- US/docs/Web/JavaScript/Reference/Statements/do...while ❖ https://www.geeksforgeeks.org/difference-between-while-and-do-while-loop-in- c-c-java/ ❖ https://www.tutorialspoint.com/cprogramming/c_break_statement.htm
  • 30. Electrical Engineering Department Do you have any questions? supermanemroz@gmail.com +91 9804226160 AeroSoft.in THANKS!