SlideShare a Scribd company logo
C Programming Language Part 6
Loops
A loop statement allows us to execute a statement or group of statements multiple
times and following is the general form of a loop statement in most of the programming
languages:
Loop Type Description
while loop Repeats a statement or group of statements while a given
condition is true. It tests the condition before executing
the loop body.
for loop Execute a sequence of statements multiple times and
abbreviates the code that manages the loop variable.
do...while loop Like a while statement, except that it tests the condition at
the end of the loop body
nested loops You can use one or more loop inside any another while, for
or do..while loop.
Type of Loops
Loop Control Statements:
Control Statement Description
break statement Terminates the loop or switch statement and transfers
execution to the statement immediately following the
loop or switch.
continue statement Causes the loop to skip the remainder of its body and
immediately retest its condition prior to reiterating.
goto statement Transfers control to the labeled statement. Though it is
not advised to use goto statement in your program.
while loop in C1
Syntax: while (condition)
{
statement(s);
}
Flow Diagram:
A while loop statement in C programming language repeatedly executes a target statement
as long as a given condition is true.
Example:
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* while loop execution */
while( a < 20 )
{
printf("value of a: %dn", a);
a++;
}
return 0;
}
do...while loop in C
2
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 );
Flow Diagram:
Example
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* do loop execution */
do
{
printf("value of a: %dn", a);
a = a + 1;
}while( a < 20 );
return 0;
}
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:
3
for ( init; condition; increment )
{
statement(s);
}
Flow Diagram:
Example:
#include <stdio.h>
int main ()
{
/* for loop execution */
for( int a = 10; a < 20; a = a + 1 )
{
printf("value of a: %dn", a);
}
return 0;
}
Nested loops in C
4
Syntax
Nested for loop
for ( init; condition; increment )
{
for ( init; condition; increment )
{
statement(s);
}
statement(s);
}
Nested while loop
while(condition)
{
while(condition)
{
statement(s);
}
statement(s);
}
Nested do...while loop
do
{
statement(s);
do
{
statement(s);
}while( condition );
}while( condition );
Example of for Loops and while loops
Write a program uses a nested for loop to find the prime numbers from 2 to 100:
#include<stdio.h>
main()
{
int a,n,f;
for(a=2;a<=200;a++)
{
n=2; f=0;
while(n<=a/2)
{
if(a%n==0)
{
f=1;
break;
}
n++;
}
if(f==0)
{
printf("prime %d n",a);
}
else
{
printf(" .....non prime %d n",a);
}
}
}
#include<stdio.h>
main()
{
int a,n,f;
for(a=2;a<=200;a++)
{
f=0;
for(n=2; n<=a/2; n++ )
{
if(a%n==0)
{
f=1;
break;
}
}
if(f==0)
{
printf("prime %d n",a);
}
else
{
printf(" .....non prime %d n",a);
}
}
}
Using
For
loop
Break statement in C1
The break statement in C programming language has the following two usages:
1. When the break statement is encountered inside a loop, the loop is immediately
terminated and program control resumes at the next statement following the loop.
2. It can be used to terminate a case in the switch statement (covered in the next chapter).
Flow Diagram:
Example:
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* while loop execution */
while( a < 20 )
{
printf("value of a: %dn", a);
a++;
if( a > 15)
{
/* terminate the loop using break statement
*/
break;
}
}
return 0;
}
Continue statement in C
2
The continue statement in C programming language works somewhat like
the break statement. Instead of forcing termination, however, continue forces
the next iteration of the loop to take place, skipping any code in between.
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 passes to the conditional tests.
Flow Diagram
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* do loop execution */
do
{
if( a == 15)
{
/* skip the iteration */
a = a + 1;
continue;
}
printf("value of a: %dn", a);
a++;
}while( a < 20 );
return 0;
}
Example
#include<stdio.h>
main( )
{
int i, j ;
for ( i = 1 ; i <= 2 ; i++ )
{
for ( j = 1 ; j <= 2 ; j++ )
{
if ( i == j )
continue ;
printf ( "n%d %dn", i, j ) ;
}
}
}
Example
goto statement in C
A goto statement in C programming language provides an unconditional jump from
the goto to a labeled statement in the same function.
NOTE: Use of goto statement is highly discouraged in any programming language
because it makes difficult to trace the control flow of a program, making the program
hard to understand and hard to modify. Any program that uses a goto can be rewritten
so that it doesn't need the goto.
Syntax
goto label;
..
.
label: statement;
Flow Diagram:
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* do loop execution */
LOOP:do
{
if( a == 15)
{
/* skip the iteration */
a = a + 1;
goto LOOP;
}
printf("value of a: %dn", a);
a++;
}while( a < 20 );
return 0;
}
Example
#include <stdio.h>
main( )
{
int goals ;
printf ( "Enter the number of goals scored against India" ) ;
scanf ( "%d", &goals ) ;
if ( goals <= 5 )
goto sos ;
else
{
printf ( "About time soccer players learnt Cn" ) ;
printf ( "and said goodbye! adieu! to soccer" ) ;
exit( ) ; /* terminates program execution */
}
sos :
printf ( "To err is human!" ) ;
}
Example

More Related Content

PPTX
C Programming Language Part 7
Rumman Ansari
 
PPTX
C Programming Language Step by Step Part 2
Rumman Ansari
 
PDF
1 introducing c language
MomenMostafa
 
PDF
7 functions
MomenMostafa
 
PPTX
C Programming Language Part 9
Rumman Ansari
 
PPSX
C programming function
argusacademy
 
PPTX
C Programming Language Part 8
Rumman Ansari
 
PPSX
Functions in c
Innovative
 
C Programming Language Part 7
Rumman Ansari
 
C Programming Language Step by Step Part 2
Rumman Ansari
 
1 introducing c language
MomenMostafa
 
7 functions
MomenMostafa
 
C Programming Language Part 9
Rumman Ansari
 
C programming function
argusacademy
 
C Programming Language Part 8
Rumman Ansari
 
Functions in c
Innovative
 

What's hot (20)

PPTX
Decision making and branching
Saranya saran
 
PPT
lets play with "c"..!!! :):)
Rupendra Choudhary
 
PPTX
Expressions using operator in c
Saranya saran
 
PDF
8 arrays and pointers
MomenMostafa
 
PPSX
Concepts of C [Module 2]
Abhishek Sinha
 
PPT
Functions and pointers_unit_4
MKalpanaDevi
 
PDF
4 operators, expressions &amp; statements
MomenMostafa
 
PDF
6 c control statements branching &amp; jumping
MomenMostafa
 
PPTX
Function in c program
CGC Technical campus,Mohali
 
PPTX
Functions in C
Princy Nelson
 
PPTX
C Programming Language Part 11
Rumman Ansari
 
PPTX
Function in c
CGC Technical campus,Mohali
 
PPTX
C programming(Part 1)
Dr. SURBHI SAROHA
 
PPSX
Function in c
savitamhaske
 
PPTX
C function
thirumalaikumar3
 
PPT
Functions and pointers_unit_4
Saranya saran
 
PDF
9 character string &amp; string library
MomenMostafa
 
PPT
Lecture 14 - Scope Rules
Md. Imran Hossain Showrov
 
PDF
C programming
Samsil Arefin
 
PPTX
Programming in C (part 2)
Dr. SURBHI SAROHA
 
Decision making and branching
Saranya saran
 
lets play with "c"..!!! :):)
Rupendra Choudhary
 
Expressions using operator in c
Saranya saran
 
8 arrays and pointers
MomenMostafa
 
Concepts of C [Module 2]
Abhishek Sinha
 
Functions and pointers_unit_4
MKalpanaDevi
 
4 operators, expressions &amp; statements
MomenMostafa
 
6 c control statements branching &amp; jumping
MomenMostafa
 
Function in c program
CGC Technical campus,Mohali
 
Functions in C
Princy Nelson
 
C Programming Language Part 11
Rumman Ansari
 
C programming(Part 1)
Dr. SURBHI SAROHA
 
Function in c
savitamhaske
 
C function
thirumalaikumar3
 
Functions and pointers_unit_4
Saranya saran
 
9 character string &amp; string library
MomenMostafa
 
Lecture 14 - Scope Rules
Md. Imran Hossain Showrov
 
C programming
Samsil Arefin
 
Programming in C (part 2)
Dr. SURBHI SAROHA
 
Ad

Viewers also liked (20)

PPTX
C Programming Language Tutorial for beginners - JavaTpoint
JavaTpoint.Com
 
PPTX
C programming tutorial for beginners
Thiyagarajan Soundhiran
 
PPTX
Overview of c language
shalini392
 
PPTX
C Programming Language Part 5
Rumman Ansari
 
PPTX
C program to write c program without using main function
Rumman Ansari
 
PPTX
Steps for c program execution
Rumman Ansari
 
PPTX
C Programming Language Part 4
Rumman Ansari
 
PPTX
Introduction to C Programming
Amr Ali (ISTQB CTAL Full, CSM, ITIL Foundation)
 
PPTX
C Programming Language Step by Step Part 1
Rumman Ansari
 
PPT
C ppt
jasmeen kr
 
PPTX
Pointer in c program
Rumman Ansari
 
PPTX
Basic c programming and explanation PPT1
Rumman Ansari
 
PPTX
How c program execute in c program
Rumman Ansari
 
PPTX
C language ppt
Ğäùråv Júñêjå
 
PPTX
My first program in c, hello world !
Rumman Ansari
 
ODP
C language. Introduction
Alexey Bovanenko
 
PPTX
Medicaid organization profile
Anurag Byala
 
PPTX
Основи мови Ci
Escuela
 
PPTX
7g
96_mavg
 
C Programming Language Tutorial for beginners - JavaTpoint
JavaTpoint.Com
 
C programming tutorial for beginners
Thiyagarajan Soundhiran
 
Overview of c language
shalini392
 
C Programming Language Part 5
Rumman Ansari
 
C program to write c program without using main function
Rumman Ansari
 
Steps for c program execution
Rumman Ansari
 
C Programming Language Part 4
Rumman Ansari
 
C Programming Language Step by Step Part 1
Rumman Ansari
 
C ppt
jasmeen kr
 
Pointer in c program
Rumman Ansari
 
Basic c programming and explanation PPT1
Rumman Ansari
 
How c program execute in c program
Rumman Ansari
 
C language ppt
Ğäùråv Júñêjå
 
My first program in c, hello world !
Rumman Ansari
 
C language. Introduction
Alexey Bovanenko
 
Medicaid organization profile
Anurag Byala
 
Основи мови Ci
Escuela
 
Ad

Similar to C Programming Language Part 6 (20)

DOCX
Looping statements
Chukka Nikhil Chakravarthy
 
PPTX
Cse lecture-7-c loop
FarshidKhan
 
PPTX
C language 2
Arafat Bin Reza
 
PDF
LOOP STATEMENTS AND TYPES OF LOOP IN C LANGUAGE BY RIZWAN
MD RIZWAN MOLLA
 
PDF
Controls & Loops in C
Thesis Scientist Private Limited
 
PPTX
Loops in c
RekhaBudhwar
 
PDF
3 flow
suresh rathod
 
PDF
3 flow
suresh rathod
 
PPTX
Decision Making and Looping
Munazza-Mah-Jabeen
 
PPTX
C PPT.power point presentation about c pro
ROHISIVAM
 
PPTX
Conditional Statement both conditional and loop.pptx
Zenith SVG
 
PDF
Unit II chapter 4 Loops in C
Sowmya Jyothi
 
PPTX
CONTROL STMTS.pptx
JavvajiVenkat
 
PPTX
handling input output and control statements
Rai University
 
PPTX
Bsc cs pic u-3 handling input output and control statements
Rai University
 
DOC
Jumping statements
Suneel Dogra
 
PPTX
Mca i pic u-3 handling input output and control statements
Rai University
 
PPTX
Btech i pic u-3 handling input output and control statements
Rai University
 
PPTX
Diploma ii cfpc u-3 handling input output and control statements
Rai University
 
PPTX
Loops Basics
Mushiii
 
Looping statements
Chukka Nikhil Chakravarthy
 
Cse lecture-7-c loop
FarshidKhan
 
C language 2
Arafat Bin Reza
 
LOOP STATEMENTS AND TYPES OF LOOP IN C LANGUAGE BY RIZWAN
MD RIZWAN MOLLA
 
Controls & Loops in C
Thesis Scientist Private Limited
 
Loops in c
RekhaBudhwar
 
Decision Making and Looping
Munazza-Mah-Jabeen
 
C PPT.power point presentation about c pro
ROHISIVAM
 
Conditional Statement both conditional and loop.pptx
Zenith SVG
 
Unit II chapter 4 Loops in C
Sowmya Jyothi
 
CONTROL STMTS.pptx
JavvajiVenkat
 
handling input output and control statements
Rai University
 
Bsc cs pic u-3 handling input output and control statements
Rai University
 
Jumping statements
Suneel Dogra
 
Mca i pic u-3 handling input output and control statements
Rai University
 
Btech i pic u-3 handling input output and control statements
Rai University
 
Diploma ii cfpc u-3 handling input output and control statements
Rai University
 
Loops Basics
Mushiii
 

More from Rumman Ansari (16)

PDF
Sql tutorial
Rumman Ansari
 
PDF
C programming exercises and solutions
Rumman Ansari
 
PDF
Java Tutorial best website
Rumman Ansari
 
DOCX
Java Questions and Answers
Rumman Ansari
 
DOCX
servlet programming
Rumman Ansari
 
PPTX
What is token c programming
Rumman Ansari
 
PPTX
What is identifier c programming
Rumman Ansari
 
PPTX
What is keyword in c programming
Rumman Ansari
 
PPTX
Type casting in c programming
Rumman Ansari
 
PPTX
C Programming Language Step by Step Part 5
Rumman Ansari
 
PPTX
C Programming Language Step by Step Part 3
Rumman Ansari
 
DOCX
C Programming
Rumman Ansari
 
PPTX
Tail recursion
Rumman Ansari
 
PPTX
Tail Recursion in data structure
Rumman Ansari
 
PDF
Spyware manual
Rumman Ansari
 
PPTX
Linked list
Rumman Ansari
 
Sql tutorial
Rumman Ansari
 
C programming exercises and solutions
Rumman Ansari
 
Java Tutorial best website
Rumman Ansari
 
Java Questions and Answers
Rumman Ansari
 
servlet programming
Rumman Ansari
 
What is token c programming
Rumman Ansari
 
What is identifier c programming
Rumman Ansari
 
What is keyword in c programming
Rumman Ansari
 
Type casting in c programming
Rumman Ansari
 
C Programming Language Step by Step Part 5
Rumman Ansari
 
C Programming Language Step by Step Part 3
Rumman Ansari
 
C Programming
Rumman Ansari
 
Tail recursion
Rumman Ansari
 
Tail Recursion in data structure
Rumman Ansari
 
Spyware manual
Rumman Ansari
 
Linked list
Rumman Ansari
 

Recently uploaded (20)

PPTX
Lesson 3_Tessellation.pptx finite Mathematics
quakeplayz54
 
PDF
algorithms-16-00088-v2hghjjnjnhhhnnjhj.pdf
Ajaykumar966781
 
PDF
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
ghousebhasha2007
 
PPTX
Production of bioplastic from fruit peels.pptx
alwingeorgealwingeor
 
PPTX
Edge to Cloud Protocol HTTP WEBSOCKET MQTT-SN MQTT.pptx
dhanashri894551
 
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
sangeethamtech26
 
PDF
6th International Conference on Artificial Intelligence and Machine Learning ...
gerogepatton
 
PDF
dse_final_merit_2025_26 gtgfffffcjjjuuyy
rushabhjain127
 
PPTX
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
dodultrongaming
 
PPTX
TE-AI-Unit VI notes using planning model
swatigaikwad6389
 
PPTX
Ship’s Structural Components.pptx 7.7 Mb
abdalwhab7327
 
PPTX
Chapter----five---Resource Recovery.pptx
078bce110prashant
 
PPTX
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
VinayB68
 
PDF
Introduction to Data Science: data science process
ShivarkarSandip
 
PPTX
Module_II_Data_Science_Project_Management.pptx
anshitanarain
 
PDF
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
PPTX
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
PPTX
Simulation of electric circuit laws using tinkercad.pptx
VidhyaH3
 
PPT
High Data Link Control Protocol in Data Link Layer
shailajacse
 
Lesson 3_Tessellation.pptx finite Mathematics
quakeplayz54
 
algorithms-16-00088-v2hghjjnjnhhhnnjhj.pdf
Ajaykumar966781
 
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
ghousebhasha2007
 
Production of bioplastic from fruit peels.pptx
alwingeorgealwingeor
 
Edge to Cloud Protocol HTTP WEBSOCKET MQTT-SN MQTT.pptx
dhanashri894551
 
Strings in CPP - Strings in C++ are sequences of characters used to store and...
sangeethamtech26
 
6th International Conference on Artificial Intelligence and Machine Learning ...
gerogepatton
 
dse_final_merit_2025_26 gtgfffffcjjjuuyy
rushabhjain127
 
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
dodultrongaming
 
TE-AI-Unit VI notes using planning model
swatigaikwad6389
 
Ship’s Structural Components.pptx 7.7 Mb
abdalwhab7327
 
Chapter----five---Resource Recovery.pptx
078bce110prashant
 
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
VinayB68
 
Introduction to Data Science: data science process
ShivarkarSandip
 
Module_II_Data_Science_Project_Management.pptx
anshitanarain
 
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
Simulation of electric circuit laws using tinkercad.pptx
VidhyaH3
 
High Data Link Control Protocol in Data Link Layer
shailajacse
 

C Programming Language Part 6

  • 2. Loops A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages:
  • 3. Loop Type Description while loop Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. for loop Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable. do...while loop Like a while statement, except that it tests the condition at the end of the loop body nested loops You can use one or more loop inside any another while, for or do..while loop. Type of Loops
  • 4. Loop Control Statements: Control Statement Description break statement Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch. continue statement Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. goto statement Transfers control to the labeled statement. Though it is not advised to use goto statement in your program.
  • 5. while loop in C1 Syntax: while (condition) { statement(s); } Flow Diagram: A while loop statement in C programming language repeatedly executes a target statement as long as a given condition is true.
  • 6. Example: #include <stdio.h> int main () { /* local variable definition */ int a = 10; /* while loop execution */ while( a < 20 ) { printf("value of a: %dn", a); a++; } return 0; }
  • 7. do...while loop in C 2 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 ); Flow Diagram:
  • 8. Example #include <stdio.h> int main () { /* local variable definition */ int a = 10; /* do loop execution */ do { printf("value of a: %dn", a); a = a + 1; }while( a < 20 ); return 0; }
  • 9. 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: 3 for ( init; condition; increment ) { statement(s); } Flow Diagram:
  • 10. Example: #include <stdio.h> int main () { /* for loop execution */ for( int a = 10; a < 20; a = a + 1 ) { printf("value of a: %dn", a); } return 0; }
  • 11. Nested loops in C 4 Syntax Nested for loop for ( init; condition; increment ) { for ( init; condition; increment ) { statement(s); } statement(s); }
  • 14. Example of for Loops and while loops Write a program uses a nested for loop to find the prime numbers from 2 to 100: #include<stdio.h> main() { int a,n,f; for(a=2;a<=200;a++) { n=2; f=0; while(n<=a/2) { if(a%n==0) { f=1; break; } n++; } if(f==0) { printf("prime %d n",a); } else { printf(" .....non prime %d n",a); } } }
  • 15. #include<stdio.h> main() { int a,n,f; for(a=2;a<=200;a++) { f=0; for(n=2; n<=a/2; n++ ) { if(a%n==0) { f=1; break; } } if(f==0) { printf("prime %d n",a); } else { printf(" .....non prime %d n",a); } } } Using For loop
  • 16. Break statement in C1 The break statement in C programming language has the following two usages: 1. When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop. 2. It can be used to terminate a case in the switch statement (covered in the next chapter).
  • 18. Example: #include <stdio.h> int main () { /* local variable definition */ int a = 10; /* while loop execution */ while( a < 20 ) { printf("value of a: %dn", a); a++; if( a > 15) { /* terminate the loop using break statement */ break; } } return 0; }
  • 19. Continue statement in C 2 The continue statement in C programming language works somewhat like the break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place, skipping any code in between. 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 passes to the conditional tests.
  • 21. #include <stdio.h> int main () { /* local variable definition */ int a = 10; /* do loop execution */ do { if( a == 15) { /* skip the iteration */ a = a + 1; continue; } printf("value of a: %dn", a); a++; }while( a < 20 ); return 0; } Example
  • 22. #include<stdio.h> main( ) { int i, j ; for ( i = 1 ; i <= 2 ; i++ ) { for ( j = 1 ; j <= 2 ; j++ ) { if ( i == j ) continue ; printf ( "n%d %dn", i, j ) ; } } } Example
  • 23. goto statement in C A goto statement in C programming language provides an unconditional jump from the goto to a labeled statement in the same function. NOTE: Use of goto statement is highly discouraged in any programming language because it makes difficult to trace the control flow of a program, making the program hard to understand and hard to modify. Any program that uses a goto can be rewritten so that it doesn't need the goto. Syntax goto label; .. . label: statement;
  • 25. #include <stdio.h> int main () { /* local variable definition */ int a = 10; /* do loop execution */ LOOP:do { if( a == 15) { /* skip the iteration */ a = a + 1; goto LOOP; } printf("value of a: %dn", a); a++; }while( a < 20 ); return 0; } Example
  • 26. #include <stdio.h> main( ) { int goals ; printf ( "Enter the number of goals scored against India" ) ; scanf ( "%d", &goals ) ; if ( goals <= 5 ) goto sos ; else { printf ( "About time soccer players learnt Cn" ) ; printf ( "and said goodbye! adieu! to soccer" ) ; exit( ) ; /* terminates program execution */ } sos : printf ( "To err is human!" ) ; } Example