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

More Related Content

What's hot

Looping statements
Looping statementsLooping statements
Looping statements
Chukka Nikhil Chakravarthy
 
Looping Statement And Flow Chart
 Looping Statement And Flow Chart Looping Statement And Flow Chart
Looping Statement And Flow Chart
Rahul Sahu
 
Computer
ComputerComputer
Computer
ckchitra jain
 
Java loops
Java loopsJava loops
Java loops
ricardovigan
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++
Deepak Singh
 
Loops in c++ programming language
Loops in c++ programming language Loops in c++ programming language
Loops
LoopsLoops
Loops in c
Loops in cLoops in c
Loops in c
shubhampandav3
 
While loop
While loopWhile loop
While loop
Feras_83
 
Chap 6(decision making-looping)
Chap 6(decision making-looping)Chap 6(decision making-looping)
C++ loop
C++ loop C++ loop
C++ loop
Khelan Ameen
 
Log4cpp - Updated - to verify modify updates
Log4cpp - Updated - to verify modify updatesLog4cpp - Updated - to verify modify updates
Log4cpp - Updated - to verify modify updates
yosaggregator2
 
For Loop
For LoopFor Loop
For Loop
Ghaffar Khan
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
bsdeol28
 
Control statements
Control statementsControl statements
Control statements
Kanwalpreet Kaur
 
Java Programming: Loops
Java Programming: LoopsJava Programming: Loops
Java Programming: Loops
Karwan Mustafa Kareem
 
What is to loop in c++
What is to loop in c++What is to loop in c++
What is to loop in c++
03446940736
 
Procedures andcursors
Procedures andcursorsProcedures andcursors
Procedures andcursors
LakshmiSamivel
 
Programming Fundamentals lecture 8
Programming Fundamentals lecture 8Programming Fundamentals lecture 8
Programming Fundamentals lecture 8
REHAN IJAZ
 
Iteration
IterationIteration
Iteration
Liam Dunphy
 

What's hot (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 to Fundamentals of Programming Chapter 7

loops in C ppt.pdf
loops in C ppt.pdfloops in C ppt.pdf
loops in C ppt.pdf
DrSamsonChepuri1
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
JavvajiVenkat
 
Loops c++
Loops c++Loops c++
Loops c++
Shivani Singh
 
Loops in c
Loops in cLoops in c
Loops in c
RekhaBudhwar
 
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
 
Loops
LoopsLoops
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
Badrul Alam
 
Loop-1.pptx
Loop-1.pptxLoop-1.pptx
Cpp loop types
Cpp loop typesCpp loop types
Cpp loop types
Rj Baculo
 
Loop in C Properties & Applications
Loop in C Properties & ApplicationsLoop in C Properties & Applications
Loop in C Properties & Applications
Emroz 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.pdf
KirubelWondwoson1
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programming
CHANDAN KUMAR
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
JayBhavsar68
 
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
muhammadFaheem656405
 
LOOPING IN C- PROGRAMMING.pptx
LOOPING IN C- PROGRAMMING.pptxLOOPING IN C- PROGRAMMING.pptx
LOOPING IN C- PROGRAMMING.pptx
AFANJIPHILL
 
Loops In C++
Loops In C++Loops In C++
Loops In C++
Banasthali Vidyapith
 
Repetations in C
Repetations in CRepetations in C
Repetations in C
yndaravind
 
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
Krishna Raj
 
Introduction to flowchart
Introduction to flowchartIntroduction to flowchart
Introduction to flowchart
Jordan Delacruz
 
Loop structures
Loop structuresLoop structures
Loop structures
tazeem sana
 

Similar to 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
 

More from Mohd Harris Ahmad Jaal

Fundamentals of Programming Chapter 6
Fundamentals of Programming Chapter 6Fundamentals of Programming Chapter 6
Fundamentals of Programming Chapter 6
Mohd Harris Ahmad Jaal
 
Fundamentals of Programming Chapter 4
Fundamentals of Programming Chapter 4Fundamentals of Programming Chapter 4
Fundamentals of Programming Chapter 4
Mohd Harris Ahmad Jaal
 
Fundamentals of Programming Chapter 3
Fundamentals of Programming Chapter 3Fundamentals of Programming Chapter 3
Fundamentals of Programming Chapter 3
Mohd Harris Ahmad Jaal
 
Fundamentals of Programming Chapter 2
Fundamentals of Programming Chapter 2Fundamentals of Programming Chapter 2
Fundamentals of Programming Chapter 2
Mohd Harris Ahmad Jaal
 
Fundamentals of Programming Chapter 1
Fundamentals of Programming Chapter 1Fundamentals of Programming Chapter 1
Fundamentals of Programming Chapter 1
Mohd Harris Ahmad Jaal
 
Fundamentals of Programming Chapter 5
Fundamentals of Programming Chapter 5Fundamentals of Programming Chapter 5
Fundamentals of Programming Chapter 5
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 8
Mohd 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 7
Mohd 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 6
Mohd 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 5
Mohd 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 4
Mohd 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 3
Mohd 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 2
Mohd 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 1
Mohd Harris Ahmad Jaal
 
Fundamentals of Computing Chapter 10
Fundamentals of Computing Chapter 10Fundamentals of Computing Chapter 10
Fundamentals of Computing Chapter 10
Mohd Harris Ahmad Jaal
 
Fundamentals of Computing Chapter 9
Fundamentals of Computing Chapter 9Fundamentals of Computing Chapter 9
Fundamentals of Computing Chapter 9
Mohd Harris Ahmad Jaal
 
Fundamentals of Computing Chapter 8
Fundamentals of Computing Chapter 8Fundamentals of Computing Chapter 8
Fundamentals of Computing Chapter 8
Mohd Harris Ahmad Jaal
 
Fundamentals of Computing Chapter 7
Fundamentals of Computing Chapter 7Fundamentals of Computing Chapter 7
Fundamentals of Computing Chapter 7
Mohd Harris Ahmad Jaal
 
Fundamentals of Computing Chapter 6
Fundamentals of Computing Chapter 6Fundamentals of Computing Chapter 6
Fundamentals of Computing Chapter 6
Mohd Harris Ahmad Jaal
 
Fundamentals of Computing Chapter 5
Fundamentals of Computing Chapter 5Fundamentals of Computing Chapter 5
Fundamentals of Computing Chapter 5
Mohd Harris Ahmad Jaal
 

More from 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
 

Recently uploaded

BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 

Recently uploaded (20)

BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 

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.