SlideShare una empresa de Scribd logo
1 de 25
C H AP T E R 7
L O O P S
PROG0101
FUNDAMENTALS OF
PROGRAMMING
C H AP T E R 6
B R AN C H I N G
PROG0101
FUNDAMENTALS OF PROGRAMMING
Chapter 5
Operators
PROG0101
FUNDAMENTALS OF PROGRAMMING
CHAPTER 4
VARIABLES AND DATA
TYPES
PROG0101
FUNDAMENTALS OF PROGRAMMING
CHAPTER 3
ALGORITHMS
PROG0101
FUNDAMENTALS OF PROGRAMMING
C H A P T E R 1
O V E R V I E W O F C O M P U T E R S A N D L O G I C
PROG0101
FUNDAMENTALS OF
PROGRAMMING
PROG0101
FUNDAMENTALS OF PROGRAMMING
CHAPTER 7
LOOP STATEMENTS
2
LOOP STATEMENTS
 A loop statement is a sequence of instructions that is
continually repeated until a certain condition is reached.
 Loops allow for the same statement to be executed a
number of times in succession.
3
LOOP STATEMENTS
Loops
Pseudocode:
Loop
Do Something
Until Condition
Flowchart:
Conditio
n
Do something
False
True
4
LOOP STATEMENTS
 There are three types which are common to most
programming languages:
 Condition Tested Loops
 Counted Loops
 Endless Loops
5
CONDITION TESTED LOOP
 A condition tested loop is one which repeats a set of
instructions until a certain condition is reached.
 The test can be performed at the start of the loop (before
any of the instructions are executed), during the loop, or at
the end of the loop.
 Usually, the condition will be testing the result of executing
the statements that are inside the loop.
6
COUNTED LOOP
 A counted loop is one which allows the programmer to
instruct the computer to perform a set of instructions x
times, where x is usually an integer value, but some
programming languages offer other data types.
 One could argue that the counted loop is just a condition
tested loop which updates a counter and exits once a given
value is reached.
7
COUNTED LOOP
 The only time to use a count loop is when the program can
determine ahead of time how many times the loop will
repeat.
 There are generally two ways that the number of repetitions
of a loop will be know ahead of time:
 The loop always repeats the same number of times.
 The program calculates the number of repetitions based
upon user input.
8
ENDLESS LOOP
 An endless loop goes round and round until one of three
things happens:
 The computer is turned off (or the application stopped,
forcefully)
 The computer encounters an EXIT (or similar) statement
 An error forces the application to 'crash'
 Some endless loops serve a purpose, in message loops,
for example, where it is necessary to continually monitor for
incoming messages from the operating system.
9
EXAMPLE OF LOOP STATEMENTS
 These are examples loop statement in programming
language
 FOR Loop
 WHILE Loop
 DO … WHILE Loop
10
FOR LOOP STATEMENT
 A FOR loop is a loop that repeats a specified number of
times.
 The loop uses a counter to tell it how many times to run the
same sequence of activities.
11
FOR LOOP STATEMENT
 The counter has the following three numeric values:
 Initial counter value
 Increment (the amount to add to the counter each time
the loop runs)
 Final counter value
 The loop ends when the counter reaches the final counter
value, or, if there is an associated test condition, when the
test condition is true.
12
Flowchart:
FOR LOOP STATEMENT
Condition
(Final
counter)
Do something
Initial value
Increment
Pseudocode:
FOR x times
Do Something
Increment
13
FOR LOOP STATEMENT
FOR loop syntax:
for (initial counter value; final counter; increment)
{
(Do Something)
}
14
FOR LOOP STATEMENT
Example 1:
FOR (x=1; x<5; x++)
{
PRINT “Hello World”
}
Output:
Hello World
Hello World
Hello World
Hello World
15
FOR LOOP STATEMENT
Example 2:
FOR (x=1; x<=5; x++)
{
PRINT x
}
Output:
1
2
3
4
5
16
WHILE LOOP STATEMENT
 A WHILE loop is a loop that repeats while some condition
is satisfied.
 The WHILE loop tests its condition at the beginning of
every loop.
 If the condition is false from the start, the sequence of
activities contained in the loop never runs at all.
17
Flowchart:
WHILE LOOP STATEMENT
Pseudocode:
WHILE condition
Do Something Condition
Do something
False
True
18
WHILE LOOP STATEMENT
WHILE loop syntax:
WHILE (Condition)
{
(Do Something)
}
19
WHILE LOOP STATEMENT
Example 1:
WHILE (x < 5)
{
PRINT “Hello World”
x++
}
Output:
Hello World
Hello World
Hello World
Hello World
20
DO-WHILE LOOP STATEMENT
 Like a while loop, a do-while loop is a loop that repeats
while some condition is satisfied.
 Unlike a while loop, a do-while loop tests its condition at
the end of the loop.
 This means that its sequence of activities always runs at
least once.
21
DO-WHILE LOOP STATEMENT
Flowchart:
Pseudocode:
Do
something
WHILE condition
Condition
Do something
False
True
22
DO-WHILE LOOP STATEMENT
DO-WHILE Loop Syntax
DO
{
(Do something)
}
WHILE (Condition)
23
DO-WHILE LOOP STATEMENT
Example 1:
x=1
DO
{
PRINT “Hello World”
x++
} WHILE (x<5)
Output:
Hello World
Hello World
Hello World
Hello World
24
DO-WHILE LOOP STATEMENT
Example 2:
DO
{
PRINT “Hello World”
} WHILE (Key != Esc)
Output:
Hello World
Hello World
Hello World
…
25
EXERCISE
Draw flowchart diagram for the following programs using
loop:
1. A program that display number 1 to 20
2. A program that display a person name x times.

Más contenido relacionado

La actualidad más candente (20)

Looping statements
Looping statementsLooping statements
Looping statements
 
Looping Statement And Flow Chart
 Looping Statement And Flow Chart Looping Statement And Flow Chart
Looping Statement And Flow Chart
 
Computer
ComputerComputer
Computer
 
Java loops
Java loopsJava loops
Java loops
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++
 
Loops in c++ programming language
Loops in c++ programming language Loops in c++ programming language
Loops in c++ programming language
 
Loops
LoopsLoops
Loops
 
Loops in c
Loops in cLoops in c
Loops in c
 
While loop
While loopWhile loop
While loop
 
Chap 6(decision making-looping)
Chap 6(decision making-looping)Chap 6(decision making-looping)
Chap 6(decision making-looping)
 
C++ loop
C++ loop C++ loop
C++ loop
 
Log4cpp - Updated - to verify modify updates
Log4cpp - Updated - to verify modify updatesLog4cpp - Updated - to verify modify updates
Log4cpp - Updated - to verify modify updates
 
For Loop
For LoopFor Loop
For Loop
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
 
Control statements
Control statementsControl statements
Control statements
 
Java Programming: Loops
Java Programming: LoopsJava Programming: Loops
Java Programming: Loops
 
What is to loop in c++
What is to loop in c++What is to loop in c++
What is to loop in c++
 
Procedures andcursors
Procedures andcursorsProcedures andcursors
Procedures andcursors
 
Programming Fundamentals lecture 8
Programming Fundamentals lecture 8Programming Fundamentals lecture 8
Programming Fundamentals lecture 8
 
Iteration
IterationIteration
Iteration
 

Similar a Fundamentals of Programming Chapter 7

loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docxJavvajiVenkat
 
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 LoopPriyom Majumder
 
Loop(for, while, do while) condition Presentation
Loop(for, while, do while) condition PresentationLoop(for, while, do while) condition Presentation
Loop(for, while, do while) condition PresentationBadrul Alam
 
Cpp loop types
Cpp loop typesCpp loop types
Cpp loop typesRj Baculo
 
Loop in C Properties & Applications
Loop in C Properties & ApplicationsLoop in C Properties & Applications
Loop in C Properties & ApplicationsEmroz Sardar
 
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.pdfKirubelWondwoson1
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programmingCHANDAN KUMAR
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while LoopJayBhavsar68
 
2nd year computer science chapter 12 notes
2nd year computer science chapter 12 notes2nd year computer science chapter 12 notes
2nd year computer science chapter 12 notesmuhammadFaheem656405
 
LOOPING IN C- PROGRAMMING.pptx
LOOPING IN C- PROGRAMMING.pptxLOOPING IN C- PROGRAMMING.pptx
LOOPING IN C- PROGRAMMING.pptxAFANJIPHILL
 
Repetations in C
Repetations in CRepetations in C
Repetations in Cyndaravind
 
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KRLoops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KRKrishna Raj
 
Introduction to flowchart
Introduction to flowchartIntroduction to flowchart
Introduction to flowchartJordan Delacruz
 

Similar a Fundamentals of Programming Chapter 7 (20)

loops in C ppt.pdf
loops in C ppt.pdfloops in C ppt.pdf
loops in C ppt.pdf
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
 
Loops c++
Loops c++Loops c++
Loops c++
 
Loops in c
Loops in cLoops in c
Loops in c
 
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
 
Loops
LoopsLoops
Loops
 
Loop(for, while, do while) condition Presentation
Loop(for, while, do while) condition PresentationLoop(for, while, do while) condition Presentation
Loop(for, while, do while) condition Presentation
 
Loop-1.pptx
Loop-1.pptxLoop-1.pptx
Loop-1.pptx
 
Cpp loop types
Cpp loop typesCpp loop types
Cpp loop types
 
Loop in C Properties & Applications
Loop in C Properties & ApplicationsLoop in C Properties & Applications
Loop in C Properties & Applications
 
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
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programming
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
 
2nd year computer science chapter 12 notes
2nd year computer science chapter 12 notes2nd year computer science chapter 12 notes
2nd year computer science chapter 12 notes
 
LOOPING IN C- PROGRAMMING.pptx
LOOPING IN C- PROGRAMMING.pptxLOOPING IN C- PROGRAMMING.pptx
LOOPING IN C- PROGRAMMING.pptx
 
Loops In C++
Loops In C++Loops In C++
Loops In C++
 
Repetations in C
Repetations in CRepetations in C
Repetations in C
 
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KRLoops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
 
Introduction to flowchart
Introduction to flowchartIntroduction to flowchart
Introduction to flowchart
 
Loop structures
Loop structuresLoop structures
Loop structures
 

Más de Mohd Harris Ahmad Jaal

Web Application Development using PHP Chapter 8
Web Application Development using PHP Chapter 8Web Application Development using PHP Chapter 8
Web Application Development using PHP Chapter 8Mohd Harris Ahmad Jaal
 
Web Application Development using PHP Chapter 7
Web Application Development using PHP Chapter 7Web Application Development using PHP Chapter 7
Web Application Development using PHP Chapter 7Mohd Harris Ahmad Jaal
 
Web Application Development using PHP Chapter 6
Web Application Development using PHP Chapter 6Web Application Development using PHP Chapter 6
Web Application Development using PHP Chapter 6Mohd Harris Ahmad Jaal
 
Web Application Development using PHP Chapter 5
Web Application Development using PHP Chapter 5Web Application Development using PHP Chapter 5
Web Application Development using PHP Chapter 5Mohd Harris Ahmad Jaal
 
Web Application Development using PHP Chapter 4
Web Application Development using PHP Chapter 4Web Application Development using PHP Chapter 4
Web Application Development using PHP Chapter 4Mohd Harris Ahmad Jaal
 
Web Application Development using PHP Chapter 3
Web Application Development using PHP Chapter 3Web Application Development using PHP Chapter 3
Web Application Development using PHP Chapter 3Mohd Harris Ahmad Jaal
 
Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2Mohd Harris Ahmad Jaal
 
Web Application Development using PHP Chapter 1
Web Application Development using PHP Chapter 1Web Application Development using PHP Chapter 1
Web Application Development using PHP Chapter 1Mohd Harris Ahmad Jaal
 

Más de Mohd Harris Ahmad Jaal (20)

Fundamentals of Programming Chapter 6
Fundamentals of Programming Chapter 6Fundamentals of Programming Chapter 6
Fundamentals of Programming Chapter 6
 
Fundamentals of Programming Chapter 4
Fundamentals of Programming Chapter 4Fundamentals of Programming Chapter 4
Fundamentals of Programming Chapter 4
 
Fundamentals of Programming Chapter 3
Fundamentals of Programming Chapter 3Fundamentals of Programming Chapter 3
Fundamentals of Programming Chapter 3
 
Fundamentals of Programming Chapter 2
Fundamentals of Programming Chapter 2Fundamentals of Programming Chapter 2
Fundamentals of Programming Chapter 2
 
Fundamentals of Programming Chapter 1
Fundamentals of Programming Chapter 1Fundamentals of Programming Chapter 1
Fundamentals of Programming Chapter 1
 
Fundamentals of Programming Chapter 5
Fundamentals of Programming Chapter 5Fundamentals of Programming Chapter 5
Fundamentals of Programming Chapter 5
 
Web Application Development using PHP Chapter 8
Web Application Development using PHP Chapter 8Web Application Development using PHP Chapter 8
Web Application Development using PHP Chapter 8
 
Web Application Development using PHP Chapter 7
Web Application Development using PHP Chapter 7Web Application Development using PHP Chapter 7
Web Application Development using PHP Chapter 7
 
Web Application Development using PHP Chapter 6
Web Application Development using PHP Chapter 6Web Application Development using PHP Chapter 6
Web Application Development using PHP Chapter 6
 
Web Application Development using PHP Chapter 5
Web Application Development using PHP Chapter 5Web Application Development using PHP Chapter 5
Web Application Development using PHP Chapter 5
 
Web Application Development using PHP Chapter 4
Web Application Development using PHP Chapter 4Web Application Development using PHP Chapter 4
Web Application Development using PHP Chapter 4
 
Web Application Development using PHP Chapter 3
Web Application Development using PHP Chapter 3Web Application Development using PHP Chapter 3
Web Application Development using PHP Chapter 3
 
Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2
 
Web Application Development using PHP Chapter 1
Web Application Development using PHP Chapter 1Web Application Development using PHP Chapter 1
Web Application Development using PHP Chapter 1
 
Fundamentals of Computing Chapter 10
Fundamentals of Computing Chapter 10Fundamentals of Computing Chapter 10
Fundamentals of Computing Chapter 10
 
Fundamentals of Computing Chapter 9
Fundamentals of Computing Chapter 9Fundamentals of Computing Chapter 9
Fundamentals of Computing Chapter 9
 
Fundamentals of Computing Chapter 8
Fundamentals of Computing Chapter 8Fundamentals of Computing Chapter 8
Fundamentals of Computing Chapter 8
 
Fundamentals of Computing Chapter 7
Fundamentals of Computing Chapter 7Fundamentals of Computing Chapter 7
Fundamentals of Computing Chapter 7
 
Fundamentals of Computing Chapter 6
Fundamentals of Computing Chapter 6Fundamentals of Computing Chapter 6
Fundamentals of Computing Chapter 6
 
Fundamentals of Computing Chapter 5
Fundamentals of Computing Chapter 5Fundamentals of Computing Chapter 5
Fundamentals of Computing Chapter 5
 

Último

Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 

Último (20)

Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 

Fundamentals of Programming Chapter 7

  • 1. C H AP T E R 7 L O O P S PROG0101 FUNDAMENTALS OF PROGRAMMING C H AP T E R 6 B R AN C H I N G PROG0101 FUNDAMENTALS OF PROGRAMMING Chapter 5 Operators PROG0101 FUNDAMENTALS OF PROGRAMMING CHAPTER 4 VARIABLES AND DATA TYPES PROG0101 FUNDAMENTALS OF PROGRAMMING CHAPTER 3 ALGORITHMS PROG0101 FUNDAMENTALS OF PROGRAMMING C H A P T E R 1 O V E R V I E W O F C O M P U T E R S A N D L O G I C PROG0101 FUNDAMENTALS OF PROGRAMMING PROG0101 FUNDAMENTALS OF PROGRAMMING CHAPTER 7 LOOP STATEMENTS
  • 2. 2 LOOP STATEMENTS  A loop statement is a sequence of instructions that is continually repeated until a certain condition is reached.  Loops allow for the same statement to be executed a number of times in succession.
  • 3. 3 LOOP STATEMENTS Loops Pseudocode: Loop Do Something Until Condition Flowchart: Conditio n Do something False True
  • 4. 4 LOOP STATEMENTS  There are three types which are common to most programming languages:  Condition Tested Loops  Counted Loops  Endless Loops
  • 5. 5 CONDITION TESTED LOOP  A condition tested loop is one which repeats a set of instructions until a certain condition is reached.  The test can be performed at the start of the loop (before any of the instructions are executed), during the loop, or at the end of the loop.  Usually, the condition will be testing the result of executing the statements that are inside the loop.
  • 6. 6 COUNTED LOOP  A counted loop is one which allows the programmer to instruct the computer to perform a set of instructions x times, where x is usually an integer value, but some programming languages offer other data types.  One could argue that the counted loop is just a condition tested loop which updates a counter and exits once a given value is reached.
  • 7. 7 COUNTED LOOP  The only time to use a count loop is when the program can determine ahead of time how many times the loop will repeat.  There are generally two ways that the number of repetitions of a loop will be know ahead of time:  The loop always repeats the same number of times.  The program calculates the number of repetitions based upon user input.
  • 8. 8 ENDLESS LOOP  An endless loop goes round and round until one of three things happens:  The computer is turned off (or the application stopped, forcefully)  The computer encounters an EXIT (or similar) statement  An error forces the application to 'crash'  Some endless loops serve a purpose, in message loops, for example, where it is necessary to continually monitor for incoming messages from the operating system.
  • 9. 9 EXAMPLE OF LOOP STATEMENTS  These are examples loop statement in programming language  FOR Loop  WHILE Loop  DO … WHILE Loop
  • 10. 10 FOR LOOP STATEMENT  A FOR loop is a loop that repeats a specified number of times.  The loop uses a counter to tell it how many times to run the same sequence of activities.
  • 11. 11 FOR LOOP STATEMENT  The counter has the following three numeric values:  Initial counter value  Increment (the amount to add to the counter each time the loop runs)  Final counter value  The loop ends when the counter reaches the final counter value, or, if there is an associated test condition, when the test condition is true.
  • 12. 12 Flowchart: FOR LOOP STATEMENT Condition (Final counter) Do something Initial value Increment Pseudocode: FOR x times Do Something Increment
  • 13. 13 FOR LOOP STATEMENT FOR loop syntax: for (initial counter value; final counter; increment) { (Do Something) }
  • 14. 14 FOR LOOP STATEMENT Example 1: FOR (x=1; x<5; x++) { PRINT “Hello World” } Output: Hello World Hello World Hello World Hello World
  • 15. 15 FOR LOOP STATEMENT Example 2: FOR (x=1; x<=5; x++) { PRINT x } Output: 1 2 3 4 5
  • 16. 16 WHILE LOOP STATEMENT  A WHILE loop is a loop that repeats while some condition is satisfied.  The WHILE loop tests its condition at the beginning of every loop.  If the condition is false from the start, the sequence of activities contained in the loop never runs at all.
  • 17. 17 Flowchart: WHILE LOOP STATEMENT Pseudocode: WHILE condition Do Something Condition Do something False True
  • 18. 18 WHILE LOOP STATEMENT WHILE loop syntax: WHILE (Condition) { (Do Something) }
  • 19. 19 WHILE LOOP STATEMENT Example 1: WHILE (x < 5) { PRINT “Hello World” x++ } Output: Hello World Hello World Hello World Hello World
  • 20. 20 DO-WHILE LOOP STATEMENT  Like a while loop, a do-while loop is a loop that repeats while some condition is satisfied.  Unlike a while loop, a do-while loop tests its condition at the end of the loop.  This means that its sequence of activities always runs at least once.
  • 21. 21 DO-WHILE LOOP STATEMENT Flowchart: Pseudocode: Do something WHILE condition Condition Do something False True
  • 22. 22 DO-WHILE LOOP STATEMENT DO-WHILE Loop Syntax DO { (Do something) } WHILE (Condition)
  • 23. 23 DO-WHILE LOOP STATEMENT Example 1: x=1 DO { PRINT “Hello World” x++ } WHILE (x<5) Output: Hello World Hello World Hello World Hello World
  • 24. 24 DO-WHILE LOOP STATEMENT Example 2: DO { PRINT “Hello World” } WHILE (Key != Esc) Output: Hello World Hello World Hello World …
  • 25. 25 EXERCISE Draw flowchart diagram for the following programs using loop: 1. A program that display number 1 to 20 2. A program that display a person name x times.