SlideShare a Scribd company logo
08
RepetitionControl Structure
C - Loops
You may encounter situations, when a block of code needs to be executed 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.
Programming languages provide various control structures that allow for more complicated
execution paths.
A loop statement allows us to execute a statement or group of statements multiple times.
Or Loops are used to repeat a block of code.
Given below is the general form of a loop statement in most of the programming languages −
Types ofloops
1. for
2. while
3. do while
1. for loop
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);
}
Here is the flow of control in a 'for' loop −
 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.
 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 the 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 Chart
Example
#include <stdio.h>
int main ()
{
int a;
/* for loop execution */
for( a = 10; a < 20; a = a + 1 )
{
printf("value of a: %dn", 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
while loop in C
A while loop in C programming 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 nonzero value. The loop iterates while the condition is true. When the
condition becomes false, the program control passes to the line immediately following the loop.
Flow Diagram
Here, the key point to note is that a while loop might not execute at all. When the condition is
tested and the result is false, the loop body will be skipped and the first statement after the while
loop will be executed.
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;
}
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
More about loops
Based on the nature of the control variables and the kind of value assigned to, the loops may be
classified into following two general categories;
a) Counter controlled loop
b) Sentinel Value controlled loop
a) Counter controlled loops:
The type of loops, where the number of the execution is known in advance are termed by the
counter controlled loop. That means, in this case, the value of the variable which controls the
execution of the loop is previously known. The control variable is known as counter. A counter
controlled loop is also called definite repetition loop.
Example : A while loop is an example of counter controlled loop.
sum = 0;
n = 1;
while (n <= 10)
{
sum = sum + n*n;
n = n+ 1;
}
Here, the loop will be executed exactly 10 times for n = 1,2,3,......,10.
b) Sentinel Value controlled loop :
The type of loop where the number of execution of the loop is unknown, is termed by sentinel
controlled loop. In this case, the value of the control variable differs within a limitation and the
execution can be terminated at any moment as the value of the variable is not controlled by the
loop. The control variable in this case is termed by sentinel variable.
Example : The following do....while loop is an example of sentinel controlled loop.
do
{
printf(“Input a number.n”);
scanf("%d", &num);
}
while(num>0);
In the above example, the loop will be executed till the entered value of the variable num is not 0
or less then 0. This is a sentinel controlled loop and here the variable num is a sentinel variable.
goto statement in C
A goto statement in C programming provides an unconditional jump from the 'goto' to a labeled
statement in the same function.
Syntax
goto label;
..
.
label: statement;
Example
#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;
}
Output
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Constant
In the C programming languages, const is a type qualifier:[a]
a keyword applied to a data type that
indicates that the data is constant (does not vary). You can use const prefix to declare constants
with a specific type as follows −
const type variable = value;
Example
#include <stdio.h>
int main() {
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = 'n';
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
printf("%c", NEWLINE);
return 0;
}
Output
value of area : 50
#include<stdio.h>
#include<conio.h>
void main()
{
const int std_reg_no;
std_reg_no= 5;
printf(“Value of you have storedis=%d”, std_reg_no);
std_reg_no= 7; .
}
Error:Cannot modify a const object

More Related Content

PPTX
Loops in c
PPTX
Looping Statement And Flow Chart
PPTX
Looping Structures
PPTX
Loops in c language
PPTX
Lecture 5
DOC
Palindrome number program in c
DOCX
Looping statements
PPTX
C++ loop
Loops in c
Looping Statement And Flow Chart
Looping Structures
Loops in c language
Lecture 5
Palindrome number program in c
Looping statements
C++ loop

What's hot (18)

PPT
For Loop
PPTX
Decision statements in c language
DOCX
Conditional Control
PPTX
C Language - Switch and For Loop
PPT
C++loop statements
PDF
Unit II chapter 4 Loops in C
PPTX
Loop c++
PPT
C++ control loops
PDF
C++ control structure
PPTX
Presentation on nesting of loops
PPTX
Loop control in c++
PPT
Looping in c++
PPTX
Comp ppt (1)
PDF
Control statements
PPT
Looping statements in Java
PPTX
C decision making and looping.
For Loop
Decision statements in c language
Conditional Control
C Language - Switch and For Loop
C++loop statements
Unit II chapter 4 Loops in C
Loop c++
C++ control loops
C++ control structure
Presentation on nesting of loops
Loop control in c++
Looping in c++
Comp ppt (1)
Control statements
Looping statements in Java
C decision making and looping.
Ad

Viewers also liked (8)

PPTX
Chapter3: fundamental programming
DOCX
How to make presentation effective assignment
PPT
Fundamental Programming Lect 2
PPT
Fundamental Programming Lect 1
PPTX
Programming Fundamentals lecture 2
PPTX
Programming Fundamental Slide No.1
PPT
Programming fundamentals lecture 1&2
PPTX
Programming fundamentals lecture 1 0f c
Chapter3: fundamental programming
How to make presentation effective assignment
Fundamental Programming Lect 2
Fundamental Programming Lect 1
Programming Fundamentals lecture 2
Programming Fundamental Slide No.1
Programming fundamentals lecture 1&2
Programming fundamentals lecture 1 0f c
Ad

Similar to Programming Fundamentals lecture 8 (20)

PPT
Decision making and looping
PDF
Controls & Loops in C
PPTX
Loops Basics
PDF
Loop and while Loop
PPTX
Control Structures in C
PPTX
Decision Making and Looping
PPTX
C Programming Language Part 6
DOC
Slide07 repetitions
PPTX
Cse lecture-7-c loop
PPTX
C Programming - Decision making, Looping
PDF
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
PDF
LOOP STATEMENTS AND TYPES OF LOOP IN C LANGUAGE BY RIZWAN
PPTX
Managing input and output operations & Decision making and branching and looping
PPT
Programs in C based on looping statements
PPTX
C language 2
PDF
loops in C ppt.pdf
DOCX
loops and iteration.docx
PPTX
C Programming: Looping Statements in C Pgm
PDF
[ITP - Lecture 11] Loops in C/C++
PPT
12 lec 12 loop
Decision making and looping
Controls & Loops in C
Loops Basics
Loop and while Loop
Control Structures in C
Decision Making and Looping
C Programming Language Part 6
Slide07 repetitions
Cse lecture-7-c loop
C Programming - Decision making, Looping
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
LOOP STATEMENTS AND TYPES OF LOOP IN C LANGUAGE BY RIZWAN
Managing input and output operations & Decision making and branching and looping
Programs in C based on looping statements
C language 2
loops in C ppt.pdf
loops and iteration.docx
C Programming: Looping Statements in C Pgm
[ITP - Lecture 11] Loops in C/C++
12 lec 12 loop

More from REHAN IJAZ (12)

DOCX
Project code for Project on Student information management system
PPTX
Introduction to artificial intelligence lecture 1
DOCX
Programming Fundamentals lecture 7
PPTX
Programming Fundamentals lecture 6
PPTX
Programming Fundamentals lecture 5
DOCX
Programming Fundamentals lecture 4
PPTX
Programming Fundamentals lecture 3
PPTX
Programming Fundamentals lecture 1
PDF
Career development interviews
DOC
importance of Communication in business
DOCX
Porposal on Student information management system
PPTX
Project on Student information management system
Project code for Project on Student information management system
Introduction to artificial intelligence lecture 1
Programming Fundamentals lecture 7
Programming Fundamentals lecture 6
Programming Fundamentals lecture 5
Programming Fundamentals lecture 4
Programming Fundamentals lecture 3
Programming Fundamentals lecture 1
Career development interviews
importance of Communication in business
Porposal on Student information management system
Project on Student information management system

Recently uploaded (20)

PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Digital Logic Computer Design lecture notes
DOCX
573137875-Attendance-Management-System-original
PPTX
Construction Project Organization Group 2.pptx
PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
web development for engineering and engineering
PDF
Structs to JSON How Go Powers REST APIs.pdf
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPT
Mechanical Engineering MATERIALS Selection
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
additive manufacturing of ss316l using mig welding
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPT
Project quality management in manufacturing
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Digital Logic Computer Design lecture notes
573137875-Attendance-Management-System-original
Construction Project Organization Group 2.pptx
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
CH1 Production IntroductoryConcepts.pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
web development for engineering and engineering
Structs to JSON How Go Powers REST APIs.pdf
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Mechanical Engineering MATERIALS Selection
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
bas. eng. economics group 4 presentation 1.pptx
additive manufacturing of ss316l using mig welding
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Project quality management in manufacturing

Programming Fundamentals lecture 8

  • 1. 08 RepetitionControl Structure C - Loops You may encounter situations, when a block of code needs to be executed 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. Programming languages provide various control structures that allow for more complicated execution paths. A loop statement allows us to execute a statement or group of statements multiple times. Or Loops are used to repeat a block of code. Given below is the general form of a loop statement in most of the programming languages − Types ofloops 1. for 2. while 3. do while 1. for loop 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); }
  • 2. Here is the flow of control in a 'for' loop −  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.  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 the 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 Chart
  • 3. Example #include <stdio.h> int main () { int a; /* for loop execution */ for( a = 10; a < 20; a = a + 1 ) { printf("value of a: %dn", 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 while loop in C A while loop in C programming 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 nonzero value. The loop iterates while the condition is true. When the condition becomes false, the program control passes to the line immediately following the loop.
  • 4. Flow Diagram Here, the key point to note is that a while loop might not execute at all. When the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed. 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; } 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
  • 5. value of a: 19 More about loops Based on the nature of the control variables and the kind of value assigned to, the loops may be classified into following two general categories; a) Counter controlled loop b) Sentinel Value controlled loop a) Counter controlled loops: The type of loops, where the number of the execution is known in advance are termed by the counter controlled loop. That means, in this case, the value of the variable which controls the execution of the loop is previously known. The control variable is known as counter. A counter controlled loop is also called definite repetition loop. Example : A while loop is an example of counter controlled loop. sum = 0; n = 1; while (n <= 10) { sum = sum + n*n; n = n+ 1; } Here, the loop will be executed exactly 10 times for n = 1,2,3,......,10. b) Sentinel Value controlled loop : The type of loop where the number of execution of the loop is unknown, is termed by sentinel controlled loop. In this case, the value of the control variable differs within a limitation and the execution can be terminated at any moment as the value of the variable is not controlled by the loop. The control variable in this case is termed by sentinel variable. Example : The following do....while loop is an example of sentinel controlled loop. do { printf(“Input a number.n”); scanf("%d", &num); } while(num>0);
  • 6. In the above example, the loop will be executed till the entered value of the variable num is not 0 or less then 0. This is a sentinel controlled loop and here the variable num is a sentinel variable. goto statement in C A goto statement in C programming provides an unconditional jump from the 'goto' to a labeled statement in the same function. Syntax goto label; .. . label: statement; Example #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; } Output value of a: 10 value of a: 11 value of a: 12
  • 7. value of a: 13 value of a: 14 value of a: 16 value of a: 17 value of a: 18 value of a: 19
  • 8. Constant In the C programming languages, const is a type qualifier:[a] a keyword applied to a data type that indicates that the data is constant (does not vary). You can use const prefix to declare constants with a specific type as follows − const type variable = value; Example #include <stdio.h> int main() { const int LENGTH = 10; const int WIDTH = 5; const char NEWLINE = 'n'; int area; area = LENGTH * WIDTH; printf("value of area : %d", area); printf("%c", NEWLINE); return 0; } Output value of area : 50 #include<stdio.h> #include<conio.h> void main() { const int std_reg_no; std_reg_no= 5; printf(“Value of you have storedis=%d”, std_reg_no); std_reg_no= 7; . }
  • 9. Error:Cannot modify a const object