SlideShare a Scribd company logo
Copyright @ IT Series www.itseries.com.pk
CHAPTER 12: Loop Constructs
COMPUTER SCIENCE
12
(MS Access and C)
Copyright @ IT Series www.itseries.com.pk
• Loop
• while Loop
• Infinite Loop
• do-while Loop
• Difference between while and do-while Loop
• for Loop
• Sentinel Controlled Loop
• continue Statement
• break Statement
• Nested Loops
• goto Statement
Topics
Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
 The control structure that executes a statement or set of statements repeatedly is called loop.
 Loops are also known as iteration or repetition structure
 Loops are basically used for two purposes:
 They are used to execute a statement or number of statements for a specified number of times
 Example: A user may display his name on screen for 10 times
 The loops are also used to get a sequence of values
 Example: A user may display a set of natural numbers from 1 to 10
 There are three types of loops available in C:
 while loop
 do-while loop
 for loop
Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
 while loop is the simplest loop of C
 It executes one or more statements while the given condition remains true
 It is useful when the number of iterations is not known in advance
Syntax
 Condition is evaluated:
 if it is true, the statement(s) are executed, and then condition is evaluated again
 if it is false, the loop is exited
An iteration is
an execution of
the loop body.
while is a
keyword. It is
always written in
lowercase.
Loop body
Don’t put ; after (condition)
while is a pretest
loop (the condition
is evaluated before
the loop executes)
Flowchart
Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
while Loop Example
int n;
n = 1;
while(n<=5)
{
printf(“Pakistann");
n++;
}
 Produces output:
Counter Variable
 A variable that is incremented or decremented each time a loop iterates
 It can be used to control the execution of the loop (as a loop control variable)
 It must be initialized before entering loop
 It may be incremented/decremented either inside the loop or in the loop test
n++; is the same as n= n + 1;
Don’t put ; after (condition)
Loop body
while is a pretest
loop (the condition
is evaluated before
the loop executes)
Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
while Loop Programs
Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
while Loop Programs (cont.)
Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
Infinite loop
 The loop must contain code to allow the condition to eventually become false so the loop can be exited
 Otherwise, you have an infinite loop
Example
int n;
n = 1;
while(n<10)
printf("%d t",n);
Infinite loop because n is always < 10
A loop that does not
stop is known as an
infinite loop
Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
 This loop executes one or more statements while the given condition is true
 In this loop, the condition comes after the body of loop
 The loop body always executes at least once
Syntax:
 Execution continues as long as the condition is true; the loop is exited when the condition becomes false
 ; after (condition) is also required
 The loop executes at least once even if the condition is false in the beginning
An error occurs if the semicolon
is not used at the end of the
condition.
Loop body
do-while is a post
test loop (condition
is evaluated after
the loop executes)
Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
do-while Loop Example
int n;
n = 1;
do
{
printf(“Welcome to Cn”);
n++;
}
while(n<=5);
Loop body
do-while is a post
test loop (condition
is evaluated after
the loop executes)
Condition
Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
do-while Loop Programs
Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
do-while Loop Programs (cont.)
Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
 It is a pretest loop that executes one or more statements for a specified number of times
 This loop is also called counter-controlled loop
 It is useful with counters or if precise number of iterations is known
Syntax / General Form Example
for (initialization; condition; increment/decrement)
{
statement 1;
statement 2;


statement N;
}
Flowchart
Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
 You can define variables in initialization code
for (int n=1; n <= 5; n++)
 Can omit initialization if already done
int n = 1;
for (; n <= 5; n++)
 Can omit update if done in loop body
for (n = 1; n<= 5;)
n++;
 The condition in for loop is mandatory. It cannot be omitted
int n=1;
for (;n<= 5;)
n++;
Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
for Loop Programs
Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
for Loop Programs (cont.)
Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
Sentinel Controlled Loop
• Sentinel-controlled loop depends on special value known as sentinel value
• A sentinel value is commonly used with while and do-while loops
• The number of iterations of sentinel-controlled loop is unknown
• Depends on the input from the user
General Form
• Start the loop iteration
• Compare the data value with the sentinel value
• Process the loop body if it is different from sentinel value
• Terminate the loop if the data value is a sentinel value
Examples
• A loop that inputs the marks of students until a negative number is entered
• A loop that inputs different characters until the character 'Z' is entered
• A loop that continues to read a string until a period "." is found
• A loop that inputs a number until a 0(zero) is entered
Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
Copyright @ IT Series www.itseries.com.pk
The inner loop goes through all its iterations for each iteration of the outer loop
 The continue statement is used in the body of the loop
 When this statement is executed in the loop body, the remaining statements of current iteration are not
executed
 The control directly moves to the next iteration
Example
 The above example has two printf statements
 One statement is before the continue statement and one is after continue statement
 The second statement is never executed
 This is because each time continue statement is executed, the control moves back to the start of the body
 So “Knowledge is power” is never displayed
Copyright @ IT Series www.itseries.com.pk
The inner loop goes through all its iterations for each iteration of the outer loop
 It can be used with while, for, do-while or switch structure
 When this statement is executed in the loop body, the remaining iterations of the loop are skipped
 The control directly moves outside the body and the statement that comes after the body is executed
 When used in an inner loop, break terminates that loop only and returns to the outer loop
Example
 The counter variable x indicates that the loop should execute for five times
 But it is executed only once
 In first iteration, the printf statement in the loop displays “Questioning” and control moves to break
statement
 This statement moves the control out of the loop. So the message appears only once
break can be used to
terminate the
execution of a loop
iteration
Copyright @ IT Series www.itseries.com.pk
The inner loop goes through all its iterations for each iteration of the outer loop
 A loop within a loop is called nested loop
 Nested loops consist of an outer loop with or more inner loops
 Any loop can be used as inner loop of another loop
 e.g. while loop can be used as outer loop and for loop can be used as inner loop in nested loop
Syntax / General Form
 The inner loop goes through all its iterations for each iteration of the outer loop
 Total number of iterations for inner loop is product of number of iterations of the two loops
 In example, Outer loop executes two times and inner loop executes three times with each iteration of outer
loop
 The inner loop iterates 6 times in total
Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
Nested Loop Programs
Copyright @ IT Series www.itseries.com.pk
possible to perform mathematical operation on character values
Nested Loop Programs(cont.)
Copyright @ IT Series www.itseries.com.pk
The inner loop goes through all its iterations for each iteration of the outer loop
 Used to perform an unconditional transfer of control to a named label
General Form
goto label; Example
----------------
-----------------
label:
Statement;
 The label is an identifier.
 When the goto statement is encountered, the program control
jumps to label and starts executing the statements
 if and goto can be used together to construct loop in C
 If statement is used to give specific condition
 goto statement is used to transfer control from one location to
another
 Control is transferred again and again until given condition become false
 In this way , loop can be constructed

More Related Content

Similar to Chapter 12 Computer Science ( ICS 12).pdf

Eo gaddis java_chapter_05_5e
Eo gaddis java_chapter_05_5eEo gaddis java_chapter_05_5e
Eo gaddis java_chapter_05_5e
Gina Bullock
 
Eo gaddis java_chapter_05_5e
Eo gaddis java_chapter_05_5eEo gaddis java_chapter_05_5e
Eo gaddis java_chapter_05_5e
Gina Bullock
 
Loop in C Properties & Applications
Loop in C Properties & ApplicationsLoop in C Properties & Applications
Loop in C Properties & Applications
Emroz Sardar
 
Ch6 Loops
Ch6 LoopsCh6 Loops
Ch6 Loops
SzeChingChen
 
Control structures repetition
Control structures   repetitionControl structures   repetition
Control structures repetition
Online
 
An Introduction to Computer Science with Java, Python an.docx
An Introduction to  Computer Science  with Java, Python an.docxAn Introduction to  Computer Science  with Java, Python an.docx
An Introduction to Computer Science with Java, Python an.docx
amrit47
 
Loops c++
Loops c++Loops c++
Loops c++
Shivani Singh
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
JavvajiVenkat
 
03 conditions loops
03   conditions loops03   conditions loops
03 conditions loops
Manzoor ALam
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loopsbsdeol28
 
Chapter 3 - Flow of Control Part II.pdf
Chapter 3  - Flow of Control Part II.pdfChapter 3  - Flow of Control Part II.pdf
Chapter 3 - Flow of Control Part II.pdf
KirubelWondwoson1
 
C language (Part 2)
C language (Part 2)C language (Part 2)
C language (Part 2)
SURBHI SAROHA
 
Loops in c
Loops in cLoops in c
Loops in c
shubhampandav3
 
Nested Loops in C.pptx
Nested Loops in C.pptxNested Loops in C.pptx
Nested Loops in C.pptx
VishnupriyaKashyap
 
Decision making and looping
Decision making and loopingDecision making and looping
Decision making and looping
Hossain Md Shakhawat
 
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
Priyom Majumder
 
Repetition, Basic loop structures, Loop programming techniques
Repetition, Basic loop structures, Loop programming techniquesRepetition, Basic loop structures, Loop programming techniques
Repetition, Basic loop structures, Loop programming techniques
Jason J Pulikkottil
 
Decision making and loop in C#
Decision making and loop in C#Decision making and loop in C#
Decision making and loop in C#
Prasanna Kumar SM
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Deepak Singh
 

Similar to Chapter 12 Computer Science ( ICS 12).pdf (20)

Eo gaddis java_chapter_05_5e
Eo gaddis java_chapter_05_5eEo gaddis java_chapter_05_5e
Eo gaddis java_chapter_05_5e
 
Eo gaddis java_chapter_05_5e
Eo gaddis java_chapter_05_5eEo gaddis java_chapter_05_5e
Eo gaddis java_chapter_05_5e
 
Loop in C Properties & Applications
Loop in C Properties & ApplicationsLoop in C Properties & Applications
Loop in C Properties & Applications
 
Ch6 Loops
Ch6 LoopsCh6 Loops
Ch6 Loops
 
Control structures repetition
Control structures   repetitionControl structures   repetition
Control structures repetition
 
An Introduction to Computer Science with Java, Python an.docx
An Introduction to  Computer Science  with Java, Python an.docxAn Introduction to  Computer Science  with Java, Python an.docx
An Introduction to Computer Science with Java, Python an.docx
 
Loops c++
Loops c++Loops c++
Loops c++
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
 
03 conditions loops
03   conditions loops03   conditions loops
03 conditions loops
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
 
Chapter 3 - Flow of Control Part II.pdf
Chapter 3  - Flow of Control Part II.pdfChapter 3  - Flow of Control Part II.pdf
Chapter 3 - Flow of Control Part II.pdf
 
C language (Part 2)
C language (Part 2)C language (Part 2)
C language (Part 2)
 
85ec7 session2 c++
85ec7 session2 c++85ec7 session2 c++
85ec7 session2 c++
 
Loops in c
Loops in cLoops in c
Loops in c
 
Nested Loops in C.pptx
Nested Loops in C.pptxNested Loops in C.pptx
Nested Loops in C.pptx
 
Decision making and looping
Decision making and loopingDecision making and looping
Decision making and looping
 
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
 
Repetition, Basic loop structures, Loop programming techniques
Repetition, Basic loop structures, Loop programming techniquesRepetition, Basic loop structures, Loop programming techniques
Repetition, Basic loop structures, Loop programming techniques
 
Decision making and loop in C#
Decision making and loop in C#Decision making and loop in C#
Decision making and loop in C#
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++
 

Recently uploaded

How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptx
ssuserbdd3e8
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
Nguyen Thanh Tu Collection
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
plant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated cropsplant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated crops
parmarsneha2
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Sourabh Kumar
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
Vivekanand Anglo Vedic Academy
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 

Recently uploaded (20)

How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
NLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptxNLC-2024-Orientation-for-RO-SDO (1).pptx
NLC-2024-Orientation-for-RO-SDO (1).pptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
plant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated cropsplant breeding methods in asexually or clonally propagated crops
plant breeding methods in asexually or clonally propagated crops
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 

Chapter 12 Computer Science ( ICS 12).pdf

  • 1. Copyright @ IT Series www.itseries.com.pk CHAPTER 12: Loop Constructs COMPUTER SCIENCE 12 (MS Access and C)
  • 2. Copyright @ IT Series www.itseries.com.pk • Loop • while Loop • Infinite Loop • do-while Loop • Difference between while and do-while Loop • for Loop • Sentinel Controlled Loop • continue Statement • break Statement • Nested Loops • goto Statement Topics
  • 3. Copyright @ IT Series www.itseries.com.pk possible to perform mathematical operation on character values  The control structure that executes a statement or set of statements repeatedly is called loop.  Loops are also known as iteration or repetition structure  Loops are basically used for two purposes:  They are used to execute a statement or number of statements for a specified number of times  Example: A user may display his name on screen for 10 times  The loops are also used to get a sequence of values  Example: A user may display a set of natural numbers from 1 to 10  There are three types of loops available in C:  while loop  do-while loop  for loop
  • 4. Copyright @ IT Series www.itseries.com.pk possible to perform mathematical operation on character values  while loop is the simplest loop of C  It executes one or more statements while the given condition remains true  It is useful when the number of iterations is not known in advance Syntax  Condition is evaluated:  if it is true, the statement(s) are executed, and then condition is evaluated again  if it is false, the loop is exited An iteration is an execution of the loop body. while is a keyword. It is always written in lowercase. Loop body Don’t put ; after (condition) while is a pretest loop (the condition is evaluated before the loop executes) Flowchart
  • 5. Copyright @ IT Series www.itseries.com.pk possible to perform mathematical operation on character values while Loop Example int n; n = 1; while(n<=5) { printf(“Pakistann"); n++; }  Produces output: Counter Variable  A variable that is incremented or decremented each time a loop iterates  It can be used to control the execution of the loop (as a loop control variable)  It must be initialized before entering loop  It may be incremented/decremented either inside the loop or in the loop test n++; is the same as n= n + 1; Don’t put ; after (condition) Loop body while is a pretest loop (the condition is evaluated before the loop executes)
  • 6. Copyright @ IT Series www.itseries.com.pk possible to perform mathematical operation on character values while Loop Programs
  • 7. Copyright @ IT Series www.itseries.com.pk possible to perform mathematical operation on character values while Loop Programs (cont.)
  • 8. Copyright @ IT Series www.itseries.com.pk possible to perform mathematical operation on character values Infinite loop  The loop must contain code to allow the condition to eventually become false so the loop can be exited  Otherwise, you have an infinite loop Example int n; n = 1; while(n<10) printf("%d t",n); Infinite loop because n is always < 10 A loop that does not stop is known as an infinite loop
  • 9. Copyright @ IT Series www.itseries.com.pk possible to perform mathematical operation on character values  This loop executes one or more statements while the given condition is true  In this loop, the condition comes after the body of loop  The loop body always executes at least once Syntax:  Execution continues as long as the condition is true; the loop is exited when the condition becomes false  ; after (condition) is also required  The loop executes at least once even if the condition is false in the beginning An error occurs if the semicolon is not used at the end of the condition. Loop body do-while is a post test loop (condition is evaluated after the loop executes)
  • 10. Copyright @ IT Series www.itseries.com.pk possible to perform mathematical operation on character values do-while Loop Example int n; n = 1; do { printf(“Welcome to Cn”); n++; } while(n<=5); Loop body do-while is a post test loop (condition is evaluated after the loop executes) Condition
  • 11. Copyright @ IT Series www.itseries.com.pk possible to perform mathematical operation on character values do-while Loop Programs
  • 12. Copyright @ IT Series www.itseries.com.pk possible to perform mathematical operation on character values do-while Loop Programs (cont.)
  • 13. Copyright @ IT Series www.itseries.com.pk possible to perform mathematical operation on character values
  • 14. Copyright @ IT Series www.itseries.com.pk possible to perform mathematical operation on character values  It is a pretest loop that executes one or more statements for a specified number of times  This loop is also called counter-controlled loop  It is useful with counters or if precise number of iterations is known Syntax / General Form Example for (initialization; condition; increment/decrement) { statement 1; statement 2;   statement N; } Flowchart
  • 15. Copyright @ IT Series www.itseries.com.pk possible to perform mathematical operation on character values  You can define variables in initialization code for (int n=1; n <= 5; n++)  Can omit initialization if already done int n = 1; for (; n <= 5; n++)  Can omit update if done in loop body for (n = 1; n<= 5;) n++;  The condition in for loop is mandatory. It cannot be omitted int n=1; for (;n<= 5;) n++;
  • 16. Copyright @ IT Series www.itseries.com.pk possible to perform mathematical operation on character values for Loop Programs
  • 17. Copyright @ IT Series www.itseries.com.pk possible to perform mathematical operation on character values for Loop Programs (cont.)
  • 18. Copyright @ IT Series www.itseries.com.pk possible to perform mathematical operation on character values Sentinel Controlled Loop • Sentinel-controlled loop depends on special value known as sentinel value • A sentinel value is commonly used with while and do-while loops • The number of iterations of sentinel-controlled loop is unknown • Depends on the input from the user General Form • Start the loop iteration • Compare the data value with the sentinel value • Process the loop body if it is different from sentinel value • Terminate the loop if the data value is a sentinel value Examples • A loop that inputs the marks of students until a negative number is entered • A loop that inputs different characters until the character 'Z' is entered • A loop that continues to read a string until a period "." is found • A loop that inputs a number until a 0(zero) is entered
  • 19. Copyright @ IT Series www.itseries.com.pk possible to perform mathematical operation on character values
  • 20. Copyright @ IT Series www.itseries.com.pk The inner loop goes through all its iterations for each iteration of the outer loop  The continue statement is used in the body of the loop  When this statement is executed in the loop body, the remaining statements of current iteration are not executed  The control directly moves to the next iteration Example  The above example has two printf statements  One statement is before the continue statement and one is after continue statement  The second statement is never executed  This is because each time continue statement is executed, the control moves back to the start of the body  So “Knowledge is power” is never displayed
  • 21. Copyright @ IT Series www.itseries.com.pk The inner loop goes through all its iterations for each iteration of the outer loop  It can be used with while, for, do-while or switch structure  When this statement is executed in the loop body, the remaining iterations of the loop are skipped  The control directly moves outside the body and the statement that comes after the body is executed  When used in an inner loop, break terminates that loop only and returns to the outer loop Example  The counter variable x indicates that the loop should execute for five times  But it is executed only once  In first iteration, the printf statement in the loop displays “Questioning” and control moves to break statement  This statement moves the control out of the loop. So the message appears only once break can be used to terminate the execution of a loop iteration
  • 22. Copyright @ IT Series www.itseries.com.pk The inner loop goes through all its iterations for each iteration of the outer loop  A loop within a loop is called nested loop  Nested loops consist of an outer loop with or more inner loops  Any loop can be used as inner loop of another loop  e.g. while loop can be used as outer loop and for loop can be used as inner loop in nested loop Syntax / General Form  The inner loop goes through all its iterations for each iteration of the outer loop  Total number of iterations for inner loop is product of number of iterations of the two loops  In example, Outer loop executes two times and inner loop executes three times with each iteration of outer loop  The inner loop iterates 6 times in total
  • 23. Copyright @ IT Series www.itseries.com.pk possible to perform mathematical operation on character values Nested Loop Programs
  • 24. Copyright @ IT Series www.itseries.com.pk possible to perform mathematical operation on character values Nested Loop Programs(cont.)
  • 25. Copyright @ IT Series www.itseries.com.pk The inner loop goes through all its iterations for each iteration of the outer loop  Used to perform an unconditional transfer of control to a named label General Form goto label; Example ---------------- ----------------- label: Statement;  The label is an identifier.  When the goto statement is encountered, the program control jumps to label and starts executing the statements  if and goto can be used together to construct loop in C  If statement is used to give specific condition  goto statement is used to transfer control from one location to another  Control is transferred again and again until given condition become false  In this way , loop can be constructed