SlideShare a Scribd company logo
1 of 15
Download to read offline
Chapter 4: Control Structure –
Repetitions/Loops
Objectives
State the types of repetitions
Differentiate among three types of
repetitions
Build a simple program to solve a problem
using repetitions

pretest Repetitions structure
 The expression (syarat) is evaluated, before the
loop body is executed.
 If the result of evaluation is true. the loop body is
executed. Following this, control goes back to
the expression and is evaluated again. This
continues until the expression computes to a
value of false.
 If the expression produce of false, the loop body
is bypassed and the program control drops
down to the statement.
Posttest repetition statement
First executes the loop body and then
computes the expression (syarat).
If the expression is true, the loop body is
executed again; otherwise the repetition
terminates.
Repetitions with counter value (nilai
pembilang)
Nilai Pembilang Hasil dari pelaksanaan arahan nilai pembilang
i++ Nilai pembilang iaitu i ditambah dengan satu
i-- Nilai pembilang i dikurangkan dengan satu
i+=2 Nilai pembilang i ditambah dengan 2
i*=2 Nilai pembilang i didarab dengan 2
While loop
 #include<iostream.h>
 main()
 {
 int i=0;
 while ( i <= 5 )
 {
 cout<<"n Welcome to while statement";
 i++;
 }
 }
do – while loop
 #include<iostream.h>
 main()
 {
 int i=0;
 do {
 cout<<"n Welcome to while statement";
 i++;
 } while ( i <=5);
 }
For loop
#include<iostream.h>
main()
{
 int i;
 for (i=0; i<=5; i++)
 cout<<"n Welcome to while statement";
}
For loops with counter
 Sintaksis:
 for (pembolehubah = nilai_awalan; syarat;
pembilang)
 Blok arahan
 { senarai penyataan;
 :
 :

 }
Loops with sentinel value
we don’t know the number of loops will
execute.
For example, we don’t know how many
items bought by customer.
Refer to Rajah 4.8 page# 62
Nested Loop
In a nested loop, one loop referred to as
the inner loop and one is called the outer
loop.

More Related Content

What's hot

Comp 220 ilab 6 of 7
Comp 220 ilab 6 of 7Comp 220 ilab 6 of 7
Comp 220 ilab 6 of 7ashhadiqbal
 
Nassi Shneiderman Diagrams: Algorithms Made Easier (at least a little bit)
Nassi Shneiderman Diagrams: Algorithms Made Easier (at least a little bit)Nassi Shneiderman Diagrams: Algorithms Made Easier (at least a little bit)
Nassi Shneiderman Diagrams: Algorithms Made Easier (at least a little bit)Richard Schwarz
 
Demystifying Eta Expansion
Demystifying Eta ExpansionDemystifying Eta Expansion
Demystifying Eta ExpansionKnoldus Inc.
 
Devry ecet 370 week 3 ilab the stack and the queue ad ts new
Devry ecet 370 week 3 ilab the stack and the queue ad ts newDevry ecet 370 week 3 ilab the stack and the queue ad ts new
Devry ecet 370 week 3 ilab the stack and the queue ad ts newnikig6806
 
Handout # 4 functions + scopes
Handout # 4   functions + scopes Handout # 4   functions + scopes
Handout # 4 functions + scopes NUST Stuff
 
Algorithm design and problem solving
Algorithm design and problem solvingAlgorithm design and problem solving
Algorithm design and problem solvingGwatso
 
Introduction to programming using mat ab
Introduction to programming using mat abIntroduction to programming using mat ab
Introduction to programming using mat abAhmed Hisham
 
Steps for Developing a 'C' program
 Steps for Developing a 'C' program Steps for Developing a 'C' program
Steps for Developing a 'C' programSahithi Naraparaju
 
Comp 220 ilab 5 of 7
Comp 220 ilab 5 of 7Comp 220 ilab 5 of 7
Comp 220 ilab 5 of 7ashhadiqbal
 
Programming Sessions KU Leuven - Session 02
Programming Sessions KU Leuven - Session 02Programming Sessions KU Leuven - Session 02
Programming Sessions KU Leuven - Session 02Rafael Camacho Dejay
 
FIT-Unit3 chapter 1 -computer program
FIT-Unit3 chapter 1 -computer programFIT-Unit3 chapter 1 -computer program
FIT-Unit3 chapter 1 -computer programraksharao
 
Flowcharts and pseudocodes
Flowcharts and pseudocodesFlowcharts and pseudocodes
Flowcharts and pseudocodesDr Piyush Charan
 

What's hot (16)

Comp 220 ilab 6 of 7
Comp 220 ilab 6 of 7Comp 220 ilab 6 of 7
Comp 220 ilab 6 of 7
 
Nassi Shneiderman Diagrams: Algorithms Made Easier (at least a little bit)
Nassi Shneiderman Diagrams: Algorithms Made Easier (at least a little bit)Nassi Shneiderman Diagrams: Algorithms Made Easier (at least a little bit)
Nassi Shneiderman Diagrams: Algorithms Made Easier (at least a little bit)
 
Demystifying Eta Expansion
Demystifying Eta ExpansionDemystifying Eta Expansion
Demystifying Eta Expansion
 
Devry ecet 370 week 3 ilab the stack and the queue ad ts new
Devry ecet 370 week 3 ilab the stack and the queue ad ts newDevry ecet 370 week 3 ilab the stack and the queue ad ts new
Devry ecet 370 week 3 ilab the stack and the queue ad ts new
 
Handout # 4 functions + scopes
Handout # 4   functions + scopes Handout # 4   functions + scopes
Handout # 4 functions + scopes
 
Algorithm design and problem solving
Algorithm design and problem solvingAlgorithm design and problem solving
Algorithm design and problem solving
 
selection structures
selection structuresselection structures
selection structures
 
Introduction to programming using mat ab
Introduction to programming using mat abIntroduction to programming using mat ab
Introduction to programming using mat ab
 
Steps for Developing a 'C' program
 Steps for Developing a 'C' program Steps for Developing a 'C' program
Steps for Developing a 'C' program
 
Comp 220 ilab 5 of 7
Comp 220 ilab 5 of 7Comp 220 ilab 5 of 7
Comp 220 ilab 5 of 7
 
Programming Sessions KU Leuven - Session 02
Programming Sessions KU Leuven - Session 02Programming Sessions KU Leuven - Session 02
Programming Sessions KU Leuven - Session 02
 
FIT-Unit3 chapter 1 -computer program
FIT-Unit3 chapter 1 -computer programFIT-Unit3 chapter 1 -computer program
FIT-Unit3 chapter 1 -computer program
 
TDD in Powershell
TDD in PowershellTDD in Powershell
TDD in Powershell
 
Operators and it's type
Operators and it's type Operators and it's type
Operators and it's type
 
03b loops
03b   loops03b   loops
03b loops
 
Flowcharts and pseudocodes
Flowcharts and pseudocodesFlowcharts and pseudocodes
Flowcharts and pseudocodes
 

Similar to Looping

Similar to Looping (20)

Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
 
UNIT 2 PPT.pdf
UNIT 2 PPT.pdfUNIT 2 PPT.pdf
UNIT 2 PPT.pdf
 
07 flow control
07   flow control07   flow control
07 flow control
 
itretion.docx
itretion.docxitretion.docx
itretion.docx
 
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
 
Intro To C++ - Class 10 - Control Statements: Part 2
Intro To C++ - Class 10 - Control Statements: Part 2Intro To C++ - Class 10 - Control Statements: Part 2
Intro To C++ - Class 10 - Control Statements: Part 2
 
C++ chapter 4
C++ chapter 4C++ chapter 4
C++ chapter 4
 
Intro To C++ - Class 09 - Control Statements: Part 1
Intro To C++ - Class 09 - Control Statements: Part 1Intro To C++ - Class 09 - Control Statements: Part 1
Intro To C++ - Class 09 - Control Statements: Part 1
 
Loops
LoopsLoops
Loops
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
 
Looping statements
Looping statementsLooping statements
Looping statements
 
control statements of clangauge (ii unit)
control statements of clangauge (ii unit)control statements of clangauge (ii unit)
control statements of clangauge (ii unit)
 
Decision making and looping
Decision making and loopingDecision making and looping
Decision making and looping
 
[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++
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
 
Loops in C.pptx
Loops in C.pptxLoops in C.pptx
Loops in C.pptx
 
control structure
control structurecontrol structure
control structure
 

More from Unit Kediaman Luar Kampus (9)

Fungsi (i)
Fungsi (i)Fungsi (i)
Fungsi (i)
 
Fail
FailFail
Fail
 
Fungsi (ii)
Fungsi (ii)Fungsi (ii)
Fungsi (ii)
 
Basic pengaturcaraan
Basic pengaturcaraanBasic pengaturcaraan
Basic pengaturcaraan
 
Latihan 2
Latihan 2Latihan 2
Latihan 2
 
Pengaturcaraan asas
Pengaturcaraan asasPengaturcaraan asas
Pengaturcaraan asas
 
Penyelesaian masalah
Penyelesaian masalahPenyelesaian masalah
Penyelesaian masalah
 
Pengenalan kepada pengaturcaraan berstruktur
Pengenalan kepada pengaturcaraan berstrukturPengenalan kepada pengaturcaraan berstruktur
Pengenalan kepada pengaturcaraan berstruktur
 
Presentation2
Presentation2Presentation2
Presentation2
 

Recently uploaded

Landscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfLandscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfAarwolf Industries LLC
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...itnewsafrica
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Mark Simos
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Karmanjay Verma
 
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...Karmanjay Verma
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsYoss Cohen
 
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Jeffrey Haguewood
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxAna-Maria Mihalceanu
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024TopCSSGallery
 
All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFMichael Gough
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 

Recently uploaded (20)

Landscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdfLandscape Catalogue 2024 Australia-1.pdf
Landscape Catalogue 2024 Australia-1.pdf
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
Tampa BSides - The No BS SOC (slides from April 6, 2024 talk)
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#
 
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platforms
 
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance Toolbox
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024
 
All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDF
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 

Looping

  • 1. Chapter 4: Control Structure – Repetitions/Loops
  • 2. Objectives State the types of repetitions Differentiate among three types of repetitions Build a simple program to solve a problem using repetitions 
  • 3. pretest Repetitions structure  The expression (syarat) is evaluated, before the loop body is executed.  If the result of evaluation is true. the loop body is executed. Following this, control goes back to the expression and is evaluated again. This continues until the expression computes to a value of false.  If the expression produce of false, the loop body is bypassed and the program control drops down to the statement.
  • 4.
  • 5.
  • 6. Posttest repetition statement First executes the loop body and then computes the expression (syarat). If the expression is true, the loop body is executed again; otherwise the repetition terminates.
  • 7.
  • 8.
  • 9. Repetitions with counter value (nilai pembilang) Nilai Pembilang Hasil dari pelaksanaan arahan nilai pembilang i++ Nilai pembilang iaitu i ditambah dengan satu i-- Nilai pembilang i dikurangkan dengan satu i+=2 Nilai pembilang i ditambah dengan 2 i*=2 Nilai pembilang i didarab dengan 2
  • 10. While loop  #include<iostream.h>  main()  {  int i=0;  while ( i <= 5 )  {  cout<<"n Welcome to while statement";  i++;  }  }
  • 11. do – while loop  #include<iostream.h>  main()  {  int i=0;  do {  cout<<"n Welcome to while statement";  i++;  } while ( i <=5);  }
  • 12. For loop #include<iostream.h> main() {  int i;  for (i=0; i<=5; i++)  cout<<"n Welcome to while statement"; }
  • 13. For loops with counter  Sintaksis:  for (pembolehubah = nilai_awalan; syarat; pembilang)  Blok arahan  { senarai penyataan;  :  :   }
  • 14. Loops with sentinel value we don’t know the number of loops will execute. For example, we don’t know how many items bought by customer. Refer to Rajah 4.8 page# 62
  • 15. Nested Loop In a nested loop, one loop referred to as the inner loop and one is called the outer loop.