SlideShare a Scribd company logo
University of Kufa
Collage of Computer Science and
Mathematics
lecture5
‫م‬
.
‫م‬
‫االمين‬ ‫الحسن‬ ‫عبد‬ ‫هدى‬
Repetition Statements
 Repetition statements allow us to execute a statement multiple times
 Often they are referred to as loops
 Like conditional statements, they are controlled by boolean
expressions
 Java has three kinds of repetition statements: while, do while,
and for loops
The while Statement
 A while statement has the following syntax:
while ( condition )
statement;
 If the condition is true, the statement is executed
 Then the condition is evaluated again, and if it is still true, the statement is
executed again
 The statement is executed repeatedly until the condition becomes false
Java Repetiotion Statements
The while Statement
 An example of a while statement:
 If the condition of a while loop is false initially, the statement is never executed
 Therefore, the body of a while loop will execute zero or more times
int count = 1;
while (count <= 5)
{
System.out.println (count);
count++;
}
Output :
1
2
3
4
5
Nested Loops
 Similar to nested if statements, loops can be nested as well
 That is, the body of a loop can contain another loop
 For each iteration of the outer loop, the inner loop iterates
completely
How many times will the string "Here" be printed?
count1 = 1;
while (count1 <= 10)
{
count2 = 1;
while (count2 < 20)
{
System.out.println ("Here");
count2++;
}
count1++;
}
How many times will the string "Here" be printed?
count1 = 1;
while (count1 <= 10)
{
count2 = 1;
while (count2 < 20)
{
System.out.println ("Here");
count2++;
}
count1++;
}
10 * 19 = 190
The Do while Statement
 Do-while loop is similar to while loop, however there is a
difference between them: In while loop, condition is
evaluated before the execution of loop’s body but in do-
while loop condition is evaluated after the execution of
loop’s body.
The Do while Statement
 Syntax of do-while loop:
Do
{ statement (S);
} while (condition);
How do-while loop works?
 First, the statements inside loop execute and then the
condition gets evaluated, if the condition returns true
then the control gets transferred to the “do” else it jumps
to the next statement after do-while.
Java Repetiotion Statements
do-while loop example
do-while loop example
For Loop
 A For statement has the following syntax:
for(initialization; condition ; increment/decrement)
{ statement (S) ;
}
Flow of Execution of the for Loop
 As a program executes, the interpreter always keeps track of which
statement is about to be executed. We call this the control flow, or the flow
of execution of the program.
Java Repetiotion Statements
For Loop
 First step: In for loop, initialization happens first and only one time, which
means that the initialization part of for loop only executes once.
 Second step: Condition in for loop is evaluated on each iteration, if the
condition is true then the statements inside for loop body gets executed.
Once the condition returns false, the statements in for loop does not execute
and the control gets transferred to the next statement in the program after
for loop
 Third step: After every execution of for loop’s body, the
increment/decrement part of for loop executes that updates the loop
counter.
 Fourth step: After third step, the control jumps to second step and condition
is re-evaluated.
Example of Simple For loop
Example of Simple For loop
Ad

More Related Content

What's hot (20)

Loops Basics
Loops BasicsLoops Basics
Loops Basics
Mushiii
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
Mahantesh Devoor
 
Java loops
Java loopsJava loops
Java loops
ricardovigan
 
Control statements
Control statementsControl statements
Control statements
Kanwalpreet Kaur
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
deekshagopaliya
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
Madishetty Prathibha
 
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
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
tanmaymodi4
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
Abhishek Choksi
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
Suneel Dogra
 
Loops in c++ programming language
Loops in c++ programming language Loops in c++ programming language
Loops in c++ programming language
MUHAMMAD ALI student of IT at karakoram International university gilgit baltistan
 
Loops in C
Loops in CLoops in C
Loops in C
Kamal Acharya
 
While loops
While loopsWhile loops
While loops
Michael Gordon
 
Loops
LoopsLoops
Loops
Kulachi Hansraj Model School Ashok Vihar
 
Different loops in C
Different loops in CDifferent loops in C
Different loops in C
Md. Arif Hossain
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++
Deepak Singh
 
Flow of control ppt
Flow of control pptFlow of control ppt
Flow of control ppt
Indraprastha Institute of Information Technology
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programming
CHANDAN KUMAR
 
C++ decision making
C++ decision makingC++ decision making
C++ decision making
Zohaib Ahmed
 
Looping
LoopingLooping
Looping
Kulachi Hansraj Model School Ashok Vihar
 

Similar to Java Repetiotion Statements (20)

cpu.pdf
cpu.pdfcpu.pdf
cpu.pdf
RAJCHATTERJEE24
 
Loops In C++
Loops In C++Loops In C++
Loops In C++
Banasthali Vidyapith
 
presentation on powerpoint template.pptx
presentation on powerpoint template.pptxpresentation on powerpoint template.pptx
presentation on powerpoint template.pptx
farantouqeer8
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
JavvajiVenkat
 
Loops in c++
Loops in c++Loops in c++
Loops in c++
Rebin Daho
 
Lecture on Loop while loop for loop + program
Lecture on Loop while loop for loop + programLecture on Loop while loop for loop + program
Lecture on Loop while loop for loop + program
RahulKumar812056
 
Cse lecture-7-c loop
Cse lecture-7-c loopCse lecture-7-c loop
Cse lecture-7-c loop
FarshidKhan
 
Programming loop
Programming loopProgramming loop
Programming loop
University of Potsdam
 
Loops in c
Loops in cLoops in c
Loops in c
RekhaBudhwar
 
C language 2
C language 2C language 2
C language 2
Arafat Bin Reza
 
dizital pods session 5-loops.pptx
dizital pods session 5-loops.pptxdizital pods session 5-loops.pptx
dizital pods session 5-loops.pptx
VijayKumarLokanadam
 
Loop
LoopLoop
Loop
MdEmonRana
 
What is loops? What is For loop?
What is loops? What is For loop?What is loops? What is For loop?
What is loops? What is For loop?
AnuragSrivastava272
 
Loop structures
Loop structuresLoop structures
Loop structures
tazeem sana
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
MURALIDHAR R
 
Looping (Computer programming and utilization)
Looping (Computer programming and utilization)Looping (Computer programming and utilization)
Looping (Computer programming and utilization)
Digvijaysinh Gohil
 
Computer programming 2 Lesson 8
Computer programming 2  Lesson 8Computer programming 2  Lesson 8
Computer programming 2 Lesson 8
MLG College of Learning, Inc
 
3.looping(iteration statements)
3.looping(iteration statements)3.looping(iteration statements)
3.looping(iteration statements)
Hardik gupta
 
Loops in C.net.pptx
Loops in C.net.pptxLoops in C.net.pptx
Loops in C.net.pptx
Fajela
 
control-statements detailed presentation
control-statements detailed presentationcontrol-statements detailed presentation
control-statements detailed presentation
gayathripcs
 
presentation on powerpoint template.pptx
presentation on powerpoint template.pptxpresentation on powerpoint template.pptx
presentation on powerpoint template.pptx
farantouqeer8
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
JavvajiVenkat
 
Lecture on Loop while loop for loop + program
Lecture on Loop while loop for loop + programLecture on Loop while loop for loop + program
Lecture on Loop while loop for loop + program
RahulKumar812056
 
Cse lecture-7-c loop
Cse lecture-7-c loopCse lecture-7-c loop
Cse lecture-7-c loop
FarshidKhan
 
dizital pods session 5-loops.pptx
dizital pods session 5-loops.pptxdizital pods session 5-loops.pptx
dizital pods session 5-loops.pptx
VijayKumarLokanadam
 
What is loops? What is For loop?
What is loops? What is For loop?What is loops? What is For loop?
What is loops? What is For loop?
AnuragSrivastava272
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
MURALIDHAR R
 
Looping (Computer programming and utilization)
Looping (Computer programming and utilization)Looping (Computer programming and utilization)
Looping (Computer programming and utilization)
Digvijaysinh Gohil
 
3.looping(iteration statements)
3.looping(iteration statements)3.looping(iteration statements)
3.looping(iteration statements)
Hardik gupta
 
Loops in C.net.pptx
Loops in C.net.pptxLoops in C.net.pptx
Loops in C.net.pptx
Fajela
 
control-statements detailed presentation
control-statements detailed presentationcontrol-statements detailed presentation
control-statements detailed presentation
gayathripcs
 
Ad

More from Huda Alameen (19)

Architectural design
Architectural designArchitectural design
Architectural design
Huda Alameen
 
System Modeling
System ModelingSystem Modeling
System Modeling
Huda Alameen
 
Requirements Engineering
Requirements EngineeringRequirements Engineering
Requirements Engineering
Huda Alameen
 
Java Print method
Java  Print methodJava  Print method
Java Print method
Huda Alameen
 
Softweare Engieering
Softweare Engieering Softweare Engieering
Softweare Engieering
Huda Alameen
 
Softweare Engieering
Softweare Engieering Softweare Engieering
Softweare Engieering
Huda Alameen
 
Structured query language(sql)
Structured query language(sql)Structured query language(sql)
Structured query language(sql)
Huda Alameen
 
Sql viwes
Sql viwesSql viwes
Sql viwes
Huda Alameen
 
Relational algebra
Relational algebraRelational algebra
Relational algebra
Huda Alameen
 
Normalization
NormalizationNormalization
Normalization
Huda Alameen
 
Lecture one db
Lecture one dbLecture one db
Lecture one db
Huda Alameen
 
Introduction to structured query language
Introduction to structured query languageIntroduction to structured query language
Introduction to structured query language
Huda Alameen
 
Indexing techniques
Indexing techniquesIndexing techniques
Indexing techniques
Huda Alameen
 
Agg fun
Agg funAgg fun
Agg fun
Huda Alameen
 
Se lec1 (1)
Se lec1 (1)Se lec1 (1)
Se lec1 (1)
Huda Alameen
 
Se lec6
Se lec6Se lec6
Se lec6
Huda Alameen
 
Se lec5
Se lec5Se lec5
Se lec5
Huda Alameen
 
Se lec 4
Se lec 4Se lec 4
Se lec 4
Huda Alameen
 
Se lec 3
Se lec 3Se lec 3
Se lec 3
Huda Alameen
 
Ad

Recently uploaded (20)

Electroencephalogram_ wave components_Aignificancr
Electroencephalogram_ wave components_AignificancrElectroencephalogram_ wave components_Aignificancr
Electroencephalogram_ wave components_Aignificancr
klynct
 
Components of the Human Circulatory System.pptx
Components of the Human  Circulatory System.pptxComponents of the Human  Circulatory System.pptx
Components of the Human Circulatory System.pptx
autumnstreaks
 
The Link Between Subsurface Rheology and EjectaMobility: The Case of Small Ne...
The Link Between Subsurface Rheology and EjectaMobility: The Case of Small Ne...The Link Between Subsurface Rheology and EjectaMobility: The Case of Small Ne...
The Link Between Subsurface Rheology and EjectaMobility: The Case of Small Ne...
Sérgio Sacani
 
2. peptic ulcer (1) (1) for Pharm D .pptx
2. peptic ulcer (1) (1) for Pharm D .pptx2. peptic ulcer (1) (1) for Pharm D .pptx
2. peptic ulcer (1) (1) for Pharm D .pptx
fafyfskhan251kmf
 
Top 10 Biotech Startups for Beginners.pptx
Top 10 Biotech Startups for Beginners.pptxTop 10 Biotech Startups for Beginners.pptx
Top 10 Biotech Startups for Beginners.pptx
alexbagheriam
 
Macrolide and Miscellaneous Antibiotics.ppt
Macrolide and Miscellaneous Antibiotics.pptMacrolide and Miscellaneous Antibiotics.ppt
Macrolide and Miscellaneous Antibiotics.ppt
HRUTUJA WAGH
 
8. Gait cycle and it's determinants completely
8. Gait cycle and it's determinants completely8. Gait cycle and it's determinants completely
8. Gait cycle and it's determinants completely
Mominaakram4
 
THE SENSORY ORGANS BY DR. SADAKAT BASHIR.pptx
THE SENSORY ORGANS BY DR. SADAKAT BASHIR.pptxTHE SENSORY ORGANS BY DR. SADAKAT BASHIR.pptx
THE SENSORY ORGANS BY DR. SADAKAT BASHIR.pptx
SadakatBashir
 
SULPHONAMIDES AND SULFONES Medicinal Chemistry III.ppt
SULPHONAMIDES AND SULFONES Medicinal Chemistry III.pptSULPHONAMIDES AND SULFONES Medicinal Chemistry III.ppt
SULPHONAMIDES AND SULFONES Medicinal Chemistry III.ppt
HRUTUJA WAGH
 
Antimalarial drug Medicinal Chemistry III
Antimalarial drug Medicinal Chemistry IIIAntimalarial drug Medicinal Chemistry III
Antimalarial drug Medicinal Chemistry III
HRUTUJA WAGH
 
Anti fungal agents Medicinal Chemistry III
Anti fungal agents Medicinal Chemistry  IIIAnti fungal agents Medicinal Chemistry  III
Anti fungal agents Medicinal Chemistry III
HRUTUJA WAGH
 
Somato_Sensory _ somatomotor_Nervous_System.pptx
Somato_Sensory _ somatomotor_Nervous_System.pptxSomato_Sensory _ somatomotor_Nervous_System.pptx
Somato_Sensory _ somatomotor_Nervous_System.pptx
klynct
 
cdna synthesis and construction of gene libraries.pptx
cdna synthesis and construction of gene libraries.pptxcdna synthesis and construction of gene libraries.pptx
cdna synthesis and construction of gene libraries.pptx
jatinjadon777
 
university of arizona ~ favor's college candidate project.pptx
university of arizona ~ favor's college candidate project.pptxuniversity of arizona ~ favor's college candidate project.pptx
university of arizona ~ favor's college candidate project.pptx
favoranamelechi107
 
An upper limit to the lifetime of stellar remnants from gravitational pair pr...
An upper limit to the lifetime of stellar remnants from gravitational pair pr...An upper limit to the lifetime of stellar remnants from gravitational pair pr...
An upper limit to the lifetime of stellar remnants from gravitational pair pr...
Sérgio Sacani
 
Chapter-10-Light-reflection-and-refraction.ppt
Chapter-10-Light-reflection-and-refraction.pptChapter-10-Light-reflection-and-refraction.ppt
Chapter-10-Light-reflection-and-refraction.ppt
uniyaladiti914
 
Forestry_Exit_Exam_Wollega University_Gimbi Campus.pdf
Forestry_Exit_Exam_Wollega University_Gimbi Campus.pdfForestry_Exit_Exam_Wollega University_Gimbi Campus.pdf
Forestry_Exit_Exam_Wollega University_Gimbi Campus.pdf
ChalaKelbessa
 
Brief Presentation on Garment Washing.pdf
Brief Presentation on Garment Washing.pdfBrief Presentation on Garment Washing.pdf
Brief Presentation on Garment Washing.pdf
BharathKumar556689
 
earthquake and faults .pdf vtrvygbgbyggbybgybgyb
earthquake and faults .pdf vtrvygbgbyggbybgybgybearthquake and faults .pdf vtrvygbgbyggbybgybgyb
earthquake and faults .pdf vtrvygbgbyggbybgybgyb
KRISELJASMINOPEA
 
class 7 polygenic inheritance.pptx biochemistry
class 7 polygenic inheritance.pptx biochemistryclass 7 polygenic inheritance.pptx biochemistry
class 7 polygenic inheritance.pptx biochemistry
LavanyaVijaykumar2
 
Electroencephalogram_ wave components_Aignificancr
Electroencephalogram_ wave components_AignificancrElectroencephalogram_ wave components_Aignificancr
Electroencephalogram_ wave components_Aignificancr
klynct
 
Components of the Human Circulatory System.pptx
Components of the Human  Circulatory System.pptxComponents of the Human  Circulatory System.pptx
Components of the Human Circulatory System.pptx
autumnstreaks
 
The Link Between Subsurface Rheology and EjectaMobility: The Case of Small Ne...
The Link Between Subsurface Rheology and EjectaMobility: The Case of Small Ne...The Link Between Subsurface Rheology and EjectaMobility: The Case of Small Ne...
The Link Between Subsurface Rheology and EjectaMobility: The Case of Small Ne...
Sérgio Sacani
 
2. peptic ulcer (1) (1) for Pharm D .pptx
2. peptic ulcer (1) (1) for Pharm D .pptx2. peptic ulcer (1) (1) for Pharm D .pptx
2. peptic ulcer (1) (1) for Pharm D .pptx
fafyfskhan251kmf
 
Top 10 Biotech Startups for Beginners.pptx
Top 10 Biotech Startups for Beginners.pptxTop 10 Biotech Startups for Beginners.pptx
Top 10 Biotech Startups for Beginners.pptx
alexbagheriam
 
Macrolide and Miscellaneous Antibiotics.ppt
Macrolide and Miscellaneous Antibiotics.pptMacrolide and Miscellaneous Antibiotics.ppt
Macrolide and Miscellaneous Antibiotics.ppt
HRUTUJA WAGH
 
8. Gait cycle and it's determinants completely
8. Gait cycle and it's determinants completely8. Gait cycle and it's determinants completely
8. Gait cycle and it's determinants completely
Mominaakram4
 
THE SENSORY ORGANS BY DR. SADAKAT BASHIR.pptx
THE SENSORY ORGANS BY DR. SADAKAT BASHIR.pptxTHE SENSORY ORGANS BY DR. SADAKAT BASHIR.pptx
THE SENSORY ORGANS BY DR. SADAKAT BASHIR.pptx
SadakatBashir
 
SULPHONAMIDES AND SULFONES Medicinal Chemistry III.ppt
SULPHONAMIDES AND SULFONES Medicinal Chemistry III.pptSULPHONAMIDES AND SULFONES Medicinal Chemistry III.ppt
SULPHONAMIDES AND SULFONES Medicinal Chemistry III.ppt
HRUTUJA WAGH
 
Antimalarial drug Medicinal Chemistry III
Antimalarial drug Medicinal Chemistry IIIAntimalarial drug Medicinal Chemistry III
Antimalarial drug Medicinal Chemistry III
HRUTUJA WAGH
 
Anti fungal agents Medicinal Chemistry III
Anti fungal agents Medicinal Chemistry  IIIAnti fungal agents Medicinal Chemistry  III
Anti fungal agents Medicinal Chemistry III
HRUTUJA WAGH
 
Somato_Sensory _ somatomotor_Nervous_System.pptx
Somato_Sensory _ somatomotor_Nervous_System.pptxSomato_Sensory _ somatomotor_Nervous_System.pptx
Somato_Sensory _ somatomotor_Nervous_System.pptx
klynct
 
cdna synthesis and construction of gene libraries.pptx
cdna synthesis and construction of gene libraries.pptxcdna synthesis and construction of gene libraries.pptx
cdna synthesis and construction of gene libraries.pptx
jatinjadon777
 
university of arizona ~ favor's college candidate project.pptx
university of arizona ~ favor's college candidate project.pptxuniversity of arizona ~ favor's college candidate project.pptx
university of arizona ~ favor's college candidate project.pptx
favoranamelechi107
 
An upper limit to the lifetime of stellar remnants from gravitational pair pr...
An upper limit to the lifetime of stellar remnants from gravitational pair pr...An upper limit to the lifetime of stellar remnants from gravitational pair pr...
An upper limit to the lifetime of stellar remnants from gravitational pair pr...
Sérgio Sacani
 
Chapter-10-Light-reflection-and-refraction.ppt
Chapter-10-Light-reflection-and-refraction.pptChapter-10-Light-reflection-and-refraction.ppt
Chapter-10-Light-reflection-and-refraction.ppt
uniyaladiti914
 
Forestry_Exit_Exam_Wollega University_Gimbi Campus.pdf
Forestry_Exit_Exam_Wollega University_Gimbi Campus.pdfForestry_Exit_Exam_Wollega University_Gimbi Campus.pdf
Forestry_Exit_Exam_Wollega University_Gimbi Campus.pdf
ChalaKelbessa
 
Brief Presentation on Garment Washing.pdf
Brief Presentation on Garment Washing.pdfBrief Presentation on Garment Washing.pdf
Brief Presentation on Garment Washing.pdf
BharathKumar556689
 
earthquake and faults .pdf vtrvygbgbyggbybgybgyb
earthquake and faults .pdf vtrvygbgbyggbybgybgybearthquake and faults .pdf vtrvygbgbyggbybgybgyb
earthquake and faults .pdf vtrvygbgbyggbybgybgyb
KRISELJASMINOPEA
 
class 7 polygenic inheritance.pptx biochemistry
class 7 polygenic inheritance.pptx biochemistryclass 7 polygenic inheritance.pptx biochemistry
class 7 polygenic inheritance.pptx biochemistry
LavanyaVijaykumar2
 

Java Repetiotion Statements

  • 1. University of Kufa Collage of Computer Science and Mathematics lecture5 ‫م‬ . ‫م‬ ‫االمين‬ ‫الحسن‬ ‫عبد‬ ‫هدى‬
  • 2. Repetition Statements  Repetition statements allow us to execute a statement multiple times  Often they are referred to as loops  Like conditional statements, they are controlled by boolean expressions  Java has three kinds of repetition statements: while, do while, and for loops
  • 3. The while Statement  A while statement has the following syntax: while ( condition ) statement;  If the condition is true, the statement is executed  Then the condition is evaluated again, and if it is still true, the statement is executed again  The statement is executed repeatedly until the condition becomes false
  • 5. The while Statement  An example of a while statement:  If the condition of a while loop is false initially, the statement is never executed  Therefore, the body of a while loop will execute zero or more times int count = 1; while (count <= 5) { System.out.println (count); count++; } Output : 1 2 3 4 5
  • 6. Nested Loops  Similar to nested if statements, loops can be nested as well  That is, the body of a loop can contain another loop  For each iteration of the outer loop, the inner loop iterates completely
  • 7. How many times will the string "Here" be printed? count1 = 1; while (count1 <= 10) { count2 = 1; while (count2 < 20) { System.out.println ("Here"); count2++; } count1++; }
  • 8. How many times will the string "Here" be printed? count1 = 1; while (count1 <= 10) { count2 = 1; while (count2 < 20) { System.out.println ("Here"); count2++; } count1++; } 10 * 19 = 190
  • 9. The Do while Statement  Do-while loop is similar to while loop, however there is a difference between them: In while loop, condition is evaluated before the execution of loop’s body but in do- while loop condition is evaluated after the execution of loop’s body.
  • 10. The Do while Statement  Syntax of do-while loop: Do { statement (S); } while (condition);
  • 11. How do-while loop works?  First, the statements inside loop execute and then the condition gets evaluated, if the condition returns true then the control gets transferred to the “do” else it jumps to the next statement after do-while.
  • 15. For Loop  A For statement has the following syntax: for(initialization; condition ; increment/decrement) { statement (S) ; }
  • 16. Flow of Execution of the for Loop  As a program executes, the interpreter always keeps track of which statement is about to be executed. We call this the control flow, or the flow of execution of the program.
  • 18. For Loop  First step: In for loop, initialization happens first and only one time, which means that the initialization part of for loop only executes once.  Second step: Condition in for loop is evaluated on each iteration, if the condition is true then the statements inside for loop body gets executed. Once the condition returns false, the statements in for loop does not execute and the control gets transferred to the next statement in the program after for loop  Third step: After every execution of for loop’s body, the increment/decrement part of for loop executes that updates the loop counter.  Fourth step: After third step, the control jumps to second step and condition is re-evaluated.
  • 19. Example of Simple For loop
  • 20. Example of Simple For loop
  翻译: