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
PPT
6 programming-using-java decision-making20102011-
PPT
Computer Programming, Loops using Java - part 2
PPT
2 programming-using-java how to built application
PPT
8 programming-using-java decision-making practices 20102011
PDF
Function procedure c6 c7
PPTX
Error handling and debugging in vb
PPT
Class 8 Lecture Notes
7 programming-using-java decision-making220102011
6 programming-using-java decision-making20102011-
Computer Programming, Loops using Java - part 2
2 programming-using-java how to built application
8 programming-using-java decision-making practices 20102011
Function procedure c6 c7
Error handling and debugging in vb
Class 8 Lecture Notes

What's hot (20)

PPT
Class 9 Lecture Notes
DOCX
Cis 355 i lab 2 of 6
DOCX
Cis 355 ilab 2 of 6
PDF
Chapter 3 branching v4
PPT
03b loops
PPTX
Exception handling in ASP .NET
PDF
4 coding from algorithms
PPT
F6dc1 session6 c++
PPTX
Intro To C++ - Class 12 - For, do … While
PDF
CP Handout#4
PPT
Exception handling in c++ by manoj vasava
PPT
Lecture 1
PPTX
Pseudocode-Flowchart
PPTX
Factorial of a number in python
PPT
Controlstatment in c
PPTX
Intro To C++ - Class 10 - Control Statements: Part 2
PPSX
Problem solving and design
PPT
Programing Fundamental
PPT
Algorithms
PPTX
control statements of clangauge (ii unit)
Class 9 Lecture Notes
Cis 355 i lab 2 of 6
Cis 355 ilab 2 of 6
Chapter 3 branching v4
03b loops
Exception handling in ASP .NET
4 coding from algorithms
F6dc1 session6 c++
Intro To C++ - Class 12 - For, do … While
CP Handout#4
Exception handling in c++ by manoj vasava
Lecture 1
Pseudocode-Flowchart
Factorial of a number in python
Controlstatment in c
Intro To C++ - Class 10 - Control Statements: Part 2
Problem solving and design
Programing Fundamental
Algorithms
control statements of clangauge (ii unit)
Ad

Similar to Computer Programming, Loops using Java (20)

PPT
Visula C# Programming Lecture 4
PDF
[ITP - Lecture 11] Loops in C/C++
PDF
Unit 2=Decision Control & Looping Statements.pdf
PDF
CIS 1403 lab 4 selection
PPTX
PPT_203105211_3.pptx
PPTX
Control statements in java
PPTX
Conditional Statement both conditional and loop.pptx
PDF
Operators, control statements represented in java
DOCX
Programming Fundamentals lecture 8
PPTX
Introduction& Overview-to-C++_programming.pptx
PPTX
Introduction to C++ programming language
PDF
Module 2 - Control Structures c programming.pptm.pdf
PPTX
Control-structure - while loop and do-while loop.pptx
PPTX
DECISION MAKING AND BRANCHING - C Programming
PDF
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
PPTX
Understanding and using while in c++
PPTX
Introduction To Programming with Python Lecture 2
PDF
UNIT 2 PPT.pdf
PPTX
Looping statements
Visula C# Programming Lecture 4
[ITP - Lecture 11] Loops in C/C++
Unit 2=Decision Control & Looping Statements.pdf
CIS 1403 lab 4 selection
PPT_203105211_3.pptx
Control statements in java
Conditional Statement both conditional and loop.pptx
Operators, control statements represented in java
Programming Fundamentals lecture 8
Introduction& Overview-to-C++_programming.pptx
Introduction to C++ programming language
Module 2 - Control Structures c programming.pptm.pdf
Control-structure - while loop and do-while loop.pptx
DECISION MAKING AND BRANCHING - C Programming
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Understanding and using while in c++
Introduction To Programming with Python Lecture 2
UNIT 2 PPT.pdf
Looping statements
Ad

More from Mahmoud Alfarra (20)

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

Recently uploaded (20)

PPTX
Education and Perspectives of Education.pptx
PDF
Farming Based Livelihood Systems English Notes
PPTX
RIZALS-LIFE-HIGHER-EDUCATION-AND-LIFE-ABROAD.pptx
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PDF
Literature_Review_methods_ BRACU_MKT426 course material
PDF
fundamentals-of-heat-and-mass-transfer-6th-edition_incropera.pdf
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
PDF
Journal of Dental Science - UDMY (2022).pdf
PDF
Empowerment Technology for Senior High School Guide
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PDF
Journal of Dental Science - UDMY (2021).pdf
PDF
MICROENCAPSULATION_NDDS_BPHARMACY__SEM VII_PCI Syllabus.pdf
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PDF
CRP102_SAGALASSOS_Final_Projects_2025.pdf
PDF
plant tissues class 6-7 mcqs chatgpt.pdf
PDF
M.Tech in Aerospace Engineering | BIT Mesra
PDF
Race Reva University – Shaping Future Leaders in Artificial Intelligence
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PDF
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
PPTX
Module on health assessment of CHN. pptx
Education and Perspectives of Education.pptx
Farming Based Livelihood Systems English Notes
RIZALS-LIFE-HIGHER-EDUCATION-AND-LIFE-ABROAD.pptx
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
Literature_Review_methods_ BRACU_MKT426 course material
fundamentals-of-heat-and-mass-transfer-6th-edition_incropera.pdf
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
Journal of Dental Science - UDMY (2022).pdf
Empowerment Technology for Senior High School Guide
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
Journal of Dental Science - UDMY (2021).pdf
MICROENCAPSULATION_NDDS_BPHARMACY__SEM VII_PCI Syllabus.pdf
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
CRP102_SAGALASSOS_Final_Projects_2025.pdf
plant tissues class 6-7 mcqs chatgpt.pdf
M.Tech in Aerospace Engineering | BIT Mesra
Race Reva University – Shaping Future Leaders in Artificial Intelligence
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
Module on health assessment of CHN. pptx

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