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
Ad

More Related Content

What's hot (20)

Class 9 Lecture Notes
Class 9 Lecture NotesClass 9 Lecture Notes
Class 9 Lecture Notes
Stephen Parsons
 
Cis 355 i lab 2 of 6
Cis 355 i lab 2 of 6Cis 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 6Cis 355 ilab 2 of 6
Cis 355 ilab 2 of 6
comp274
 
Chapter 3 branching v4
Chapter 3 branching v4Chapter 3 branching v4
Chapter 3 branching v4
Sunarto Quek
 
03b loops
03b   loops03b   loops
03b loops
Manzoor ALam
 
Exception handling in ASP .NET
Exception handling in ASP .NETException handling in ASP .NET
Exception handling in ASP .NET
baabtra.com - No. 1 supplier of quality freshers
 
4 coding from algorithms
4 coding from algorithms4 coding from algorithms
4 coding from algorithms
hccit
 
F6dc1 session6 c++
F6dc1 session6 c++F6dc1 session6 c++
F6dc1 session6 c++
Mukund Trivedi
 
Intro To C++ - Class 12 - For, do … While
Intro To C++ - Class 12 - For, do … WhileIntro To C++ - Class 12 - For, do … While
Intro To C++ - Class 12 - For, do … While
Blue Elephant Consulting
 
CP Handout#4
CP Handout#4CP Handout#4
CP Handout#4
trupti1976
 
Exception handling in c++ by manoj vasava
Exception handling in c++ by manoj vasavaException handling in c++ by manoj vasava
Exception handling in c++ by manoj vasava
Manoj_vasava
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
Mohammed Saleh
 
Pseudocode-Flowchart
Pseudocode-FlowchartPseudocode-Flowchart
Pseudocode-Flowchart
lotlot
 
Factorial of a number in python
Factorial of a number in pythonFactorial of a number in python
Factorial of a number in python
varshachhajera
 
Controlstatment in c
Controlstatment in cControlstatment 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 2Intro 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 designProblem solving and design
Problem solving and design
Renée Howard-Johnson
 
Programing Fundamental
Programing FundamentalPrograming Fundamental
Programing Fundamental
Qazi Shahzad Ali
 
Algorithms
AlgorithmsAlgorithms
Algorithms
nicky_walters
 
control statements of clangauge (ii unit)
control statements of clangauge (ii unit)control statements of clangauge (ii unit)
control statements of clangauge (ii unit)
Prashant Sharma
 

Similar to Computer Programming, Loops using Java (20)

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

More from Mahmoud Alfarra (20)

Chapter 10: hashing data structure
Chapter 10:  hashing data structureChapter 10:  hashing data structure
Chapter 10: hashing data structure
Mahmoud Alfarra
 
Chapter9 graph data structure
Chapter9  graph data structureChapter9  graph data structure
Chapter9 graph data structure
Mahmoud Alfarra
 
Chapter 8: tree data structure
Chapter 8:  tree data structureChapter 8:  tree data structure
Chapter 8: tree data structure
Mahmoud Alfarra
 
Chapter 7: Queue data structure
Chapter 7:  Queue data structureChapter 7:  Queue data structure
Chapter 7: Queue data structure
Mahmoud Alfarra
 
Chapter 6: stack data structure
Chapter 6:  stack data structureChapter 6:  stack data structure
Chapter 6: stack data structure
Mahmoud Alfarra
 
Chapter 5: linked list data structure
Chapter 5: linked list data structureChapter 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 structureChapter 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 structureChapter 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 structureChapter 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_csChapter1  intro toprincipleofc#_datastructure_b_cs
Chapter1 intro toprincipleofc#_datastructure_b_cs
Mahmoud Alfarra
 
Chapter 0: introduction to data structure
Chapter 0: introduction to data structureChapter 0: introduction to data structure
Chapter 0: introduction to data structure
Mahmoud Alfarra
 
3 classification
3  classification3  classification
3 classification
Mahmoud Alfarra
 
5 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop201020115 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop20102011
Mahmoud Alfarra
 
4 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava201020114 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 computer3 programming-using-java introduction-to computer
3 programming-using-java introduction-to computer
Mahmoud Alfarra
 
1 programming-using-java -introduction
1 programming-using-java -introduction1 programming-using-java -introduction
1 programming-using-java -introduction
Mahmoud Alfarra
 
تلخيص النصوص تلقائيا
تلخيص النصوص تلقائياتلخيص النصوص تلقائيا
تلخيص النصوص تلقائيا
Mahmoud Alfarra
 
4×4×4 لتحصيل التميز
4×4×4 لتحصيل التميز4×4×4 لتحصيل التميز
4×4×4 لتحصيل التميز
Mahmoud Alfarra
 
Data preparation and processing chapter 2
Data preparation and processing chapter  2Data preparation and processing chapter  2
Data preparation and processing chapter 2
Mahmoud Alfarra
 
Introduction to-data-mining chapter 1
Introduction to-data-mining  chapter 1Introduction to-data-mining  chapter 1
Introduction to-data-mining chapter 1
Mahmoud Alfarra
 
Chapter 10: hashing data structure
Chapter 10:  hashing data structureChapter 10:  hashing data structure
Chapter 10: hashing data structure
Mahmoud Alfarra
 
Chapter9 graph data structure
Chapter9  graph data structureChapter9  graph data structure
Chapter9 graph data structure
Mahmoud Alfarra
 
Chapter 8: tree data structure
Chapter 8:  tree data structureChapter 8:  tree data structure
Chapter 8: tree data structure
Mahmoud Alfarra
 
Chapter 7: Queue data structure
Chapter 7:  Queue data structureChapter 7:  Queue data structure
Chapter 7: Queue data structure
Mahmoud Alfarra
 
Chapter 6: stack data structure
Chapter 6:  stack data structureChapter 6:  stack data structure
Chapter 6: stack data structure
Mahmoud Alfarra
 
Chapter 5: linked list data structure
Chapter 5: linked list data structureChapter 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 structureChapter 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 structureChapter 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 structureChapter 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_csChapter1  intro toprincipleofc#_datastructure_b_cs
Chapter1 intro toprincipleofc#_datastructure_b_cs
Mahmoud Alfarra
 
Chapter 0: introduction to data structure
Chapter 0: introduction to data structureChapter 0: introduction to data structure
Chapter 0: introduction to data structure
Mahmoud Alfarra
 
5 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop201020115 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop20102011
Mahmoud Alfarra
 
4 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava201020114 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 computer3 programming-using-java introduction-to computer
3 programming-using-java introduction-to computer
Mahmoud Alfarra
 
1 programming-using-java -introduction
1 programming-using-java -introduction1 programming-using-java -introduction
1 programming-using-java -introduction
Mahmoud Alfarra
 
تلخيص النصوص تلقائيا
تلخيص النصوص تلقائياتلخيص النصوص تلقائيا
تلخيص النصوص تلقائيا
Mahmoud Alfarra
 
4×4×4 لتحصيل التميز
4×4×4 لتحصيل التميز4×4×4 لتحصيل التميز
4×4×4 لتحصيل التميز
Mahmoud Alfarra
 
Data preparation and processing chapter 2
Data preparation and processing chapter  2Data preparation and processing chapter  2
Data preparation and processing chapter 2
Mahmoud Alfarra
 
Introduction to-data-mining chapter 1
Introduction to-data-mining  chapter 1Introduction to-data-mining  chapter 1
Introduction to-data-mining chapter 1
Mahmoud Alfarra
 
Ad

Recently uploaded (20)

Kenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 CohortKenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 Cohort
EducationNC
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
"Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules""Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules"
rupalinirmalbpharm
 
Grade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable WorksheetGrade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable Worksheet
Sritoma Majumder
 
Sugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptxSugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptx
Dr. Renu Jangid
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdfIntroduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
james5028
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Link your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRMLink your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRM
Celine George
 
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx   quiz by Ridip HazarikaTHE STG QUIZ GROUP D.pptx   quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
Ridip Hazarika
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Herbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptxHerbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptx
RAJU THENGE
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Contact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: OptometryContact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: Optometry
MushahidRaza8
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
APM Midlands Region April 2025 Sacha Hind Circulated.pdf
APM Midlands Region April 2025 Sacha Hind Circulated.pdfAPM Midlands Region April 2025 Sacha Hind Circulated.pdf
APM Midlands Region April 2025 Sacha Hind Circulated.pdf
Association for Project Management
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
Kenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 CohortKenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 Cohort
EducationNC
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
"Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules""Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules"
rupalinirmalbpharm
 
Grade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable WorksheetGrade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable Worksheet
Sritoma Majumder
 
Sugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptxSugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptx
Dr. Renu Jangid
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdfIntroduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
james5028
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Link your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRMLink your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRM
Celine George
 
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx   quiz by Ridip HazarikaTHE STG QUIZ GROUP D.pptx   quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
Ridip Hazarika
 
Herbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptxHerbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptx
RAJU THENGE
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Contact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: OptometryContact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: Optometry
MushahidRaza8
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 

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