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

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 6helpido9
 
Cis 355 ilab 2 of 6
Cis 355 ilab 2 of 6Cis 355 ilab 2 of 6
Cis 355 ilab 2 of 6comp274
 
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 algorithmshccit
 
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-Flowchartlotlot
 
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
 
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
 

What's hot (20)

Class 9 Lecture Notes
Class 9 Lecture NotesClass 9 Lecture Notes
Class 9 Lecture Notes
 
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
 
Cis 355 ilab 2 of 6
Cis 355 ilab 2 of 6Cis 355 ilab 2 of 6
Cis 355 ilab 2 of 6
 
Chapter 3 branching v4
Chapter 3 branching v4Chapter 3 branching v4
Chapter 3 branching v4
 
03b loops
03b   loops03b   loops
03b loops
 
Exception handling in ASP .NET
Exception handling in ASP .NETException handling in ASP .NET
Exception handling in ASP .NET
 
4 coding from algorithms
4 coding from algorithms4 coding from algorithms
4 coding from algorithms
 
F6dc1 session6 c++
F6dc1 session6 c++F6dc1 session6 c++
F6dc1 session6 c++
 
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
 
CP Handout#4
CP Handout#4CP Handout#4
CP Handout#4
 
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
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Pseudocode-Flowchart
Pseudocode-FlowchartPseudocode-Flowchart
Pseudocode-Flowchart
 
Factorial of a number in python
Factorial of a number in pythonFactorial of a number in python
Factorial of a number in python
 
Controlstatment in c
Controlstatment in cControlstatment in c
Controlstatment in c
 
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
 
Problem solving and design
Problem solving and designProblem solving and design
Problem solving and design
 
Programing Fundamental
Programing FundamentalPrograming Fundamental
Programing Fundamental
 
Algorithms
AlgorithmsAlgorithms
Algorithms
 
control statements of clangauge (ii unit)
control statements of clangauge (ii unit)control statements of clangauge (ii unit)
control statements of clangauge (ii unit)
 

Similar to Computer Programming, Loops using Java

Visula C# Programming Lecture 4
Visula C# Programming Lecture 4Visula C# Programming Lecture 4
Visula C# Programming Lecture 4
Abou Bakr Ashraf
 
[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
 
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
 
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
 
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
 
banaz hilmy.pptx
banaz hilmy.pptxbanaz hilmy.pptx
banaz hilmy.pptx
BaNaz8
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
JayBhavsar68
 
What is loops? What is For loop?
What is loops? What is For loop?What is loops? What is For loop?
What is loops? What is For loop?
AnuragSrivastava272
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
Saad Sheikh
 

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
 
Ch05
Ch05Ch05
Ch05
 
[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++
 
CIS 1403 lab 4 selection
CIS 1403 lab 4 selectionCIS 1403 lab 4 selection
CIS 1403 lab 4 selection
 
PPT_203105211_3.pptx
PPT_203105211_3.pptxPPT_203105211_3.pptx
PPT_203105211_3.pptx
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
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
 
Operators, control statements represented in java
Operators, control statements  represented in javaOperators, control statements  represented in java
Operators, control statements represented in java
 
Programming Fundamentals lecture 8
Programming Fundamentals lecture 8Programming Fundamentals lecture 8
Programming Fundamentals lecture 8
 
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
 
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
 
Understanding and using while in c++
Understanding and using while in c++Understanding and using while in c++
Understanding and using while in c++
 
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
 
UNIT 2 PPT.pdf
UNIT 2 PPT.pdfUNIT 2 PPT.pdf
UNIT 2 PPT.pdf
 
Looping statements
Looping statementsLooping statements
Looping statements
 
banaz hilmy.pptx
banaz hilmy.pptxbanaz hilmy.pptx
banaz hilmy.pptx
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
 
What is loops? What is For loop?
What is loops? What is For loop?What is loops? What is For loop?
What is loops? What is For loop?
 
LOOPS AND DECISIONS
LOOPS AND DECISIONSLOOPS AND DECISIONS
LOOPS AND DECISIONS
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 

More from 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
 
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
 

More from Mahmoud Alfarra (20)

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

Recently uploaded

June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 

Recently uploaded (20)

June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 

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