SlideShare a Scribd company logo
1 of 20
Download to read offline
CSC 103
Lecture 9
Introduction to Computers and Programming
Loop Control Instructions
 When an activity needs to be performed more than
once, then the mechanism that meets this need is
called Loop
 Loop is defined as to perform set of instructions
repeatedly
 Three major loop structures in C.
 The for loop
 The while loop
 the do-while loop (also called cousin of while loop)
2
The for Loop
 When an activity needs to be performed a fixed
number of times then The for loop is used in such
cases.
 For example to calculate the paychecks for 120
employees or printout the squares of all the numbers
from 1 to 50 etc.
 In all such cases and other related the for loop is best
suited
3
General form of for loop Statement
4
for ( initialize counter ; test counter ; increment counter )
{
do this ;
and this ;
and this ;
}
for loop Flow Chart Structure
5
Some valid for loop expressions
6
 for ( i = 10 ; i ; i -- )
printf ( " %d", i ) ;
 for ( j=0 ; j < 5 ; j++ )
printf ( " %d", j ) ;
 for ( i = 1; i <=10 ; printf ("%d",i++ ) )
 for ( scanf ( "%d", &i ) ; i <= 10 ; i++ )
printf ( "%d", i ) ;
The for Loop
7
main()
{
int count;
for (count=0; count<10; count++)
printf ("count=%dn", count);
}
Output:
count=0
count=1
count=2
count=3
count=4
count=5
count=6
count=7
count=8
count=9
Structure of the for Loop
for (count=0; count<10; count++)
 Parentheses following keyword for contain what we
will call the “loop expression”
 Loop expression is divided by semicolons into the
following three separate expressions:
 the “initialize expression” i.e count=0
 the “test expression” i.e count<10
 the “increment expression” i.e count++
 The count variable has a key role, it is used to control
the operations of the loop
8
Structure of the for Loop
9
the “initialize expression”
 count=0, initialize the count variable
 It is executed as soon as loop is entered
 It can start from any given number
 However, loops often start at 1
Structure of the for Loop
10
the “test expression”
 count<10, tests each time to see the count value
 It makes use of relational operator (in this case <)
 The loop will be executed till the test expression
becomes false
 When it become false the loop will be terminated
 and control will pass to next statement following loop
Structure of the for Loop
the “increment expression”
 count++ (same as count=count+1), it increments
the variable count each time loop is executed
 It make use of increment operator i.e. ++
 It should be noted that it will not only be incremented
but can also be decremented as well.
11
The Body of the for Loop
 Following the keyword for and the loop expression (as
discussed above) is the body of the loop, i.e. the
statement (or statements) that will be executed each
time round the loop. i.e. in our example, only one
statement:
printf ("count=%dn", count);
 Note that in a for loop don’t place semicolon between
loop expression & body of loop, since the keyword for
& loop expression don’t constitute a complete C
statement
For Example: for (count=0; count<10; count++)
;
12
Operations of the for Loop
 1st initialization expression executed then
 2nd test condition examined
 If False, the body of the loop will not be executed
 If True, the body of the loop be executed
 3rd increment expression executed
 Note that printf will be executed before increment
so the 1st value will be printed “0”
 Process will continue till test expression become false.
13
Multiple statement in the for Loop
 In our previous example only one statement is used in
the body of the loop i.e
printf ("count=%dn", count);
 However, two or more than two statements can also
be used in a loop
14
Multiple statement in the for Loop
15
main()
{
int count, total;
for (count=0, total=0; count<10; count++)
{
total = total + count;
printf ("count=%d, total=%dn", count, total);
}
}
Output:
count=0, total=0
count=1, total=1
count=2, total=3
count=3, total=6
count=4, total=10
count=5, total=15
count=6, total=21
count=7, total=28
count=8, total=36
count=9, total=45
Multiple statement in the for Loop
 Two points to remember:
 The whole package i.e. the opening brace, the
statements, and closing brace, is a single C statement.
Often called “Compound Statement” or “block”
 Each statement within the block is also a C statement
and must be terminated with a semicolon as in the usual
way
 We can also use
total += count;
it is same as total = total + count;
16
Multiple Initialization in for Loop
17
 In for loop, we have initialized multiple variables
 These variables are separated by comma “,”
count=0 and total=0
for (count=0, total=0; count<10; count++)
 in this example we really didn’t need to initialize total
within the loop, we could also have done:
total=0;
for (count=0; count<10; count++)
 we can also increment two or more variables at the same
time
 similarly, we can have multiple conditions
for (i=10,j=0; i>0,j<10; i--,j++)
Exercise
18
 Input a number from user and print its table
 Program output should look like:
Enter a number: 7
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
19
main()
{
int a;
printf("Enter a number: ");
scanf("%d", &a);
int i;
for (i=1; i<=10; i++)
{
printf ("%d x %d t= %dn", a, i, a*i);
}
}
Change Exercise a bit
20
 Input a number from user to print its table. Also, ask
user to enter how many times you want to generate
the table.
 Program output should look like:
Enter a number: 2
Enter number of times: 5
2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10

More Related Content

What's hot

Java Programming: Loops
Java Programming: LoopsJava Programming: Loops
Java Programming: Loops
Karwan Mustafa Kareem
 
Java Programmin: Selections
Java Programmin: SelectionsJava Programmin: Selections
Java Programmin: Selections
Karwan Mustafa Kareem
 
Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7
alish sha
 
C++ control structure
C++ control structureC++ control structure
C++ control structure
bluejayjunior
 

What's hot (20)

Java Programming: Loops
Java Programming: LoopsJava Programming: Loops
Java Programming: Loops
 
For Loops and Nesting in Python
For Loops and Nesting in PythonFor Loops and Nesting in Python
For Loops and Nesting in Python
 
Loop c++
Loop c++Loop c++
Loop c++
 
For Loop
For LoopFor Loop
For Loop
 
Looping statements
Looping statementsLooping statements
Looping statements
 
for loop in java
for loop in java for loop in java
for loop in java
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements looping
 
Looping Statement And Flow Chart
 Looping Statement And Flow Chart Looping Statement And Flow Chart
Looping Statement And Flow Chart
 
Java Programmin: Selections
Java Programmin: SelectionsJava Programmin: Selections
Java Programmin: Selections
 
130707833146508191
130707833146508191130707833146508191
130707833146508191
 
Looping
LoopingLooping
Looping
 
Looping in C
Looping in CLooping in C
Looping in C
 
Conditional Loops Python
Conditional Loops PythonConditional Loops Python
Conditional Loops Python
 
Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7
 
Programming Fundamentals lecture 8
Programming Fundamentals lecture 8Programming Fundamentals lecture 8
Programming Fundamentals lecture 8
 
Loops in c
Loops in cLoops in c
Loops in c
 
Semaphore
SemaphoreSemaphore
Semaphore
 
Semaphore
SemaphoreSemaphore
Semaphore
 
C++ control structure
C++ control structureC++ control structure
C++ control structure
 
C Language - Switch and For Loop
C Language - Switch and For LoopC Language - Switch and For Loop
C Language - Switch and For Loop
 

Viewers also liked

Islamic Studies - Lecture#1 (Religion)
Islamic Studies - Lecture#1 (Religion)Islamic Studies - Lecture#1 (Religion)
Islamic Studies - Lecture#1 (Religion)
hassaanciit
 
Islamic Studies - Course Outline
Islamic Studies - Course OutlineIslamic Studies - Course Outline
Islamic Studies - Course Outline
hassaanciit
 
Introduction to Computer and Programing - Lab2
Introduction to Computer and Programing - Lab2Introduction to Computer and Programing - Lab2
Introduction to Computer and Programing - Lab2
hassaanciit
 

Viewers also liked (20)

Sharia
ShariaSharia
Sharia
 
Islamic Studies - Lecture#1 (Religion)
Islamic Studies - Lecture#1 (Religion)Islamic Studies - Lecture#1 (Religion)
Islamic Studies - Lecture#1 (Religion)
 
What is Sharia Law?
What is Sharia Law?What is Sharia Law?
What is Sharia Law?
 
Business analyst job description
Business analyst job descriptionBusiness analyst job description
Business analyst job description
 
Ch01(1)
Ch01(1)Ch01(1)
Ch01(1)
 
Week 11
Week 11Week 11
Week 11
 
Ebc10 e ch13-instructor ppt-final
Ebc10 e ch13-instructor ppt-finalEbc10 e ch13-instructor ppt-final
Ebc10 e ch13-instructor ppt-final
 
Ebc10 e ch14-instructor ppt-final
Ebc10 e ch14-instructor ppt-finalEbc10 e ch14-instructor ppt-final
Ebc10 e ch14-instructor ppt-final
 
Week 06 power_point-acct_101_8w_online
Week 06 power_point-acct_101_8w_onlineWeek 06 power_point-acct_101_8w_online
Week 06 power_point-acct_101_8w_online
 
Ebc10e ch04-instructor ppt-final
Ebc10e ch04-instructor ppt-finalEbc10e ch04-instructor ppt-final
Ebc10e ch04-instructor ppt-final
 
Ebc10e ch07-instructor ppt-final
Ebc10e ch07-instructor ppt-finalEbc10e ch07-instructor ppt-final
Ebc10e ch07-instructor ppt-final
 
Ebc10e ch02-instructor ppt-final
Ebc10e ch02-instructor ppt-finalEbc10e ch02-instructor ppt-final
Ebc10e ch02-instructor ppt-final
 
Ebc10 e ch12-instructor ppt - final
Ebc10 e ch12-instructor ppt - finalEbc10 e ch12-instructor ppt - final
Ebc10 e ch12-instructor ppt - final
 
Ebc10e ch08-instructor ppt-final
Ebc10e ch08-instructor ppt-finalEbc10e ch08-instructor ppt-final
Ebc10e ch08-instructor ppt-final
 
Ebc10e ch06-instructor ppt-final (2)
Ebc10e ch06-instructor ppt-final (2)Ebc10e ch06-instructor ppt-final (2)
Ebc10e ch06-instructor ppt-final (2)
 
Islam s6 shari'a law and dhimmi
Islam s6 shari'a law and dhimmiIslam s6 shari'a law and dhimmi
Islam s6 shari'a law and dhimmi
 
Islamic Studies - Course Outline
Islamic Studies - Course OutlineIslamic Studies - Course Outline
Islamic Studies - Course Outline
 
Ebc10e ch03-instructor ppt-final
Ebc10e ch03-instructor ppt-finalEbc10e ch03-instructor ppt-final
Ebc10e ch03-instructor ppt-final
 
Introduction to Computer and Programing - Lab2
Introduction to Computer and Programing - Lab2Introduction to Computer and Programing - Lab2
Introduction to Computer and Programing - Lab2
 
Ch13 instructor
Ch13 instructorCh13 instructor
Ch13 instructor
 

Similar to ICP - Lecture 9

Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
Vince Vo
 
12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptx12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptx
AqeelAbbas94
 

Similar to ICP - Lecture 9 (20)

Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopLoops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
 
Lec7 - Loops updated.pptx
Lec7 - Loops updated.pptxLec7 - Loops updated.pptx
Lec7 - Loops updated.pptx
 
Workbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdfWorkbook_2_Problem_Solving_and_programming.pdf
Workbook_2_Problem_Solving_and_programming.pdf
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
 
12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptx12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptx
 
Control structures
Control structuresControl structures
Control structures
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
 
Loops in c
Loops in cLoops in c
Loops in c
 
COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops
 
12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
Loops c++
Loops c++Loops c++
Loops c++
 
Programming loop
Programming loopProgramming loop
Programming loop
 
Matlab Script - Loop Control
Matlab Script - Loop ControlMatlab Script - Loop Control
Matlab Script - Loop Control
 
Python Control structures
Python Control structuresPython Control structures
Python Control structures
 

More from hassaanciit

Circuits Lecture 5 with examples
Circuits Lecture 5 with examplesCircuits Lecture 5 with examples
Circuits Lecture 5 with examples
hassaanciit
 
Calculus - Functions Review
Calculus - Functions ReviewCalculus - Functions Review
Calculus - Functions Review
hassaanciit
 
Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04
hassaanciit
 
Introduction to Computer and Programming - Lecture 03
Introduction to Computer and Programming - Lecture 03Introduction to Computer and Programming - Lecture 03
Introduction to Computer and Programming - Lecture 03
hassaanciit
 
Ex 1 3_fsc_part1
Ex 1 3_fsc_part1Ex 1 3_fsc_part1
Ex 1 3_fsc_part1
hassaanciit
 
Islamic Studies - Fundamental beliefs
Islamic Studies - Fundamental beliefsIslamic Studies - Fundamental beliefs
Islamic Studies - Fundamental beliefs
hassaanciit
 
Islamic Studies - Concepts About Religion
Islamic Studies - Concepts About ReligionIslamic Studies - Concepts About Religion
Islamic Studies - Concepts About Religion
hassaanciit
 
Introduction to Computer and Programming - Lecture 02
Introduction to Computer and Programming - Lecture 02Introduction to Computer and Programming - Lecture 02
Introduction to Computer and Programming - Lecture 02
hassaanciit
 

More from hassaanciit (9)

Circuits Lecture 5 with examples
Circuits Lecture 5 with examplesCircuits Lecture 5 with examples
Circuits Lecture 5 with examples
 
Calculus - Functions Review
Calculus - Functions ReviewCalculus - Functions Review
Calculus - Functions Review
 
Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04
 
Introduction to Computer and Programming - Lecture 03
Introduction to Computer and Programming - Lecture 03Introduction to Computer and Programming - Lecture 03
Introduction to Computer and Programming - Lecture 03
 
Ex 1 3_fsc_part1
Ex 1 3_fsc_part1Ex 1 3_fsc_part1
Ex 1 3_fsc_part1
 
Islamic Studies - Fundamental beliefs
Islamic Studies - Fundamental beliefsIslamic Studies - Fundamental beliefs
Islamic Studies - Fundamental beliefs
 
Islamic Studies - Concepts About Religion
Islamic Studies - Concepts About ReligionIslamic Studies - Concepts About Religion
Islamic Studies - Concepts About Religion
 
Introduction to Computer and Programming - Lecture 02
Introduction to Computer and Programming - Lecture 02Introduction to Computer and Programming - Lecture 02
Introduction to Computer and Programming - Lecture 02
 
Introduction to Computer and Programming - Lecture 01
Introduction to Computer and Programming - Lecture 01Introduction to Computer and Programming - Lecture 01
Introduction to Computer and Programming - Lecture 01
 

Recently uploaded

Recently uploaded (20)

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 

ICP - Lecture 9

  • 1. CSC 103 Lecture 9 Introduction to Computers and Programming
  • 2. Loop Control Instructions  When an activity needs to be performed more than once, then the mechanism that meets this need is called Loop  Loop is defined as to perform set of instructions repeatedly  Three major loop structures in C.  The for loop  The while loop  the do-while loop (also called cousin of while loop) 2
  • 3. The for Loop  When an activity needs to be performed a fixed number of times then The for loop is used in such cases.  For example to calculate the paychecks for 120 employees or printout the squares of all the numbers from 1 to 50 etc.  In all such cases and other related the for loop is best suited 3
  • 4. General form of for loop Statement 4 for ( initialize counter ; test counter ; increment counter ) { do this ; and this ; and this ; }
  • 5. for loop Flow Chart Structure 5
  • 6. Some valid for loop expressions 6  for ( i = 10 ; i ; i -- ) printf ( " %d", i ) ;  for ( j=0 ; j < 5 ; j++ ) printf ( " %d", j ) ;  for ( i = 1; i <=10 ; printf ("%d",i++ ) )  for ( scanf ( "%d", &i ) ; i <= 10 ; i++ ) printf ( "%d", i ) ;
  • 7. The for Loop 7 main() { int count; for (count=0; count<10; count++) printf ("count=%dn", count); } Output: count=0 count=1 count=2 count=3 count=4 count=5 count=6 count=7 count=8 count=9
  • 8. Structure of the for Loop for (count=0; count<10; count++)  Parentheses following keyword for contain what we will call the “loop expression”  Loop expression is divided by semicolons into the following three separate expressions:  the “initialize expression” i.e count=0  the “test expression” i.e count<10  the “increment expression” i.e count++  The count variable has a key role, it is used to control the operations of the loop 8
  • 9. Structure of the for Loop 9 the “initialize expression”  count=0, initialize the count variable  It is executed as soon as loop is entered  It can start from any given number  However, loops often start at 1
  • 10. Structure of the for Loop 10 the “test expression”  count<10, tests each time to see the count value  It makes use of relational operator (in this case <)  The loop will be executed till the test expression becomes false  When it become false the loop will be terminated  and control will pass to next statement following loop
  • 11. Structure of the for Loop the “increment expression”  count++ (same as count=count+1), it increments the variable count each time loop is executed  It make use of increment operator i.e. ++  It should be noted that it will not only be incremented but can also be decremented as well. 11
  • 12. The Body of the for Loop  Following the keyword for and the loop expression (as discussed above) is the body of the loop, i.e. the statement (or statements) that will be executed each time round the loop. i.e. in our example, only one statement: printf ("count=%dn", count);  Note that in a for loop don’t place semicolon between loop expression & body of loop, since the keyword for & loop expression don’t constitute a complete C statement For Example: for (count=0; count<10; count++) ; 12
  • 13. Operations of the for Loop  1st initialization expression executed then  2nd test condition examined  If False, the body of the loop will not be executed  If True, the body of the loop be executed  3rd increment expression executed  Note that printf will be executed before increment so the 1st value will be printed “0”  Process will continue till test expression become false. 13
  • 14. Multiple statement in the for Loop  In our previous example only one statement is used in the body of the loop i.e printf ("count=%dn", count);  However, two or more than two statements can also be used in a loop 14
  • 15. Multiple statement in the for Loop 15 main() { int count, total; for (count=0, total=0; count<10; count++) { total = total + count; printf ("count=%d, total=%dn", count, total); } } Output: count=0, total=0 count=1, total=1 count=2, total=3 count=3, total=6 count=4, total=10 count=5, total=15 count=6, total=21 count=7, total=28 count=8, total=36 count=9, total=45
  • 16. Multiple statement in the for Loop  Two points to remember:  The whole package i.e. the opening brace, the statements, and closing brace, is a single C statement. Often called “Compound Statement” or “block”  Each statement within the block is also a C statement and must be terminated with a semicolon as in the usual way  We can also use total += count; it is same as total = total + count; 16
  • 17. Multiple Initialization in for Loop 17  In for loop, we have initialized multiple variables  These variables are separated by comma “,” count=0 and total=0 for (count=0, total=0; count<10; count++)  in this example we really didn’t need to initialize total within the loop, we could also have done: total=0; for (count=0; count<10; count++)  we can also increment two or more variables at the same time  similarly, we can have multiple conditions for (i=10,j=0; i>0,j<10; i--,j++)
  • 18. Exercise 18  Input a number from user and print its table  Program output should look like: Enter a number: 7 7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 7 x 10 = 70
  • 19. 19 main() { int a; printf("Enter a number: "); scanf("%d", &a); int i; for (i=1; i<=10; i++) { printf ("%d x %d t= %dn", a, i, a*i); } }
  • 20. Change Exercise a bit 20  Input a number from user to print its table. Also, ask user to enter how many times you want to generate the table.  Program output should look like: Enter a number: 2 Enter number of times: 5 2 X 1 = 2 2 X 2 = 4 2 X 3 = 6 2 X 4 = 8 2 X 5 = 10