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
PPTX
C Programming Language Step by Step Part 2
PDF
1 introducing c language
PDF
7 functions
PPTX
C Programming Language Part 9
PPSX
C programming function
PPTX
C Programming Language Part 8
PPSX
Functions in c
C Programming Language Part 7
C Programming Language Step by Step Part 2
1 introducing c language
7 functions
C Programming Language Part 9
C programming function
C Programming Language Part 8
Functions in c

What's hot (20)

PPTX
Decision making and branching
PPT
lets play with "c"..!!! :):)
PPTX
Expressions using operator in c
PDF
8 arrays and pointers
PPSX
Concepts of C [Module 2]
PPT
Functions and pointers_unit_4
PDF
4 operators, expressions &amp; statements
PDF
6 c control statements branching &amp; jumping
PPTX
Function in c program
PPTX
Functions in C
PPTX
C Programming Language Part 11
PPTX
PPTX
C programming(Part 1)
PPSX
Function in c
PPTX
C function
PPT
Functions and pointers_unit_4
PDF
9 character string &amp; string library
PPT
Lecture 14 - Scope Rules
PDF
C programming
PPTX
Programming in C (part 2)
Decision making and branching
lets play with "c"..!!! :):)
Expressions using operator in c
8 arrays and pointers
Concepts of C [Module 2]
Functions and pointers_unit_4
4 operators, expressions &amp; statements
6 c control statements branching &amp; jumping
Function in c program
Functions in C
C Programming Language Part 11
C programming(Part 1)
Function in c
C function
Functions and pointers_unit_4
9 character string &amp; string library
Lecture 14 - Scope Rules
C programming
Programming in C (part 2)
Ad

Viewers also liked (20)

PPTX
C Programming Language Tutorial for beginners - JavaTpoint
PPTX
C programming tutorial for beginners
PPTX
Overview of c language
PPTX
C Programming Language Part 5
PPTX
C program to write c program without using main function
PPTX
Steps for c program execution
PPTX
C Programming Language Part 4
PPTX
PPTX
C Programming Language Step by Step Part 1
PPT
C ppt
PPTX
Pointer in c program
PPTX
Basic c programming and explanation PPT1
PPTX
How c program execute in c program
PPTX
C language ppt
PPTX
My first program in c, hello world !
ODP
C language. Introduction
PPTX
Medicaid organization profile
PPTX
Основи мови Ci
PPTX
C Programming Language Tutorial for beginners - JavaTpoint
C programming tutorial for beginners
Overview of c language
C Programming Language Part 5
C program to write c program without using main function
Steps for c program execution
C Programming Language Part 4
C Programming Language Step by Step Part 1
C ppt
Pointer in c program
Basic c programming and explanation PPT1
How c program execute in c program
C language ppt
My first program in c, hello world !
C language. Introduction
Medicaid organization profile
Основи мови Ci
Ad

Similar to C Programming Language Part 6 (20)

DOCX
Looping statements
PDF
PPTX
Decision Making and Looping
DOC
Jumping statements
PPT
C++ programming
PPT
C++ programming
PPT
Ch3 repetition
PPTX
Managing input and output operations & Decision making and branching and looping
PPTX
Object oriented programming system with C++
DOCX
Programming Fundamentals lecture 8
PDF
Chapter 3 - Flow of Control Part II.pdf
PPTX
C Programming Unit-2
PPTX
Loops c++
PPTX
Decision statements in c language
PPTX
Decision statements in c laguage
PPTX
Loops in c
PPTX
Programming in C
PPTX
Final requirement
PPTX
C PROGRAMMING-CONTROL STATEMENT (IF-ELSE, SWITCH)
Looping statements
Decision Making and Looping
Jumping statements
C++ programming
C++ programming
Ch3 repetition
Managing input and output operations & Decision making and branching and looping
Object oriented programming system with C++
Programming Fundamentals lecture 8
Chapter 3 - Flow of Control Part II.pdf
C Programming Unit-2
Loops c++
Decision statements in c language
Decision statements in c laguage
Loops in c
Programming in C
Final requirement
C PROGRAMMING-CONTROL STATEMENT (IF-ELSE, SWITCH)

More from Rumman Ansari (16)

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

Recently uploaded (20)

PPT
Project quality management in manufacturing
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
Sustainable Sites - Green Building Construction
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
DOCX
573137875-Attendance-Management-System-original
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
Project quality management in manufacturing
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Arduino robotics embedded978-1-4302-3184-4.pdf
Lesson 3_Tessellation.pptx finite Mathematics
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
CH1 Production IntroductoryConcepts.pptx
bas. eng. economics group 4 presentation 1.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Lecture Notes Electrical Wiring System Components
Sustainable Sites - Green Building Construction
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
573137875-Attendance-Management-System-original
Model Code of Practice - Construction Work - 21102022 .pdf

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