SlideShare a Scribd company logo
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

PPTX
Lecture 4
DOC
Slide07 repetitions
PPTX
Loop control structure
PPTX
Nested loops
PPT
C++ control loops
PDF
Chapter 3
PPTX
Looping statements in C
PPTX
Synapse india dotnet development overloading operater part 3
Lecture 4
Slide07 repetitions
Loop control structure
Nested loops
C++ control loops
Chapter 3
Looping statements in C
Synapse india dotnet development overloading operater part 3

What's hot (20)

PPT
Java Programming: Loops
PPTX
For Loops and Nesting in Python
PPTX
Loop c++
PPT
For Loop
DOCX
Looping statements
PPTX
for loop in java
PDF
5 c control statements looping
PPTX
Looping Statement And Flow Chart
PPT
Java Programmin: Selections
PPTX
130707833146508191
PPT
Looping in C
PPT
Conditional Loops Python
DOCX
Dam31303 dti2143 lab sheet 7
DOCX
Programming Fundamentals lecture 8
PPTX
Loops in c
ODP
Semaphore
ODP
Semaphore
PDF
C++ control structure
PPTX
C Language - Switch and For Loop
Java Programming: Loops
For Loops and Nesting in Python
Loop c++
For Loop
Looping statements
for loop in java
5 c control statements looping
Looping Statement And Flow Chart
Java Programmin: Selections
130707833146508191
Looping in C
Conditional Loops Python
Dam31303 dti2143 lab sheet 7
Programming Fundamentals lecture 8
Loops in c
Semaphore
Semaphore
C++ control structure
C Language - Switch and For Loop
Ad

Viewers also liked (20)

PDF
Sharia
PPT
Islamic Studies - Lecture#1 (Religion)
PPTX
What is Sharia Law?
PDF
Business analyst job description
PPTX
Ebc10 e ch13-instructor ppt-final
PPTX
Ebc10 e ch14-instructor ppt-final
PPTX
Week 06 power_point-acct_101_8w_online
PPTX
Ebc10e ch04-instructor ppt-final
PPTX
Ebc10e ch07-instructor ppt-final
PPTX
Ebc10e ch02-instructor ppt-final
PPTX
Ebc10 e ch12-instructor ppt - final
PPTX
Ebc10e ch08-instructor ppt-final
PPTX
Ebc10e ch06-instructor ppt-final (2)
PPTX
Islam s6 shari'a law and dhimmi
PPT
Islamic Studies - Course Outline
PPTX
Ebc10e ch03-instructor ppt-final
PDF
Introduction to Computer and Programing - Lab2
PPT
Sharia
Islamic Studies - Lecture#1 (Religion)
What is Sharia Law?
Business analyst job description
Ebc10 e ch13-instructor ppt-final
Ebc10 e ch14-instructor ppt-final
Week 06 power_point-acct_101_8w_online
Ebc10e ch04-instructor ppt-final
Ebc10e ch07-instructor ppt-final
Ebc10e ch02-instructor ppt-final
Ebc10 e ch12-instructor ppt - final
Ebc10e ch08-instructor ppt-final
Ebc10e ch06-instructor ppt-final (2)
Islam s6 shari'a law and dhimmi
Islamic Studies - Course Outline
Ebc10e ch03-instructor ppt-final
Introduction to Computer and Programing - Lab2
Ad

Similar to ICP - Lecture 9 (20)

PDF
LOOPS TOPIC FOR CLASS XI PYTHON SYLLABUS
PDF
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
PPTX
Lec7 - Loops updated.pptx
PDF
Workbook_2_Problem_Solving_and_programming.pdf
PPTX
Loops in c language
PPTX
Loops in c language
PDF
[ITP - Lecture 11] Loops in C/C++
PDF
Programming Fundamentals presentation slide
PPT
Java căn bản - Chapter6
PPTX
12-Lec - Repetition For Loop.pptx
PDF
Lecture10(Repetition-Part 1) computers.pdf
DOC
Control structures
PDF
Fundamental of Information Technology - UNIT 8
PPTX
Loops in c
PPTX
COM1407: Program Control Structures – Repetition and Loops
PPT
Python Programming Introduction - Loops & Boolean
PPT
12 lec 12 loop
PPTX
Lecture 3
PDF
Loop and while Loop
PDF
04-Looping( For , while and do while looping) .pdf
LOOPS TOPIC FOR CLASS XI PYTHON SYLLABUS
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Lec7 - Loops updated.pptx
Workbook_2_Problem_Solving_and_programming.pdf
Loops in c language
Loops in c language
[ITP - Lecture 11] Loops in C/C++
Programming Fundamentals presentation slide
Java căn bản - Chapter6
12-Lec - Repetition For Loop.pptx
Lecture10(Repetition-Part 1) computers.pdf
Control structures
Fundamental of Information Technology - UNIT 8
Loops in c
COM1407: Program Control Structures – Repetition and Loops
Python Programming Introduction - Loops & Boolean
12 lec 12 loop
Lecture 3
Loop and while Loop
04-Looping( For , while and do while looping) .pdf

More from hassaanciit (9)

PPT
Circuits Lecture 5 with examples
PDF
Calculus - Functions Review
PDF
Introduction to Computer and Programing - Lecture 04
PDF
Introduction to Computer and Programming - Lecture 03
PDF
Ex 1 3_fsc_part1
DOC
Islamic Studies - Fundamental beliefs
DOC
Islamic Studies - Concepts About Religion
PDF
Introduction to Computer and Programming - Lecture 02
PDF
Introduction to Computer and Programming - Lecture 01
Circuits Lecture 5 with examples
Calculus - Functions Review
Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programming - Lecture 03
Ex 1 3_fsc_part1
Islamic Studies - Fundamental beliefs
Islamic Studies - Concepts About Religion
Introduction to Computer and Programming - Lecture 02
Introduction to Computer and Programming - Lecture 01

Recently uploaded (20)

PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Approach and Philosophy of On baking technology
PDF
NewMind AI Weekly Chronicles - August'25 Week I
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Spectroscopy.pptx food analysis technology
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
cuic standard and advanced reporting.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
MYSQL Presentation for SQL database connectivity
Reach Out and Touch Someone: Haptics and Empathic Computing
Encapsulation_ Review paper, used for researhc scholars
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Approach and Philosophy of On baking technology
NewMind AI Weekly Chronicles - August'25 Week I
The AUB Centre for AI in Media Proposal.docx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Spectroscopy.pptx food analysis technology
sap open course for s4hana steps from ECC to s4
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Per capita expenditure prediction using model stacking based on satellite ima...
20250228 LYD VKU AI Blended-Learning.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
“AI and Expert System Decision Support & Business Intelligence Systems”
cuic standard and advanced reporting.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