SlideShare a Scribd company logo
2
Most read
4
Most read
The wizards of For Loop A Lecture By Abdul Ghaffar Khan
‘ C’ and Iterative Logic To apply the Iterative logic we have three control structures in ‘C’ Language as listed below, for   Loop while  loop do while  loop Ch – 8 For Loop By Abdul Ghaffar Khan
The  for  Loop: for  loop is used when a process is to repeat a fixed number of times. The structure of a  for  loop is as follows for(expression_l; expression_2; expression_3) any_instruction; Or for(expression_l; expression_2; expression_3) { body-of-IOOP } Ch – 8 For Loop By Abdul Ghaffar Khan
Order of execution When the loop is started normally a control variable is initialized by executing expression_l. expression_2 is tested if it is false then control is transferred outside the loop otherwise do next step 3 Body of loop is executed. Value of control variable is altered by expression_3 Steps (2) to (4) are repeated until condition becomes false Ch – 8 For Loop By Abdul Ghaffar Khan for(exl; ex2; ex3) {   body-of-IOOP }
Example 8.1  ( Print 10 natural numbers) Ch – 8 For Loop By Abdul Ghaffar Khan main()  {  int NBR; clrscr();  for(NBR=l; NBR<=10; ++NBR)  printf(&quot;%d &quot;,NBR);  getch()  }  Output: 2 3 4 5 6 7  8 9  10
Example 8.2  ( print a table ) main() { int n, p, i= 0; clrscr(); printf(&quot;Enter a number whose table is to print &quot;); scanf(&quot;%d&quot;, &n); printf(&quot;Table of %d \n&quot;, n); for (i=1; i<=12; ++i) { p = i* n; printf(&quot;%d * %4 = %d\n&quot; n, i, p ); . } getch(); } Ch – 8 For Loop By Abdul Ghaffar Khan Output Enter a number whose table is to print 6 6  *  1 = 6 6 *  2 = 12 6 *  3 =18 6  *  4 = 24 6  *  5 = 30 6  *  6 = 36 6  *  7 = 42 6  *  8 = 48  6  *  9 = 54 6  * 10 = 60  6  * 11 = 66 6  * 12 = 72
Example 8.3 Write down a program to generate the following output Ch – 8 For Loop By Abdul Ghaffar Khan C8 lower left corner BC lower right corner
Example 8.3 void main(void) { int x, y; clrscr(); . printf (&quot;\xc9&quot;); for (x=1; x<72 ; ++x) { printf(&quot;\xcd&quot;) ; } printf(&quot;\xBB\n&quot;);  // drawing vertical lines  for (x=1;x<10;++x) { if (x==5) printf(&quot;\xBA\t\t\tWelcome To Learning C\t\t\t\t\xBA\n&quot;); printf(&quot;\xBA\t\t\t\t\t\t\t\t\t\xBA\n&quot;); } Ch – 8 For Loop By Abdul Ghaffar Khan printf(&quot;\xC8&quot;); // drawing bottom line  for (x=1;x<72;++x) {    printf(&quot;\xcd&quot;); } printf(&quot;\xBC\n&quot;) ; getch(); }
Multiple assignments in the for loop We can use more than one  initialization  statement and more than one  Increment decrement  statements however only  one condition  is allowed within a for loop. When used multiple assignments or increment commas separate each statement. for( assignment I, asslgnment2; Condition; Increment1, intement2) . { body of loop } Ch – 8 For Loop By Abdul Ghaffar Khan
Example 8.3 #include<stdio.h> #include<conio.h> void main(void) { int x, y; clrscr(); for(x = 1, y= 10; x<= 10; ++x, --y) printf(&quot;x = %d\tY = %d\n&quot;,x,y); getch(); ; }  Ch – 8 For Loop By Abdul Ghaffar Khan Output: X= 1 Y = 10 X= 2  Y = 9  X= 3  Y = 8 X= 4  Y = 7 X=5  Y = 6 X=6  Y = 5 X=7 Y = 4 X=8  Y =3 X=9  Y =2 X=10  Y =1
The break statement break statement is used to take immediate exit from the for loop. When break statement is executed the control immediately be transferred to the first statement following the for loop.  Ch – 8 For Loop By Abdul Ghaffar Khan main() { for( exp_l ; exp_2; exp_3) { statement1; statement2; if( condition) beak; statement3; statement4; } statement5; statement6; }
The continue statement Continue statement is used to skip the remaining statement in a loop this statement immediately transfer the control to the increment or decrement expression in the loop Ch – 8 For Loop By Abdul Ghaffar Khan main() { for( exp_l ; exp_2;exp_3) { statement 1; statement 2.; if( condition) continue; statement3; statement4; }  statement5;. statement6; }
Nested for loop: When a for loop is completely embedded ( enclosed) within another for loop, the structure is called nested for loop,  Ch – 8 For Loop By Abdul Ghaffar Khan for ( exp1 ; condition;exp2) { body-of-loop 1 for( exp3; condition2;exp4) { body-of-loop2 } }
Example 8.4  ( Factorials) void main(void) { int i,j; long fact; . clrscr(); for (i =1;i<=7;++i) {   fact=l.   for (j=i; j> I; --j)   {   fact *= j;   } printf(&quot;\n%d! = %d\n&quot;,i,fact); } getch(); } Ch – 8 For Loop By Abdul Ghaffar Khan Output: 1! = I 2! =2 3! =6 4! =24 5! = 120 6! = 720 7! = 5040
Example 8.5  ( Stars) void main(void) { int I,j; clrscr(); for (i =1;i<=10;++i) { for 0= 1; j<=i ; ++j) { printf('*'); , } printf(&quot;\n&quot;);  }  getch(); } Ch – 8 For Loop By Abdul Ghaffar Khan Output: *  ** *** **** ***** ****** ******* ******** ********* **********

More Related Content

What's hot (20)

PPTX
Process scheduling
Riya Choudhary
 
PPTX
VIRTUAL MEMORY
Kamran Ashraf
 
PPTX
Cache memory ppt
Arpita Naik
 
PDF
CSS Grid Layout for Topconf, Linz
Rachel Andrew
 
PPT
File handling in C++
Hitesh Kumar
 
PDF
Process scheduling (CPU Scheduling)
Mukesh Chinta
 
PPTX
Power point presentation on memory of computer
Arpita Banerjee
 
PPTX
Allocation of Frames & Thrashing
arifmollick8578
 
PPTX
Multithreading
sagsharma
 
PPTX
Operating System- Services,types.Batch files and DOS history
Dr. SURBHI SAROHA
 
PPT
Satellite advantages and disadvantages and data transmission
bhoumik413
 
PPTX
Looping statements in C
Jeya Lakshmi
 
PPTX
Evolution of operating system
Arshad khan
 
PPT
5. spooling and buffering
myrajendra
 
PPT
Deadlock detection and recovery by saad symbian
saad symbian
 
PPT
Presentation on Internet Cookies
Ritika Barethia
 
PPTX
Java - Exception Handling
Prabhdeep Singh
 
PPTX
Multithreading in java
Monika Mishra
 
PPTX
Bus topology ppt
Ananthkumar6965
 
PPT
6 multiprogramming & time sharing
myrajendra
 
Process scheduling
Riya Choudhary
 
VIRTUAL MEMORY
Kamran Ashraf
 
Cache memory ppt
Arpita Naik
 
CSS Grid Layout for Topconf, Linz
Rachel Andrew
 
File handling in C++
Hitesh Kumar
 
Process scheduling (CPU Scheduling)
Mukesh Chinta
 
Power point presentation on memory of computer
Arpita Banerjee
 
Allocation of Frames & Thrashing
arifmollick8578
 
Multithreading
sagsharma
 
Operating System- Services,types.Batch files and DOS history
Dr. SURBHI SAROHA
 
Satellite advantages and disadvantages and data transmission
bhoumik413
 
Looping statements in C
Jeya Lakshmi
 
Evolution of operating system
Arshad khan
 
5. spooling and buffering
myrajendra
 
Deadlock detection and recovery by saad symbian
saad symbian
 
Presentation on Internet Cookies
Ritika Barethia
 
Java - Exception Handling
Prabhdeep Singh
 
Multithreading in java
Monika Mishra
 
Bus topology ppt
Ananthkumar6965
 
6 multiprogramming & time sharing
myrajendra
 

Similar to For Loop (20)

PPT
Ch3 repetition
Hattori Sidek
 
PPTX
Control Structures in C
sana shaikh
 
PDF
Control structuresin c
Vikash Dhal
 
PPTX
Loops in c
shubhampandav3
 
PPT
FP 201 - Unit 3 Part 2
rohassanie
 
PPT
12 lec 12 loop
kapil078
 
PPTX
C PROGRAMMING-CONTROL STATEMENT (IF-ELSE, SWITCH)
PallaviGholap4
 
PDF
Programming basics
246paa
 
PDF
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
JNTUK KAKINADA
 
PPTX
Unit 5 Control Structures.pptx
Precise Mya
 
PPTX
JPC#8 Introduction to Java Programming
Pathomchon Sriwilairit
 
PDF
Fundamental of Information Technology - UNIT 8
Shipra Swati
 
PPTX
C Programming Unit-2
Vikram Nandini
 
DOCX
Looping statements
Chukka Nikhil Chakravarthy
 
PPTX
C Programming Control Structures(if,if-else)
poonambhagat36
 
PPTX
COM1407: Program Control Structures – Repetition and Loops
Hemantha Kulathilake
 
PDF
C_Dayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 2.pdf
amanpathak160605
 
Ch3 repetition
Hattori Sidek
 
Control Structures in C
sana shaikh
 
Control structuresin c
Vikash Dhal
 
Loops in c
shubhampandav3
 
FP 201 - Unit 3 Part 2
rohassanie
 
12 lec 12 loop
kapil078
 
C PROGRAMMING-CONTROL STATEMENT (IF-ELSE, SWITCH)
PallaviGholap4
 
Programming basics
246paa
 
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
JNTUK KAKINADA
 
Unit 5 Control Structures.pptx
Precise Mya
 
JPC#8 Introduction to Java Programming
Pathomchon Sriwilairit
 
Fundamental of Information Technology - UNIT 8
Shipra Swati
 
C Programming Unit-2
Vikram Nandini
 
Looping statements
Chukka Nikhil Chakravarthy
 
C Programming Control Structures(if,if-else)
poonambhagat36
 
COM1407: Program Control Structures – Repetition and Loops
Hemantha Kulathilake
 
C_Dayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 2.pdf
amanpathak160605
 
Ad

More from Ghaffar Khan (20)

PPT
World is beautiful ... ...
Ghaffar Khan
 
PPTX
My Presentation On Ajax
Ghaffar Khan
 
PPT
Sorting
Ghaffar Khan
 
PPT
How A Computer Works
Ghaffar Khan
 
PPT
Exponential and Logarthmic funtions
Ghaffar Khan
 
PPT
Exponential and Logarthmic funtions (1)
Ghaffar Khan
 
PPT
Functions
Ghaffar Khan
 
PPT
Quadratic And Polinomial Function
Ghaffar Khan
 
PPT
Quadratic And Polinomial Function
Ghaffar Khan
 
PPT
Exponentioal And Logarthmic Functions
Ghaffar Khan
 
PPT
Internet Protocol
Ghaffar Khan
 
PPT
Introduction to Computer Networks
Ghaffar Khan
 
PPT
Network Layer
Ghaffar Khan
 
PPT
Control Structures
Ghaffar Khan
 
PPT
Input And Output
Ghaffar Khan
 
PPT
Surfaces
Ghaffar Khan
 
PPT
Vector Tools
Ghaffar Khan
 
PPT
Drawing Tools
Ghaffar Khan
 
PPT
Drawing Figures
Ghaffar Khan
 
PPT
Computer Graphics Introduction
Ghaffar Khan
 
World is beautiful ... ...
Ghaffar Khan
 
My Presentation On Ajax
Ghaffar Khan
 
Sorting
Ghaffar Khan
 
How A Computer Works
Ghaffar Khan
 
Exponential and Logarthmic funtions
Ghaffar Khan
 
Exponential and Logarthmic funtions (1)
Ghaffar Khan
 
Functions
Ghaffar Khan
 
Quadratic And Polinomial Function
Ghaffar Khan
 
Quadratic And Polinomial Function
Ghaffar Khan
 
Exponentioal And Logarthmic Functions
Ghaffar Khan
 
Internet Protocol
Ghaffar Khan
 
Introduction to Computer Networks
Ghaffar Khan
 
Network Layer
Ghaffar Khan
 
Control Structures
Ghaffar Khan
 
Input And Output
Ghaffar Khan
 
Surfaces
Ghaffar Khan
 
Vector Tools
Ghaffar Khan
 
Drawing Tools
Ghaffar Khan
 
Drawing Figures
Ghaffar Khan
 
Computer Graphics Introduction
Ghaffar Khan
 
Ad

Recently uploaded (20)

PDF
Building Resilience with Digital Twins : Lessons from Korea
SANGHEE SHIN
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
PDF
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Women in Automation Presents: Reinventing Yourself — Bold Career Pivots That ...
DianaGray10
 
PDF
Rethinking Security Operations - SOC Evolution Journey.pdf
Haris Chughtai
 
PDF
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PDF
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
PPTX
Building and Operating a Private Cloud with CloudStack and LINBIT CloudStack ...
ShapeBlue
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
Building Resilience with Digital Twins : Lessons from Korea
SANGHEE SHIN
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Women in Automation Presents: Reinventing Yourself — Bold Career Pivots That ...
DianaGray10
 
Rethinking Security Operations - SOC Evolution Journey.pdf
Haris Chughtai
 
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
Building and Operating a Private Cloud with CloudStack and LINBIT CloudStack ...
ShapeBlue
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 

For Loop

  • 1. The wizards of For Loop A Lecture By Abdul Ghaffar Khan
  • 2. ‘ C’ and Iterative Logic To apply the Iterative logic we have three control structures in ‘C’ Language as listed below, for Loop while loop do while loop Ch – 8 For Loop By Abdul Ghaffar Khan
  • 3. The for Loop: for loop is used when a process is to repeat a fixed number of times. The structure of a for loop is as follows for(expression_l; expression_2; expression_3) any_instruction; Or for(expression_l; expression_2; expression_3) { body-of-IOOP } Ch – 8 For Loop By Abdul Ghaffar Khan
  • 4. Order of execution When the loop is started normally a control variable is initialized by executing expression_l. expression_2 is tested if it is false then control is transferred outside the loop otherwise do next step 3 Body of loop is executed. Value of control variable is altered by expression_3 Steps (2) to (4) are repeated until condition becomes false Ch – 8 For Loop By Abdul Ghaffar Khan for(exl; ex2; ex3) { body-of-IOOP }
  • 5. Example 8.1 ( Print 10 natural numbers) Ch – 8 For Loop By Abdul Ghaffar Khan main() { int NBR; clrscr(); for(NBR=l; NBR<=10; ++NBR) printf(&quot;%d &quot;,NBR); getch() } Output: 2 3 4 5 6 7 8 9 10
  • 6. Example 8.2 ( print a table ) main() { int n, p, i= 0; clrscr(); printf(&quot;Enter a number whose table is to print &quot;); scanf(&quot;%d&quot;, &n); printf(&quot;Table of %d \n&quot;, n); for (i=1; i<=12; ++i) { p = i* n; printf(&quot;%d * %4 = %d\n&quot; n, i, p ); . } getch(); } Ch – 8 For Loop By Abdul Ghaffar Khan Output Enter a number whose table is to print 6 6 * 1 = 6 6 * 2 = 12 6 * 3 =18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 6 * 7 = 42 6 * 8 = 48 6 * 9 = 54 6 * 10 = 60 6 * 11 = 66 6 * 12 = 72
  • 7. Example 8.3 Write down a program to generate the following output Ch – 8 For Loop By Abdul Ghaffar Khan C8 lower left corner BC lower right corner
  • 8. Example 8.3 void main(void) { int x, y; clrscr(); . printf (&quot;\xc9&quot;); for (x=1; x<72 ; ++x) { printf(&quot;\xcd&quot;) ; } printf(&quot;\xBB\n&quot;); // drawing vertical lines for (x=1;x<10;++x) { if (x==5) printf(&quot;\xBA\t\t\tWelcome To Learning C\t\t\t\t\xBA\n&quot;); printf(&quot;\xBA\t\t\t\t\t\t\t\t\t\xBA\n&quot;); } Ch – 8 For Loop By Abdul Ghaffar Khan printf(&quot;\xC8&quot;); // drawing bottom line for (x=1;x<72;++x) { printf(&quot;\xcd&quot;); } printf(&quot;\xBC\n&quot;) ; getch(); }
  • 9. Multiple assignments in the for loop We can use more than one initialization statement and more than one Increment decrement statements however only one condition is allowed within a for loop. When used multiple assignments or increment commas separate each statement. for( assignment I, asslgnment2; Condition; Increment1, intement2) . { body of loop } Ch – 8 For Loop By Abdul Ghaffar Khan
  • 10. Example 8.3 #include<stdio.h> #include<conio.h> void main(void) { int x, y; clrscr(); for(x = 1, y= 10; x<= 10; ++x, --y) printf(&quot;x = %d\tY = %d\n&quot;,x,y); getch(); ; } Ch – 8 For Loop By Abdul Ghaffar Khan Output: X= 1 Y = 10 X= 2 Y = 9 X= 3 Y = 8 X= 4 Y = 7 X=5 Y = 6 X=6 Y = 5 X=7 Y = 4 X=8 Y =3 X=9 Y =2 X=10 Y =1
  • 11. The break statement break statement is used to take immediate exit from the for loop. When break statement is executed the control immediately be transferred to the first statement following the for loop. Ch – 8 For Loop By Abdul Ghaffar Khan main() { for( exp_l ; exp_2; exp_3) { statement1; statement2; if( condition) beak; statement3; statement4; } statement5; statement6; }
  • 12. The continue statement Continue statement is used to skip the remaining statement in a loop this statement immediately transfer the control to the increment or decrement expression in the loop Ch – 8 For Loop By Abdul Ghaffar Khan main() { for( exp_l ; exp_2;exp_3) { statement 1; statement 2.; if( condition) continue; statement3; statement4; } statement5;. statement6; }
  • 13. Nested for loop: When a for loop is completely embedded ( enclosed) within another for loop, the structure is called nested for loop, Ch – 8 For Loop By Abdul Ghaffar Khan for ( exp1 ; condition;exp2) { body-of-loop 1 for( exp3; condition2;exp4) { body-of-loop2 } }
  • 14. Example 8.4 ( Factorials) void main(void) { int i,j; long fact; . clrscr(); for (i =1;i<=7;++i) { fact=l. for (j=i; j> I; --j) { fact *= j; } printf(&quot;\n%d! = %d\n&quot;,i,fact); } getch(); } Ch – 8 For Loop By Abdul Ghaffar Khan Output: 1! = I 2! =2 3! =6 4! =24 5! = 120 6! = 720 7! = 5040
  • 15. Example 8.5 ( Stars) void main(void) { int I,j; clrscr(); for (i =1;i<=10;++i) { for 0= 1; j<=i ; ++j) { printf('*'); , } printf(&quot;\n&quot;); } getch(); } Ch – 8 For Loop By Abdul Ghaffar Khan Output: * ** *** **** ***** ****** ******* ******** ********* **********