SlideShare a Scribd company logo
1 of 20
Download to read offline
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
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.
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.
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

More Related Content

What's hot (20)

Loops Basics
Loops BasicsLoops Basics
Loops Basics
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
 
Java loops
Java loopsJava loops
Java loops
 
Control statements
Control statementsControl statements
Control statements
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
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
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
 
Loops in c++ programming language
Loops in c++ programming language Loops in c++ programming language
Loops in c++ programming language
 
Loops in C
Loops in CLoops in C
Loops in C
 
While loops
While loopsWhile loops
While loops
 
Loops
LoopsLoops
Loops
 
Different loops in C
Different loops in CDifferent loops in C
Different loops in C
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++
 
Flow of control ppt
Flow of control pptFlow of control ppt
Flow of control ppt
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programming
 
C++ decision making
C++ decision makingC++ decision making
C++ decision making
 
Looping
LoopingLooping
Looping
 

Similar to Java Repetiotion Statements

Similar to Java Repetiotion Statements (20)

cpu.pdf
cpu.pdfcpu.pdf
cpu.pdf
 
Loops In C++
Loops In C++Loops In C++
Loops In C++
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
 
Loops in c++
Loops in c++Loops in c++
Loops in c++
 
Cse lecture-7-c loop
Cse lecture-7-c loopCse lecture-7-c loop
Cse lecture-7-c loop
 
Programming loop
Programming loopProgramming loop
Programming loop
 
Loops in c
Loops in cLoops in c
Loops in c
 
C language 2
C language 2C language 2
C language 2
 
dizital pods session 5-loops.pptx
dizital pods session 5-loops.pptxdizital pods session 5-loops.pptx
dizital pods session 5-loops.pptx
 
Loop
LoopLoop
Loop
 
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?
 
Loop structures
Loop structuresLoop structures
Loop structures
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
 
Looping (Computer programming and utilization)
Looping (Computer programming and utilization)Looping (Computer programming and utilization)
Looping (Computer programming and utilization)
 
Computer programming 2 Lesson 8
Computer programming 2  Lesson 8Computer programming 2  Lesson 8
Computer programming 2 Lesson 8
 
3.looping(iteration statements)
3.looping(iteration statements)3.looping(iteration statements)
3.looping(iteration statements)
 
Loops in C.net.pptx
Loops in C.net.pptxLoops in C.net.pptx
Loops in C.net.pptx
 
itretion.docx
itretion.docxitretion.docx
itretion.docx
 
Loops and iteration.docx
Loops and iteration.docxLoops and iteration.docx
Loops and iteration.docx
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 

More from Huda Alameen

More from Huda Alameen (19)

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

Recently uploaded

TransientOffsetin14CAftertheCarringtonEventRecordedbyPolarTreeRings
TransientOffsetin14CAftertheCarringtonEventRecordedbyPolarTreeRingsTransientOffsetin14CAftertheCarringtonEventRecordedbyPolarTreeRings
TransientOffsetin14CAftertheCarringtonEventRecordedbyPolarTreeRingsSérgio Sacani
 
POGONATUM : morphology, anatomy, reproduction etc.
POGONATUM : morphology, anatomy, reproduction etc.POGONATUM : morphology, anatomy, reproduction etc.
POGONATUM : morphology, anatomy, reproduction etc.Cherry
 
GBSN - Biochemistry (Unit 2) Basic concept of organic chemistry
GBSN - Biochemistry (Unit 2) Basic concept of organic chemistry GBSN - Biochemistry (Unit 2) Basic concept of organic chemistry
GBSN - Biochemistry (Unit 2) Basic concept of organic chemistry Areesha Ahmad
 
Dr. E. Muralinath_ Blood indices_clinical aspects
Dr. E. Muralinath_ Blood indices_clinical  aspectsDr. E. Muralinath_ Blood indices_clinical  aspects
Dr. E. Muralinath_ Blood indices_clinical aspectsmuralinath2
 
Terpineol and it's characterization pptx
Terpineol and it's characterization pptxTerpineol and it's characterization pptx
Terpineol and it's characterization pptxMuhammadRazzaq31
 
GBSN - Microbiology (Unit 3)Defense Mechanism of the body
GBSN - Microbiology (Unit 3)Defense Mechanism of the body GBSN - Microbiology (Unit 3)Defense Mechanism of the body
GBSN - Microbiology (Unit 3)Defense Mechanism of the body Areesha Ahmad
 
Gwalior ❤CALL GIRL 84099*07087 ❤CALL GIRLS IN Gwalior ESCORT SERVICE❤CALL GIRL
Gwalior ❤CALL GIRL 84099*07087 ❤CALL GIRLS IN Gwalior ESCORT SERVICE❤CALL GIRLGwalior ❤CALL GIRL 84099*07087 ❤CALL GIRLS IN Gwalior ESCORT SERVICE❤CALL GIRL
Gwalior ❤CALL GIRL 84099*07087 ❤CALL GIRLS IN Gwalior ESCORT SERVICE❤CALL GIRLkantirani197
 
Selaginella: features, morphology ,anatomy and reproduction.
Selaginella: features, morphology ,anatomy and reproduction.Selaginella: features, morphology ,anatomy and reproduction.
Selaginella: features, morphology ,anatomy and reproduction.Cherry
 
ONLINE VOTING SYSTEM SE Project for vote
ONLINE VOTING SYSTEM SE Project for voteONLINE VOTING SYSTEM SE Project for vote
ONLINE VOTING SYSTEM SE Project for voteRaunakRastogi4
 
Pteris : features, anatomy, morphology and lifecycle
Pteris : features, anatomy, morphology and lifecyclePteris : features, anatomy, morphology and lifecycle
Pteris : features, anatomy, morphology and lifecycleCherry
 
The Mariana Trench remarkable geological features on Earth.pptx
The Mariana Trench remarkable geological features on Earth.pptxThe Mariana Trench remarkable geological features on Earth.pptx
The Mariana Trench remarkable geological features on Earth.pptxseri bangash
 
Kanchipuram Escorts 🥰 8617370543 Call Girls Offer VIP Hot Girls
Kanchipuram Escorts 🥰 8617370543 Call Girls Offer VIP Hot GirlsKanchipuram Escorts 🥰 8617370543 Call Girls Offer VIP Hot Girls
Kanchipuram Escorts 🥰 8617370543 Call Girls Offer VIP Hot GirlsDeepika Singh
 
Factory Acceptance Test( FAT).pptx .
Factory Acceptance Test( FAT).pptx       .Factory Acceptance Test( FAT).pptx       .
Factory Acceptance Test( FAT).pptx .Poonam Aher Patil
 
Call Girls Ahmedabad +917728919243 call me Independent Escort Service
Call Girls Ahmedabad +917728919243 call me Independent Escort ServiceCall Girls Ahmedabad +917728919243 call me Independent Escort Service
Call Girls Ahmedabad +917728919243 call me Independent Escort Serviceshivanisharma5244
 
Genome sequencing,shotgun sequencing.pptx
Genome sequencing,shotgun sequencing.pptxGenome sequencing,shotgun sequencing.pptx
Genome sequencing,shotgun sequencing.pptxCherry
 
FAIRSpectra - Enabling the FAIRification of Analytical Science
FAIRSpectra - Enabling the FAIRification of Analytical ScienceFAIRSpectra - Enabling the FAIRification of Analytical Science
FAIRSpectra - Enabling the FAIRification of Analytical ScienceAlex Henderson
 
FS P2 COMBO MSTA LAST PUSH past exam papers.
FS P2 COMBO MSTA LAST PUSH past exam papers.FS P2 COMBO MSTA LAST PUSH past exam papers.
FS P2 COMBO MSTA LAST PUSH past exam papers.takadzanijustinmaime
 
development of diagnostic enzyme assay to detect leuser virus
development of diagnostic enzyme assay to detect leuser virusdevelopment of diagnostic enzyme assay to detect leuser virus
development of diagnostic enzyme assay to detect leuser virusNazaninKarimi6
 

Recently uploaded (20)

TransientOffsetin14CAftertheCarringtonEventRecordedbyPolarTreeRings
TransientOffsetin14CAftertheCarringtonEventRecordedbyPolarTreeRingsTransientOffsetin14CAftertheCarringtonEventRecordedbyPolarTreeRings
TransientOffsetin14CAftertheCarringtonEventRecordedbyPolarTreeRings
 
POGONATUM : morphology, anatomy, reproduction etc.
POGONATUM : morphology, anatomy, reproduction etc.POGONATUM : morphology, anatomy, reproduction etc.
POGONATUM : morphology, anatomy, reproduction etc.
 
GBSN - Biochemistry (Unit 2) Basic concept of organic chemistry
GBSN - Biochemistry (Unit 2) Basic concept of organic chemistry GBSN - Biochemistry (Unit 2) Basic concept of organic chemistry
GBSN - Biochemistry (Unit 2) Basic concept of organic chemistry
 
ABHISHEK ANTIBIOTICS PPT MICROBIOLOGY // USES OF ANTIOBIOTICS TYPES OF ANTIB...
ABHISHEK ANTIBIOTICS PPT MICROBIOLOGY  // USES OF ANTIOBIOTICS TYPES OF ANTIB...ABHISHEK ANTIBIOTICS PPT MICROBIOLOGY  // USES OF ANTIOBIOTICS TYPES OF ANTIB...
ABHISHEK ANTIBIOTICS PPT MICROBIOLOGY // USES OF ANTIOBIOTICS TYPES OF ANTIB...
 
Dr. E. Muralinath_ Blood indices_clinical aspects
Dr. E. Muralinath_ Blood indices_clinical  aspectsDr. E. Muralinath_ Blood indices_clinical  aspects
Dr. E. Muralinath_ Blood indices_clinical aspects
 
Terpineol and it's characterization pptx
Terpineol and it's characterization pptxTerpineol and it's characterization pptx
Terpineol and it's characterization pptx
 
GBSN - Microbiology (Unit 3)Defense Mechanism of the body
GBSN - Microbiology (Unit 3)Defense Mechanism of the body GBSN - Microbiology (Unit 3)Defense Mechanism of the body
GBSN - Microbiology (Unit 3)Defense Mechanism of the body
 
Gwalior ❤CALL GIRL 84099*07087 ❤CALL GIRLS IN Gwalior ESCORT SERVICE❤CALL GIRL
Gwalior ❤CALL GIRL 84099*07087 ❤CALL GIRLS IN Gwalior ESCORT SERVICE❤CALL GIRLGwalior ❤CALL GIRL 84099*07087 ❤CALL GIRLS IN Gwalior ESCORT SERVICE❤CALL GIRL
Gwalior ❤CALL GIRL 84099*07087 ❤CALL GIRLS IN Gwalior ESCORT SERVICE❤CALL GIRL
 
Selaginella: features, morphology ,anatomy and reproduction.
Selaginella: features, morphology ,anatomy and reproduction.Selaginella: features, morphology ,anatomy and reproduction.
Selaginella: features, morphology ,anatomy and reproduction.
 
ONLINE VOTING SYSTEM SE Project for vote
ONLINE VOTING SYSTEM SE Project for voteONLINE VOTING SYSTEM SE Project for vote
ONLINE VOTING SYSTEM SE Project for vote
 
Pteris : features, anatomy, morphology and lifecycle
Pteris : features, anatomy, morphology and lifecyclePteris : features, anatomy, morphology and lifecycle
Pteris : features, anatomy, morphology and lifecycle
 
The Mariana Trench remarkable geological features on Earth.pptx
The Mariana Trench remarkable geological features on Earth.pptxThe Mariana Trench remarkable geological features on Earth.pptx
The Mariana Trench remarkable geological features on Earth.pptx
 
Kanchipuram Escorts 🥰 8617370543 Call Girls Offer VIP Hot Girls
Kanchipuram Escorts 🥰 8617370543 Call Girls Offer VIP Hot GirlsKanchipuram Escorts 🥰 8617370543 Call Girls Offer VIP Hot Girls
Kanchipuram Escorts 🥰 8617370543 Call Girls Offer VIP Hot Girls
 
Factory Acceptance Test( FAT).pptx .
Factory Acceptance Test( FAT).pptx       .Factory Acceptance Test( FAT).pptx       .
Factory Acceptance Test( FAT).pptx .
 
Call Girls Ahmedabad +917728919243 call me Independent Escort Service
Call Girls Ahmedabad +917728919243 call me Independent Escort ServiceCall Girls Ahmedabad +917728919243 call me Independent Escort Service
Call Girls Ahmedabad +917728919243 call me Independent Escort Service
 
Genome sequencing,shotgun sequencing.pptx
Genome sequencing,shotgun sequencing.pptxGenome sequencing,shotgun sequencing.pptx
Genome sequencing,shotgun sequencing.pptx
 
FAIRSpectra - Enabling the FAIRification of Analytical Science
FAIRSpectra - Enabling the FAIRification of Analytical ScienceFAIRSpectra - Enabling the FAIRification of Analytical Science
FAIRSpectra - Enabling the FAIRification of Analytical Science
 
FS P2 COMBO MSTA LAST PUSH past exam papers.
FS P2 COMBO MSTA LAST PUSH past exam papers.FS P2 COMBO MSTA LAST PUSH past exam papers.
FS P2 COMBO MSTA LAST PUSH past exam papers.
 
Site Acceptance Test .
Site Acceptance Test                    .Site Acceptance Test                    .
Site Acceptance Test .
 
development of diagnostic enzyme assay to detect leuser virus
development of diagnostic enzyme assay to detect leuser virusdevelopment of diagnostic enzyme assay to detect leuser virus
development of diagnostic enzyme assay to detect leuser virus
 

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
  • 4.
  • 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.
  • 12.
  • 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.
  • 17.
  • 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