SlideShare a Scribd company logo
Using Java
MINISTRY OF EDUCATION & HIGHER EDUCATION
COLLEGE OF SCIENCE AND TECHNOLOGY
KHANYOUNIS- PALESTINE
Lecture 9 & 10
Repetition Statements
 Essentials of Counter-Controlled Repetition
 while Repetition Statement
 Example:The average problem
 for Repetition Statement
 Example: Summing the Even Integers from 2 to 20
 do...while Repetition Statement
 break and continue Statements
 Emank X Mezank
2
Presented & Prepared by: Mahmoud R. Alfarra
 A repetition statement (also called a looping
statement or a loop) allows the programmer
to specify that a program should repeat an
action while some condition remains true.
3
Presented & Prepared by: Mahmoud R. Alfarra
What is repetition statement ?
While there are more items on my shopping list
Purchase next item and cross it off my list
 Counter-controlled repetition requires:
1. A control variable (or loop counter)
2. The initial value of the control variable
3. The increment (or decrement) by which the
control variable is modified each time through
the loop (also known as each iteration of the
loop)
4. The loop-continuation condition that determines
whether looping should continue.
4
Presented & Prepared by: Mahmoud R. Alfarra
Essentials of Counter-Controlled Repetition
 A program can test multiple cases by placing if...else
statements inside other if...else statements to
create nested if...else statements.
5
Presented & Prepared by: Mahmoud R. Alfarra
While Repetition Statement
int x = number;
while (Condition)
{
// actions
x--;
}
Initial variable
The loop-continuation condition,
Can be any other operators
Has a close relation with the
operators and the initial value of x
6
Presented & Prepared by: Mahmoud R. Alfarra
Be care
Not providing, in the body of a while statement, an action
that eventually causes the condition in the while to become
false normally results in a logic error called an infinite loop,
in which the loop never terminates.
Set a semi colon after the condition results a logic error
7
Presented & Prepared by: Mahmoud R. Alfarra
While Repetition Statement
true
false
Actions
Condition
Start
End
 The while loop is used when you want to test a
condition before entering into the loop.
 The while loop is considered a pretest loop, this allows
you to stop entering the loop even once if the condition
is not true.
 A class of ten students took a quiz. The
grades (integers in the range 0 to 100) for this
quiz are available to you. Determine the class
average on the quiz.
8
Presented & Prepared by: Mahmoud R. Alfarra
Example: The average problem
Write the pseudo code and flowchart of the above
example
HW 8.1
9
Presented & Prepared by: Mahmoud R. Alfarra
Example: The average problem
 If you want to execute a certain block of code
a specified number of times, use a for loop.
 The syntax of the for loop is as follows:
10
Presented & Prepared by: Mahmoud R. Alfarra
for Repetition Statement
Presented & Prepared by: Mahmoud R. Alfarra 11
How to perform repetition using for ?
System.out.println
(counter * 10);
12
Presented & Prepared by: Mahmoud R. Alfarra
Be care
Using an incorrect relational operator or an incorrect final
value of a loop counter in the loop-continuation condition of a
repetition statement can cause an off-by-one error.
Using commas instead of the two required semicolons in a
for header is a syntax error.
When a for statement's control variable is declared in the
initialization section of the for's header, using the control
variable after the for's body is a compilation error.
Placing a semicolon immediately to the right of the right
parenthesis of a for header makes that for's body an empty
statement. This is normally a logic error.
 Use a for statement to sum the even integers
from 2 to 20 and store the result in an int
variable called total.
13
Presented & Prepared by: Mahmoud R. Alfarra
Example: Summing the Even Integers
Write the pseudo code and flowchart of the above
example
HW 8.2
14
Presented & Prepared by: Mahmoud R. Alfarra
Example: Summing the Even Integers
Not using the proper relational operator in the loop-
continuation condition of a loop that counts downward (e.g.,
using i <= 1 instead of i >= 1 in a loop counting down to 1) is
usually a logic error.
15
Presented & Prepared by: Mahmoud R. Alfarra
 A person invests $1,000 in a savings account yielding 5%
interest. Assuming that all the interest is left on deposit,
calculate and print the amount of money in the account at
the end of each year for 10 years. Use the following formula
to determine the amounts:
where
• p is the original amount invested (i.e., the principal)
• r is the annual interest rate (e.g., use 0.05 for 5%)
• n is the number of years
• a is the amount on deposit at the end of the nth year.
Home Work
HW 8.3
a = p(1+r)
n
 If you would prefer that the testing of the
loop condition is done after executing the
loop’s code, you would use the post-test
version of the while loop, called the do …
while loop.
16
Presented & Prepared by: Mahmoud R. Alfarra
do...while Repetition Statement
Using do..While statement, the body always executes
at least once, but using while statement does not.
17
Presented & Prepared by: Mahmoud R. Alfarra
do...while Repetition Statement
int x = number;
do
{
// actions
x--;
} while (Condition)
Initial variable
The loop-continuation condition,
Can be any other operators
Has a close relation with the
operators and the initial value of x
18
Presented & Prepared by: Mahmoud R. Alfarra
do...while Repetition Statement
true
false
Actions Condition
Start
End
19
Presented & Prepared by: Mahmoud R. Alfarra
What is the difference between them ?
 a pretest condition
 can be executed for 0 or more
times
 does not end with semicolon !!!
 a posttest condition
 can be executed for 1 or more
times
 must end with semicolon !!!
while do…while
while (condition)
{
// actions
}
do
{
// actions
}
while (condition);
 Java provides statements break and continue
to alter the flow of control.
20
Presented & Prepared by: Mahmoud R. Alfarra
break and continue Statements
.
.
.
break;
Before loop’s block
After loop’s block
.
.
.
continue ;
Before loop’s block
After loop’s block
 The break statement, when executed in a
while, for, do...while or switch, causes
immediate exit from that statement.
 Execution continues with the first statement
after the control statement.
 Common uses of the break statement are to
escape early from a loop or to skip the
remainder of a switch.
21
Presented & Prepared by: Mahmoud R. Alfarra
break Statement
22
Presented & Prepared by: Mahmoud R. Alfarra
Example: break Statement
All the
iterations
are
completed
from 1 to 4
and in 5’th
iteration
the loop is
broken
 The continue statement, when executed in a
while, for or do...while, skips the remaining
statements in the loop body and proceeds
with the next iteration of the loop.
23
Presented & Prepared by: Mahmoud R. Alfarra
continue Statement
In while and do...while statements, the program evaluates
the loop-continuation test immediately after the continue
statement executes.
In a for statement, the increment expression executes, then
the program evaluates the loop-continuation test.
24
Presented & Prepared by: Mahmoud R. Alfarra
Example: continue Statement
All the
iterations
are
completed
except
iteration
number 5
‫قال‬
‫تيمية‬ ‫بن‬
:
‫و‬ ‫الدين‬ ‫من‬ ‫اإلسناد‬
‫لقال‬ ‫اإلسناد‬ ‫لوال‬
‫من‬
‫شاء‬ ‫ما‬ ‫شاء‬
25
Presented & Prepared by: Mahmoud R. Alfarra
Practices
26
Presented & Prepared by: Mahmoud R. Alfarra

More Related Content

What's hot (20)

Class 9 Lecture Notes
Class 9 Lecture Notes
Stephen Parsons
 
Cis 355 i lab 2 of 6
Cis 355 i lab 2 of 6
helpido9
 
Cis 355 ilab 2 of 6
Cis 355 ilab 2 of 6
comp274
 
Chapter 3 branching v4
Chapter 3 branching v4
Sunarto Quek
 
03b loops
03b loops
Manzoor ALam
 
Exception handling in ASP .NET
Exception handling in ASP .NET
baabtra.com - No. 1 supplier of quality freshers
 
4 coding from algorithms
4 coding from algorithms
hccit
 
F6dc1 session6 c++
F6dc1 session6 c++
Mukund Trivedi
 
Intro To C++ - Class 12 - For, do … While
Intro To C++ - Class 12 - For, do … While
Blue Elephant Consulting
 
CP Handout#4
CP Handout#4
trupti1976
 
Exception handling in c++ by manoj vasava
Exception handling in c++ by manoj vasava
Manoj_vasava
 
Lecture 1
Lecture 1
Mohammed Saleh
 
Pseudocode-Flowchart
Pseudocode-Flowchart
lotlot
 
Factorial of a number in python
Factorial of a number in python
varshachhajera
 
Controlstatment in c
Controlstatment in c
Md.Al-imran Roton
 
Intro To C++ - Class 10 - Control Statements: Part 2
Intro To C++ - Class 10 - Control Statements: Part 2
Blue Elephant Consulting
 
Problem solving and design
Problem solving and design
Renée Howard-Johnson
 
Programing Fundamental
Programing Fundamental
Qazi Shahzad Ali
 
Algorithms
Algorithms
nicky_walters
 
control statements of clangauge (ii unit)
control statements of clangauge (ii unit)
Prashant Sharma
 

Similar to Computer Programming, Loops using Java (20)

Java chapter 3
Java chapter 3
Abdii Rashid
 
130707833146508191
130707833146508191
Tanzeel Ahmad
 
Control statements in java
Control statements in java
Madishetty Prathibha
 
Java Repetiotion Statements
Java Repetiotion Statements
Huda Alameen
 
cpu.pdf
cpu.pdf
RAJCHATTERJEE24
 
While , For , Do-While Loop
While , For , Do-While Loop
Abhishek Choksi
 
Chapter 5 Loops by z al saeddddddddddddddddddddddddddddddddddd
Chapter 5 Loops by z al saeddddddddddddddddddddddddddddddddddd
zainaimadsaed
 
Introduction to Java Programming - Lecture 11.pptx
Introduction to Java Programming - Lecture 11.pptx
AbdulKhaleqHerawi1
 
Programming loop
Programming loop
University of Potsdam
 
UNIT 2 PPT.pdf
UNIT 2 PPT.pdf
DhanushKumar610673
 
Unit-02 Selection, Mathematical Functions and loops.pptx
Unit-02 Selection, Mathematical Functions and loops.pptx
jessicafalcao1
 
Looping statements
Looping statements
AbhishekMondal42
 
07 flow control
07 flow control
dhrubo kayal
 
_Java__Expressions__and__FlowControl.ppt
_Java__Expressions__and__FlowControl.ppt
JyothiAmpally
 
Looping statements in Java
Looping statements in Java
Jin Castor
 
Computer programming 2 Lesson 8
Computer programming 2 Lesson 8
MLG College of Learning, Inc
 
15-Loops.ppt
15-Loops.ppt
harshsolankurephotos
 
while- loop understanding with -ease.pdf
while- loop understanding with -ease.pdf
jigarvoice11
 
Eo gaddis java_chapter_05_5e
Eo gaddis java_chapter_05_5e
Gina Bullock
 
Eo gaddis java_chapter_05_5e
Eo gaddis java_chapter_05_5e
Gina Bullock
 
Java Repetiotion Statements
Java Repetiotion Statements
Huda Alameen
 
While , For , Do-While Loop
While , For , Do-While Loop
Abhishek Choksi
 
Chapter 5 Loops by z al saeddddddddddddddddddddddddddddddddddd
Chapter 5 Loops by z al saeddddddddddddddddddddddddddddddddddd
zainaimadsaed
 
Introduction to Java Programming - Lecture 11.pptx
Introduction to Java Programming - Lecture 11.pptx
AbdulKhaleqHerawi1
 
Unit-02 Selection, Mathematical Functions and loops.pptx
Unit-02 Selection, Mathematical Functions and loops.pptx
jessicafalcao1
 
_Java__Expressions__and__FlowControl.ppt
_Java__Expressions__and__FlowControl.ppt
JyothiAmpally
 
Looping statements in Java
Looping statements in Java
Jin Castor
 
while- loop understanding with -ease.pdf
while- loop understanding with -ease.pdf
jigarvoice11
 
Eo gaddis java_chapter_05_5e
Eo gaddis java_chapter_05_5e
Gina Bullock
 
Eo gaddis java_chapter_05_5e
Eo gaddis java_chapter_05_5e
Gina Bullock
 
Ad

More from Mahmoud Alfarra (20)

Chapter 10: hashing data structure
Chapter 10: hashing data structure
Mahmoud Alfarra
 
Chapter9 graph data structure
Chapter9 graph data structure
Mahmoud Alfarra
 
Chapter 8: tree data structure
Chapter 8: tree data structure
Mahmoud Alfarra
 
Chapter 7: Queue data structure
Chapter 7: Queue data structure
Mahmoud Alfarra
 
Chapter 6: stack data structure
Chapter 6: stack data structure
Mahmoud Alfarra
 
Chapter 5: linked list data structure
Chapter 5: linked list data structure
Mahmoud Alfarra
 
Chapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structure
Mahmoud Alfarra
 
Chapter 3: basic sorting algorithms data structure
Chapter 3: basic sorting algorithms data structure
Mahmoud Alfarra
 
Chapter 2: array and array list data structure
Chapter 2: array and array list data structure
Mahmoud Alfarra
 
Chapter1 intro toprincipleofc#_datastructure_b_cs
Chapter1 intro toprincipleofc#_datastructure_b_cs
Mahmoud Alfarra
 
Chapter 0: introduction to data structure
Chapter 0: introduction to data structure
Mahmoud Alfarra
 
3 classification
3 classification
Mahmoud Alfarra
 
5 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop20102011
Mahmoud Alfarra
 
4 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava20102011
Mahmoud Alfarra
 
3 programming-using-java introduction-to computer
3 programming-using-java introduction-to computer
Mahmoud Alfarra
 
1 programming-using-java -introduction
1 programming-using-java -introduction
Mahmoud Alfarra
 
تلخيص النصوص تلقائيا
تلخيص النصوص تلقائيا
Mahmoud Alfarra
 
4×4×4 لتحصيل التميز
4×4×4 لتحصيل التميز
Mahmoud Alfarra
 
Data preparation and processing chapter 2
Data preparation and processing chapter 2
Mahmoud Alfarra
 
Introduction to-data-mining chapter 1
Introduction to-data-mining chapter 1
Mahmoud Alfarra
 
Chapter 10: hashing data structure
Chapter 10: hashing data structure
Mahmoud Alfarra
 
Chapter9 graph data structure
Chapter9 graph data structure
Mahmoud Alfarra
 
Chapter 8: tree data structure
Chapter 8: tree data structure
Mahmoud Alfarra
 
Chapter 7: Queue data structure
Chapter 7: Queue data structure
Mahmoud Alfarra
 
Chapter 6: stack data structure
Chapter 6: stack data structure
Mahmoud Alfarra
 
Chapter 5: linked list data structure
Chapter 5: linked list data structure
Mahmoud Alfarra
 
Chapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structure
Mahmoud Alfarra
 
Chapter 3: basic sorting algorithms data structure
Chapter 3: basic sorting algorithms data structure
Mahmoud Alfarra
 
Chapter 2: array and array list data structure
Chapter 2: array and array list data structure
Mahmoud Alfarra
 
Chapter1 intro toprincipleofc#_datastructure_b_cs
Chapter1 intro toprincipleofc#_datastructure_b_cs
Mahmoud Alfarra
 
Chapter 0: introduction to data structure
Chapter 0: introduction to data structure
Mahmoud Alfarra
 
5 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop20102011
Mahmoud Alfarra
 
4 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava20102011
Mahmoud Alfarra
 
3 programming-using-java introduction-to computer
3 programming-using-java introduction-to computer
Mahmoud Alfarra
 
1 programming-using-java -introduction
1 programming-using-java -introduction
Mahmoud Alfarra
 
تلخيص النصوص تلقائيا
تلخيص النصوص تلقائيا
Mahmoud Alfarra
 
4×4×4 لتحصيل التميز
4×4×4 لتحصيل التميز
Mahmoud Alfarra
 
Data preparation and processing chapter 2
Data preparation and processing chapter 2
Mahmoud Alfarra
 
Introduction to-data-mining chapter 1
Introduction to-data-mining chapter 1
Mahmoud Alfarra
 
Ad

Recently uploaded (20)

june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
BINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Publishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke Warner
Brooke Warner
 
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Rajdeep Bavaliya
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
How to Manage Multi Language for Invoice in Odoo 18
How to Manage Multi Language for Invoice in Odoo 18
Celine George
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
LDMMIA Spring Ending Guest Grad Student News
LDMMIA Spring Ending Guest Grad Student News
LDM & Mia eStudios
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
LDMMIA GRAD Student Check-in Orientation Sampler
LDMMIA GRAD Student Check-in Orientation Sampler
LDM & Mia eStudios
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
BINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
Publishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke Warner
Brooke Warner
 
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Rajdeep Bavaliya
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
How to Manage Multi Language for Invoice in Odoo 18
How to Manage Multi Language for Invoice in Odoo 18
Celine George
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
LDMMIA Spring Ending Guest Grad Student News
LDMMIA Spring Ending Guest Grad Student News
LDM & Mia eStudios
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Webcrawler_Mule_AIChain_MuleSoft_Meetup_Hyderabad
Veera Pallapu
 
LDMMIA GRAD Student Check-in Orientation Sampler
LDMMIA GRAD Student Check-in Orientation Sampler
LDM & Mia eStudios
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 

Computer Programming, Loops using Java

  • 1. Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements
  • 2.  Essentials of Counter-Controlled Repetition  while Repetition Statement  Example:The average problem  for Repetition Statement  Example: Summing the Even Integers from 2 to 20  do...while Repetition Statement  break and continue Statements  Emank X Mezank 2 Presented & Prepared by: Mahmoud R. Alfarra
  • 3.  A repetition statement (also called a looping statement or a loop) allows the programmer to specify that a program should repeat an action while some condition remains true. 3 Presented & Prepared by: Mahmoud R. Alfarra What is repetition statement ? While there are more items on my shopping list Purchase next item and cross it off my list
  • 4.  Counter-controlled repetition requires: 1. A control variable (or loop counter) 2. The initial value of the control variable 3. The increment (or decrement) by which the control variable is modified each time through the loop (also known as each iteration of the loop) 4. The loop-continuation condition that determines whether looping should continue. 4 Presented & Prepared by: Mahmoud R. Alfarra Essentials of Counter-Controlled Repetition
  • 5.  A program can test multiple cases by placing if...else statements inside other if...else statements to create nested if...else statements. 5 Presented & Prepared by: Mahmoud R. Alfarra While Repetition Statement int x = number; while (Condition) { // actions x--; } Initial variable The loop-continuation condition, Can be any other operators Has a close relation with the operators and the initial value of x
  • 6. 6 Presented & Prepared by: Mahmoud R. Alfarra Be care Not providing, in the body of a while statement, an action that eventually causes the condition in the while to become false normally results in a logic error called an infinite loop, in which the loop never terminates. Set a semi colon after the condition results a logic error
  • 7. 7 Presented & Prepared by: Mahmoud R. Alfarra While Repetition Statement true false Actions Condition Start End  The while loop is used when you want to test a condition before entering into the loop.  The while loop is considered a pretest loop, this allows you to stop entering the loop even once if the condition is not true.
  • 8.  A class of ten students took a quiz. The grades (integers in the range 0 to 100) for this quiz are available to you. Determine the class average on the quiz. 8 Presented & Prepared by: Mahmoud R. Alfarra Example: The average problem Write the pseudo code and flowchart of the above example HW 8.1
  • 9. 9 Presented & Prepared by: Mahmoud R. Alfarra Example: The average problem
  • 10.  If you want to execute a certain block of code a specified number of times, use a for loop.  The syntax of the for loop is as follows: 10 Presented & Prepared by: Mahmoud R. Alfarra for Repetition Statement
  • 11. Presented & Prepared by: Mahmoud R. Alfarra 11 How to perform repetition using for ? System.out.println (counter * 10);
  • 12. 12 Presented & Prepared by: Mahmoud R. Alfarra Be care Using an incorrect relational operator or an incorrect final value of a loop counter in the loop-continuation condition of a repetition statement can cause an off-by-one error. Using commas instead of the two required semicolons in a for header is a syntax error. When a for statement's control variable is declared in the initialization section of the for's header, using the control variable after the for's body is a compilation error. Placing a semicolon immediately to the right of the right parenthesis of a for header makes that for's body an empty statement. This is normally a logic error.
  • 13.  Use a for statement to sum the even integers from 2 to 20 and store the result in an int variable called total. 13 Presented & Prepared by: Mahmoud R. Alfarra Example: Summing the Even Integers Write the pseudo code and flowchart of the above example HW 8.2
  • 14. 14 Presented & Prepared by: Mahmoud R. Alfarra Example: Summing the Even Integers Not using the proper relational operator in the loop- continuation condition of a loop that counts downward (e.g., using i <= 1 instead of i >= 1 in a loop counting down to 1) is usually a logic error.
  • 15. 15 Presented & Prepared by: Mahmoud R. Alfarra  A person invests $1,000 in a savings account yielding 5% interest. Assuming that all the interest is left on deposit, calculate and print the amount of money in the account at the end of each year for 10 years. Use the following formula to determine the amounts: where • p is the original amount invested (i.e., the principal) • r is the annual interest rate (e.g., use 0.05 for 5%) • n is the number of years • a is the amount on deposit at the end of the nth year. Home Work HW 8.3 a = p(1+r) n
  • 16.  If you would prefer that the testing of the loop condition is done after executing the loop’s code, you would use the post-test version of the while loop, called the do … while loop. 16 Presented & Prepared by: Mahmoud R. Alfarra do...while Repetition Statement Using do..While statement, the body always executes at least once, but using while statement does not.
  • 17. 17 Presented & Prepared by: Mahmoud R. Alfarra do...while Repetition Statement int x = number; do { // actions x--; } while (Condition) Initial variable The loop-continuation condition, Can be any other operators Has a close relation with the operators and the initial value of x
  • 18. 18 Presented & Prepared by: Mahmoud R. Alfarra do...while Repetition Statement true false Actions Condition Start End
  • 19. 19 Presented & Prepared by: Mahmoud R. Alfarra What is the difference between them ?  a pretest condition  can be executed for 0 or more times  does not end with semicolon !!!  a posttest condition  can be executed for 1 or more times  must end with semicolon !!! while do…while while (condition) { // actions } do { // actions } while (condition);
  • 20.  Java provides statements break and continue to alter the flow of control. 20 Presented & Prepared by: Mahmoud R. Alfarra break and continue Statements . . . break; Before loop’s block After loop’s block . . . continue ; Before loop’s block After loop’s block
  • 21.  The break statement, when executed in a while, for, do...while or switch, causes immediate exit from that statement.  Execution continues with the first statement after the control statement.  Common uses of the break statement are to escape early from a loop or to skip the remainder of a switch. 21 Presented & Prepared by: Mahmoud R. Alfarra break Statement
  • 22. 22 Presented & Prepared by: Mahmoud R. Alfarra Example: break Statement All the iterations are completed from 1 to 4 and in 5’th iteration the loop is broken
  • 23.  The continue statement, when executed in a while, for or do...while, skips the remaining statements in the loop body and proceeds with the next iteration of the loop. 23 Presented & Prepared by: Mahmoud R. Alfarra continue Statement In while and do...while statements, the program evaluates the loop-continuation test immediately after the continue statement executes. In a for statement, the increment expression executes, then the program evaluates the loop-continuation test.
  • 24. 24 Presented & Prepared by: Mahmoud R. Alfarra Example: continue Statement All the iterations are completed except iteration number 5
  • 25. ‫قال‬ ‫تيمية‬ ‫بن‬ : ‫و‬ ‫الدين‬ ‫من‬ ‫اإلسناد‬ ‫لقال‬ ‫اإلسناد‬ ‫لوال‬ ‫من‬ ‫شاء‬ ‫ما‬ ‫شاء‬ 25 Presented & Prepared by: Mahmoud R. Alfarra
  • 26. Practices 26 Presented & Prepared by: Mahmoud R. Alfarra