SlideShare a Scribd company logo
2
Most read
3
Most read
7
Most read
LOOPS IN C LANGUAGE
LOOPS
 WHY DO WE NEED LOOPS ???
 There may be a situation, when you need to execute a block
of code several number of times.
 In general statements are executed sequentially: The first
statement in a function is executed first, followed by the
second, and so on.
 A loop statement allows us to execute a statement or group
of statements multiple times.
LOOPS :
 TYPES OF LOOPS :
 WHILE LOOP
 FOR LOOP
 DO-WHILE LOOP
 NESTED LOOP
WHILE LOOP
o A while loop statement repeatedly executes a target
statement as long as a given condition is true.
Syntax:
while(condition)
{
statement(s);
}
 Here, statement(s) may be a single statement or a block of
statements. The condition may be any expression, and true
is any non-zero value. The loop iterates while the condition
is true.
 When the condition becomes false, program control
passes to the line immediately following the loop.
FLOW DAIGRAM
EXAMPLE :
#include<stdio.h>
int main ()
{
// Local variable declaration:
int a = 10;
// while loop execution
while( a<20 )
{
printf(“value of a:%d n”, a);
a++;
}
return 0;
}
OUTPUT:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
FOR LOOP
o A for loop is a repetition control structure that allows you to
efficiently write a loop that needs to execute a specific
number of times.
Syntax:
for ( init; condition; increment )
{
statement(s);
}
o The init step is executed first, and only once. This step
allows you to declare and initialize any loop control
variables. You are not required to put a statement here, as
long as a semicolon appears.
FOR LOOP cont….
 Next, the condition is evaluated. If it is true, the body of the
loop is executed. If it is false, the body of the loop does not
execute and flow of control jumps to the next statement just
after the for loop.
 After the body of the for loop executes, the flow of control
jumps back up to the increment statement. This statement
allows you to update any loop control variables. This
statement can be left
blank, as long as a semicolon appears after the condition.
 The condition is now evaluated again. If it is true, the loop
executes and the process repeats
itself (body of loop, then increment step, and then again
condition). After the condition becomes false, the for loop
terminates.
Flow Diagram:
Example:
#include<stdio.h>
int main ()
{ // for loop execution
for( int a = 10; a < 20; a = a + 1 )
{
printf("value of a: %d n”, a);
}
return 0;
}
OUTPUT:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
DO-WHILE LOOP
 Unlike for and while loops, which 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.
syntax
do
{
statement(s);
}
while( condition );
Notice that the conditional expression appears at the end of
the loop, so the statement(s) in the loop execute once before
the condition is tested
Flow Diagram
Example
#include<stdio.h>
int main ()
{ // Local variable declaration:
int a = 10;
// do loop execution
do
{
printf( "value of a: %dn “ ,a);
a = a + 1;
} while( a < 20 );
return 0;
}
OUTPUT:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
NESTED LOOPS
 A loop can be nested inside of another loop.
 Syntax:
for ( init; condition; increment )
{
for ( init; condition; increment )
{
statement(s);
}
statement(s);
// you can put more statements.
}
EXAMPLE
#include<stdio.h>
int main ()
{
int a=1,b; while(a<=3)
{
for(b=1;b<=3;b++)
{
printf("a = %d , b = %dn",a,b);
}
printf("n");
a++;
}
return 0;
}
OUTPUT:
a = 1 , b = 1
a = 1 , b = 2
a = 1 , b = 3
a = 2 , b = 1
a = 2 , b = 2
a = 2 , b = 3
a = 3 , b = 1
a = 3 , b = 2
a = 3 , b = 3

More Related Content

PPTX
C Programming: Control Structure
PPT
cryptography
PPTX
ICT Integrated Instruction: A Teaching Strategy
DOCX
LI-FI documentation
PPT
Today’s youth tomorrow’s future
PPTX
Java script errors &amp; exceptions handling
PPTX
Function in C program
PPTX
Dbms architecture
C Programming: Control Structure
cryptography
ICT Integrated Instruction: A Teaching Strategy
LI-FI documentation
Today’s youth tomorrow’s future
Java script errors &amp; exceptions handling
Function in C program
Dbms architecture

What's hot (20)

PPTX
Switch case in C++
PDF
Loops and conditional statements
PPTX
Loop(for, while, do while) condition Presentation
PPTX
Loops in c language
PPTX
CONDITIONAL STATEMENT IN C LANGUAGE
PDF
10. switch case
PPTX
Conditional statement c++
PPTX
Switch Case in C Programming
PPTX
Loops in c language
PPT
Switch statements in Java
PPTX
The Loops
PPTX
Control statements in c
PPTX
Nesting of if else statement & Else If Ladder
PPTX
Loops in c programming
PPTX
Loops in C
PPTX
C if else
PDF
Files and streams
PPTX
Looping statement
DOC
Jumping statements
Switch case in C++
Loops and conditional statements
Loop(for, while, do while) condition Presentation
Loops in c language
CONDITIONAL STATEMENT IN C LANGUAGE
10. switch case
Conditional statement c++
Switch Case in C Programming
Loops in c language
Switch statements in Java
The Loops
Control statements in c
Nesting of if else statement & Else If Ladder
Loops in c programming
Loops in C
C if else
Files and streams
Looping statement
Jumping statements
Ad

Similar to Loops in c (20)

PPTX
Loops Basics
PDF
loops in C ppt.pdf
PPTX
Cse lecture-7-c loop
DOCX
Programming Fundamentals lecture 8
PPTX
C language 2
PPTX
Managing input and output operations & Decision making and branching and looping
PPTX
Loops in C.net.pptx
PPTX
Loops c++
DOCX
loops and iteration.docx
PDF
Chapter 9 - Loops in C++
PPTX
Loops In C++
PPTX
Loop structures
PPTX
2nd year computer science chapter 12 notes
PDF
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PDF
Java Repetiotion Statements
PDF
Cpp loop types
PPTX
Lecture 5
PPT
Programs in C based on looping statements
PDF
CS305PC_C++_UNIT 2.pdf jntuh third semester
PDF
whileloop-161225171903.pdf
Loops Basics
loops in C ppt.pdf
Cse lecture-7-c loop
Programming Fundamentals lecture 8
C language 2
Managing input and output operations & Decision making and branching and looping
Loops in C.net.pptx
Loops c++
loops and iteration.docx
Chapter 9 - Loops in C++
Loops In C++
Loop structures
2nd year computer science chapter 12 notes
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
Java Repetiotion Statements
Cpp loop types
Lecture 5
Programs in C based on looping statements
CS305PC_C++_UNIT 2.pdf jntuh third semester
whileloop-161225171903.pdf
Ad

Recently uploaded (20)

PPTX
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
PDF
Module 3: Health Systems Tutorial Slides S2 2025
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PPTX
Introduction and Scope of Bichemistry.pptx
PPTX
IMMUNIZATION PROGRAMME pptx
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
PDF
What Is Coercive Control? Understanding and Recognizing Hidden Abuse
PPTX
Congenital Hypothyroidism pptx
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
PPTX
Strengthening open access through collaboration: building connections with OP...
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
PPTX
Software Engineering BSC DS UNIT 1 .pptx
PDF
LDMMIA Reiki Yoga S2 L3 Vod Sample Preview
PDF
5.Universal-Franchise-and-Indias-Electoral-System.pdfppt/pdf/8th class social...
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PPTX
How to Manage Starshipit in Odoo 18 - Odoo Slides
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
Module 3: Health Systems Tutorial Slides S2 2025
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
Introduction and Scope of Bichemistry.pptx
IMMUNIZATION PROGRAMME pptx
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
What Is Coercive Control? Understanding and Recognizing Hidden Abuse
Congenital Hypothyroidism pptx
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Strengthening open access through collaboration: building connections with OP...
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
UPPER GASTRO INTESTINAL DISORDER.docx
Software Engineering BSC DS UNIT 1 .pptx
LDMMIA Reiki Yoga S2 L3 Vod Sample Preview
5.Universal-Franchise-and-Indias-Electoral-System.pdfppt/pdf/8th class social...
NOI Hackathon - Summer Edition - GreenThumber.pptx
How to Manage Starshipit in Odoo 18 - Odoo Slides

Loops in c

  • 1. LOOPS IN C LANGUAGE
  • 2. LOOPS  WHY DO WE NEED LOOPS ???  There may be a situation, when you need to execute a block of code several number of times.  In general statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.  A loop statement allows us to execute a statement or group of statements multiple times.
  • 3. LOOPS :  TYPES OF LOOPS :  WHILE LOOP  FOR LOOP  DO-WHILE LOOP  NESTED LOOP
  • 4. WHILE LOOP o A while loop statement repeatedly executes a target statement as long as a given condition is true. Syntax: while(condition) { statement(s); }  Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true.  When the condition becomes false, program control passes to the line immediately following the loop.
  • 6. EXAMPLE : #include<stdio.h> int main () { // Local variable declaration: int a = 10; // while loop execution while( a<20 ) { printf(“value of a:%d n”, a); a++; } return 0; } OUTPUT: value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19
  • 7. FOR LOOP o A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. Syntax: for ( init; condition; increment ) { statement(s); } o The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.
  • 8. FOR LOOP cont….  Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.  After the body of the for loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.  The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.
  • 10. Example: #include<stdio.h> int main () { // for loop execution for( int a = 10; a < 20; a = a + 1 ) { printf("value of a: %d n”, a); } return 0; } OUTPUT: value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19
  • 11. DO-WHILE LOOP  Unlike for and while loops, which 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. syntax do { statement(s); } while( condition ); Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop execute once before the condition is tested
  • 13. Example #include<stdio.h> int main () { // Local variable declaration: int a = 10; // do loop execution do { printf( "value of a: %dn “ ,a); a = a + 1; } while( a < 20 ); return 0; } OUTPUT: value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19
  • 14. NESTED LOOPS  A loop can be nested inside of another loop.  Syntax: for ( init; condition; increment ) { for ( init; condition; increment ) { statement(s); } statement(s); // you can put more statements. }
  • 15. EXAMPLE #include<stdio.h> int main () { int a=1,b; while(a<=3) { for(b=1;b<=3;b++) { printf("a = %d , b = %dn",a,b); } printf("n"); a++; } return 0; } OUTPUT: a = 1 , b = 1 a = 1 , b = 2 a = 1 , b = 3 a = 2 , b = 1 a = 2 , b = 2 a = 2 , b = 3 a = 3 , b = 1 a = 3 , b = 2 a = 3 , b = 3