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

PPT
7 programming-using-java decision-making220102011
Mahmoud Alfarra
 
PPT
6 programming-using-java decision-making20102011-
Mahmoud Alfarra
 
PPT
Computer Programming, Loops using Java - part 2
Mahmoud Alfarra
 
PPT
2 programming-using-java how to built application
Mahmoud Alfarra
 
PPT
8 programming-using-java decision-making practices 20102011
Mahmoud Alfarra
 
PDF
Function procedure c6 c7
Omar Al-Sabek
 
PPTX
Error handling and debugging in vb
Salim M
 
PPT
Class 8 Lecture Notes
Stephen Parsons
 
7 programming-using-java decision-making220102011
Mahmoud Alfarra
 
6 programming-using-java decision-making20102011-
Mahmoud Alfarra
 
Computer Programming, Loops using Java - part 2
Mahmoud Alfarra
 
2 programming-using-java how to built application
Mahmoud Alfarra
 
8 programming-using-java decision-making practices 20102011
Mahmoud Alfarra
 
Function procedure c6 c7
Omar Al-Sabek
 
Error handling and debugging in vb
Salim M
 
Class 8 Lecture Notes
Stephen Parsons
 

What's hot (20)

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

Similar to Computer Programming, Loops using Java (20)

PPT
Visula C# Programming Lecture 4
Abou Bakr Ashraf
 
PDF
[ITP - Lecture 11] Loops in C/C++
Muhammad Hammad Waseem
 
PDF
Unit 2=Decision Control & Looping Statements.pdf
Dr. Ambedkar Institute of Technology, Bangalore 56
 
PDF
CIS 1403 lab 4 selection
Hamad Odhabi
 
PPTX
PPT_203105211_3.pptx
SaurabhNage1
 
PPTX
Control statements in java
Madishetty Prathibha
 
PPTX
Conditional Statement both conditional and loop.pptx
Zenith SVG
 
PDF
Operators, control statements represented in java
TharuniDiddekunta
 
DOCX
Programming Fundamentals lecture 8
REHAN IJAZ
 
PPTX
Introduction& Overview-to-C++_programming.pptx
divyadhanwani67
 
PPTX
Introduction to C++ programming language
divyadhanwani67
 
PDF
Module 2 - Control Structures c programming.pptm.pdf
SudipDebnath20
 
PPTX
Control-structure - while loop and do-while loop.pptx
arshadfarhad08
 
PPTX
DECISION MAKING AND BRANCHING - C Programming
MSridhar18
 
PDF
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Priyom Majumder
 
PPTX
Understanding and using while in c++
MOHANA ALMUQATI
 
PPTX
Introduction To Programming with Python Lecture 2
Syed Farjad Zia Zaidi
 
PDF
UNIT 2 PPT.pdf
DhanushKumar610673
 
PPTX
Looping statements
Jaya Kumari
 
Visula C# Programming Lecture 4
Abou Bakr Ashraf
 
[ITP - Lecture 11] Loops in C/C++
Muhammad Hammad Waseem
 
Unit 2=Decision Control & Looping Statements.pdf
Dr. Ambedkar Institute of Technology, Bangalore 56
 
CIS 1403 lab 4 selection
Hamad Odhabi
 
PPT_203105211_3.pptx
SaurabhNage1
 
Control statements in java
Madishetty Prathibha
 
Conditional Statement both conditional and loop.pptx
Zenith SVG
 
Operators, control statements represented in java
TharuniDiddekunta
 
Programming Fundamentals lecture 8
REHAN IJAZ
 
Introduction& Overview-to-C++_programming.pptx
divyadhanwani67
 
Introduction to C++ programming language
divyadhanwani67
 
Module 2 - Control Structures c programming.pptm.pdf
SudipDebnath20
 
Control-structure - while loop and do-while loop.pptx
arshadfarhad08
 
DECISION MAKING AND BRANCHING - C Programming
MSridhar18
 
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Priyom Majumder
 
Understanding and using while in c++
MOHANA ALMUQATI
 
Introduction To Programming with Python Lecture 2
Syed Farjad Zia Zaidi
 
UNIT 2 PPT.pdf
DhanushKumar610673
 
Looping statements
Jaya Kumari
 
Ad

More from Mahmoud Alfarra (20)

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

Recently uploaded (20)

PPT
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
PDF
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
PDF
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PPTX
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PPTX
CDH. pptx
AneetaSharma15
 
PDF
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
PPTX
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
CDH. pptx
AneetaSharma15
 
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 

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