SlideShare a Scribd company logo
Lecture#5
Aleeha Iftikhar
Repetitions Statements
 while Loops
 do-while Loops
 for Loops
 break and continue
The while Statement
 Syntax for the while Statement
 while ( <boolean expression> )
<statement>
while ( number <= 100 ) {
sum = sum + number;
number = number + 1;
}
Statement
(loop body)
Boolean Expression
Control Flow of while
int sum = 0, number = 1
number <= 100 ?
false
sum = sum + number;
number = number + 1;
true
int age;
Scanner scanner = new Scanner(System.in);
System.out.print("Your Age (between 0 and 130): ");
age = scanner.nextInt( );
while (age < 0 || age > 130) {
System.out.println(
"An invalid age was entered. Please try again.");
System.out.print("Your Age (between 0 and 130): ");
age = scanner.nextInt( );
}
Example: Testing Input Data
Priming Read
For Integer input
Caution
 Don’t use floating-point values for
equality checking in a loop control. Since
floating-point values are approximations,
using them could result in imprecise
counter values and inaccurate results.
This example uses int value for data. If a
floating-point type value is used for
data, (data != 0) may be true even though
data is 0.
 Make sure the loop body contains a statement that will
eventually cause the loop to terminate.
 Make sure the loop repeats exactly the correct number of
times.
 If you want to execute the loop body N times, then
initialize the counter to 0 and use the test condition
counter < N or initialize the counter to 1 and use the test
Loop Pitfall - 1
Infinite Loops
Both loops will not
terminate because the
boolean expressions will
never become false.int count = 1;
while ( count != 10 ) {
count = count + 2;
}
2
int product = 0;
while ( product < 500000 ) {
product = product * 5;
}
1
Loop Pitfall - 2
• Goal: Execute the loop body 10 times.
count = 1;
while ( count < 10 ){
. . .
count++;
}
1
count = 0;
while ( count <= 10 ){
. . .
count++;
}
3
count = 1;
while ( count <= 10 ){
. . .
count++;
}
2
count = 0;
while ( count < 10 ){
. . .
count++;
}
4
The do-while Statement
do {
sum += number;
number++;
} while ( sum <= 1000000 );
do
<statement>
while ( <boolean expression> ) ;
Statement
(loop body)
Boolean Expression
Control Flow of do-while
int sum = 0, number = 1
sum += number;
number++;
sum <= 1000000 ?
true
false
The for Statement
for ( i = 0 ; i < 20 ; i++ ) {
number = scanner.nextInt();
sum += number;
}
for ( <initialization>; <boolean expression>; <increment> )
<statement>
Initialization
Boolean
Expression
Increment
Statement
(loop body)
The for Statement
int i, sum = 0, number;
for (i = 0; i < 20; i++) {
number = scanner.nextInt( );
sum += number;
}
These statements are
executed for 20 times
( i = 0, 1, 2, … , 19).
Control Flow of for
i = 0;
false
number = . . . ;
sum += number;
true
i ++;
i < 20 ?
More for Loop Examples
for (int i = 0; i < 100; i += 5)1
i = 0, 5, 10, … , 95
for (int j = 2; j < 40; j *= 2)2
j = 2, 4, 8, 16, 32
for (int k = 100; k > 0; k--) )3
k = 100, 99, 98, 97, ..., 1
Which Loop to Use?
 The three forms of loop statements, while, do, and for, are
expressively equivalent; that is, you can write a loop in any
of these three forms.
 I recommend that you use the one that is most intuitive
and comfortable for you. In general, a for loop may be used
if the number of repetitions is known, as, for example,
when you need to print a message 100 times. A while loop
may be used if the number of repetitions is not known, as
in the case of reading the numbers until the input is 0. A
do-while loop can be used to replace a while loop if the
loop body has to be executed before testing the
continuation condition.
Caution
Adding a semicolon at the end of the for clause
before the loop body is a common mistake, as shown
below:
for (int i=0; i<10; i++);
{
System.out.println("i is " + i);
}
Similarly, the following loop is also wrong:
int i=0;
while (i<10);
{
System.out.println("i is " + i);
i++;
}
In the case of the do loop, the following semicolon is
needed to end the loop.
int i=0;
do {
System.out.println("i is " + i);
i++;
} while (i<10);
Wrong
Correct
Loop-and-a-Half Repetition Control
• Loop-and-a-half repetition control can be
used to test a loop’s terminating condition in
the middle of the loop body.
• It is implemented by using reserved words
while, if, and break.
Example: Loop-and-a-Half Control
String name;
Scanner scanner = new Scanner(System.in);
while (true){
System.out.print("Your name“);
name = scanner.next( );
if (name.length() > 0) break;
System.out.println("Invalid Entry." +
"You must enter at least one character.");
}
Example: Loop-and-a-Half Control
String name;
Scanner scanner = new Scanner(System.in);
while (true){
System.out.print("Your name“);
name = scanner.next( );
if (name.length() > 0) break;
System.out.println("Invalid Entry." +
"You must enter at least one character.");
}
The break Keyword
false
true
Statement(s)
Next
Statement
Continuation
condition?
Statement(s)
break
The continue Keyword
false
true
Statement(s)
Next
Statement
Continue
condition?
Statement(s)
continue
Chapter 4 Methods
 Introducing Methods
 Benefits of methods, Declaring Methods, and Calling Methods
 Passing Parameters
 Pass by Value
Introducing Methods
Method Structure
A method is a
collection of
statements that
are grouped
together to
perform an
operation.
Introducing Methods, cont.
•parameter profile refers to the
type, order, and number of the
parameters of a method.
•method signature is the
combination of the method name and
the parameter profiles.
•The parameters defined in the
method header are known as formal
parameters.
•When a method is invoked, its
DeclaringDefinning Methods
public static int max(int num1, int num2)
{
if (num1 > num2)
return num1;
else
return num2;
}
Calling Methods, cont.
public static void main(String[] args) {
int i = 5;
int j = 2;
int k = max(i, j);
System.out.println(
"The maximum between " + i +
" and " + j + " is " + k);
}
public static int max(int num1, int num2) {
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
pass i
pass j
Calling Methods, cont.
The main method
i:
j:
k:
The max method
num1:
num2:
result:
pass 5
5
2
5
5
2
5
pass 2
parameters
THE END

More Related Content

What's hot

Python Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and LoopsPython Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and Loops
P3 InfoTech Solutions Pvt. Ltd.
 
Repetition Structure
Repetition StructureRepetition Structure
Repetition Structure
PRN USM
 
Control statements
Control statementsControl statements
Control statements
Kanwalpreet Kaur
 
conditional statements
conditional statementsconditional statements
conditional statements
James Brotsos
 
Loop c++
Loop c++Loop c++
Loop c++
Mood Mood
 
Loop control in c++
Loop control in c++Loop control in c++
Loop control in c++
Debre Tabor University
 
Working of while loop
Working of while loopWorking of while loop
Working of while loop
Neeru Mittal
 
C++ loop
C++ loop C++ loop
C++ loop
Khelan Ameen
 
Conditional statements in vb script
Conditional statements in vb scriptConditional statements in vb script
Conditional statements in vb script
Nilanjan Saha
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
Anil Dutt
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
Mohammed Khan
 
Iteration
IterationIteration
Iteration
Liam Dunphy
 
Loops in java script
Loops in java scriptLoops in java script
Loops in java script
Ravi Bhadauria
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loopsbsdeol28
 
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshare
Gagan Deep
 

What's hot (20)

Looping
LoopingLooping
Looping
 
Python Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and LoopsPython Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and Loops
 
Repetition Structure
Repetition StructureRepetition Structure
Repetition Structure
 
Control statements
Control statementsControl statements
Control statements
 
Java loops
Java loopsJava loops
Java loops
 
conditional statements
conditional statementsconditional statements
conditional statements
 
Loop c++
Loop c++Loop c++
Loop c++
 
Loop control in c++
Loop control in c++Loop control in c++
Loop control in c++
 
Working of while loop
Working of while loopWorking of while loop
Working of while loop
 
C++ loop
C++ loop C++ loop
C++ loop
 
Flow of control ppt
Flow of control pptFlow of control ppt
Flow of control ppt
 
Control structures
Control structuresControl structures
Control structures
 
Conditional statements in vb script
Conditional statements in vb scriptConditional statements in vb script
Conditional statements in vb script
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Iteration
IterationIteration
Iteration
 
Loops in java script
Loops in java scriptLoops in java script
Loops in java script
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
 
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshare
 

Similar to 130707833146508191

Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6Vince Vo
 
130706266060138191
130706266060138191130706266060138191
130706266060138191
Tanzeel Ahmad
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
Saad Sheikh
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
altwirqi
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)jahanullah
 
06 Loops
06 Loops06 Loops
06 Loops
maznabili
 
Programming loop
Programming loopProgramming loop
Programming loop
University of Potsdam
 
Loops
LoopsLoops
Loops
Kamran
 
06.Loops
06.Loops06.Loops
06.Loops
Intro C# Book
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
Tanmay Modi
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
tanmaymodi4
 
Lec7 - Loops updated.pptx
Lec7 - Loops updated.pptxLec7 - Loops updated.pptx
Lec7 - Loops updated.pptx
NaumanRasheed11
 
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
Neeru Mittal
 
4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf
ShifatiRabbi
 
Oop object oriented programing topics
Oop object oriented programing topicsOop object oriented programing topics
Oop object oriented programing topics
(•̮̮̃•̃) Prince Do Not Work
 
Repetition Structure.pptx
Repetition Structure.pptxRepetition Structure.pptx
Repetition Structure.pptx
rhiene05
 
SPL 8 | Loop Statements in C
SPL 8 | Loop Statements in CSPL 8 | Loop Statements in C
SPL 8 | Loop Statements in C
Mohammad Imam Hossain
 
LECTURE 3 LOOPS, ARRAYS.pdf
LECTURE 3 LOOPS, ARRAYS.pdfLECTURE 3 LOOPS, ARRAYS.pdf
LECTURE 3 LOOPS, ARRAYS.pdf
SHASHIKANT346021
 

Similar to 130707833146508191 (20)

Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
 
130706266060138191
130706266060138191130706266060138191
130706266060138191
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
 
06 Loops
06 Loops06 Loops
06 Loops
 
Programming loop
Programming loopProgramming loop
Programming loop
 
Loops
LoopsLoops
Loops
 
06.Loops
06.Loops06.Loops
06.Loops
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
ICP - Lecture 9
ICP - Lecture 9ICP - Lecture 9
ICP - Lecture 9
 
Lec7 - Loops updated.pptx
Lec7 - Loops updated.pptxLec7 - Loops updated.pptx
Lec7 - Loops updated.pptx
 
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
 
4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf
 
Oop object oriented programing topics
Oop object oriented programing topicsOop object oriented programing topics
Oop object oriented programing topics
 
Repetition Structure.pptx
Repetition Structure.pptxRepetition Structure.pptx
Repetition Structure.pptx
 
SPL 8 | Loop Statements in C
SPL 8 | Loop Statements in CSPL 8 | Loop Statements in C
SPL 8 | Loop Statements in C
 
LECTURE 3 LOOPS, ARRAYS.pdf
LECTURE 3 LOOPS, ARRAYS.pdfLECTURE 3 LOOPS, ARRAYS.pdf
LECTURE 3 LOOPS, ARRAYS.pdf
 
Java presentation
Java presentationJava presentation
Java presentation
 

More from Tanzeel Ahmad

Signal and systems solution manual 2ed a v oppenheim a s willsky - prentic...
Signal and systems  solution manual 2ed   a v oppenheim a s willsky - prentic...Signal and systems  solution manual 2ed   a v oppenheim a s willsky - prentic...
Signal and systems solution manual 2ed a v oppenheim a s willsky - prentic...
Tanzeel Ahmad
 
Simulation of sinosoidal pulse width modulation
Simulation of sinosoidal pulse width modulationSimulation of sinosoidal pulse width modulation
Simulation of sinosoidal pulse width modulation
Tanzeel Ahmad
 
Discrete Time Signal Processing Oppenhm book 2nd
Discrete Time Signal Processing Oppenhm book 2nd Discrete Time Signal Processing Oppenhm book 2nd
Discrete Time Signal Processing Oppenhm book 2nd
Tanzeel Ahmad
 
Automobile cruise control
Automobile cruise controlAutomobile cruise control
Automobile cruise control
Tanzeel Ahmad
 
130717666736980000
130717666736980000130717666736980000
130717666736980000
Tanzeel Ahmad
 
130704798265658191
130704798265658191130704798265658191
130704798265658191
Tanzeel Ahmad
 
130700548484460000
130700548484460000130700548484460000
130700548484460000
Tanzeel Ahmad
 
indus valley civilization
indus valley civilizationindus valley civilization
indus valley civilization
Tanzeel Ahmad
 

More from Tanzeel Ahmad (10)

Signal and systems solution manual 2ed a v oppenheim a s willsky - prentic...
Signal and systems  solution manual 2ed   a v oppenheim a s willsky - prentic...Signal and systems  solution manual 2ed   a v oppenheim a s willsky - prentic...
Signal and systems solution manual 2ed a v oppenheim a s willsky - prentic...
 
Simulation of sinosoidal pulse width modulation
Simulation of sinosoidal pulse width modulationSimulation of sinosoidal pulse width modulation
Simulation of sinosoidal pulse width modulation
 
Discrete Time Signal Processing Oppenhm book 2nd
Discrete Time Signal Processing Oppenhm book 2nd Discrete Time Signal Processing Oppenhm book 2nd
Discrete Time Signal Processing Oppenhm book 2nd
 
Automobile cruise control
Automobile cruise controlAutomobile cruise control
Automobile cruise control
 
130717666736980000
130717666736980000130717666736980000
130717666736980000
 
130704798265658191
130704798265658191130704798265658191
130704798265658191
 
130700548484460000
130700548484460000130700548484460000
130700548484460000
 
indus valley civilization
indus valley civilizationindus valley civilization
indus valley civilization
 
130553713037500000
130553713037500000130553713037500000
130553713037500000
 
130553704223906250
130553704223906250130553704223906250
130553704223906250
 

Recently uploaded

Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
aqil azizi
 
Fundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptxFundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptx
manasideore6
 
Swimming pool mechanical components design.pptx
Swimming pool  mechanical components design.pptxSwimming pool  mechanical components design.pptx
Swimming pool mechanical components design.pptx
yokeleetan1
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
Dr Ramhari Poudyal
 
Water billing management system project report.pdf
Water billing management system project report.pdfWater billing management system project report.pdf
Water billing management system project report.pdf
Kamal Acharya
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
drwaing
 
Series of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.pptSeries of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.ppt
PauloRodrigues104553
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
SyedAbiiAzazi1
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
camseq
 
Ethernet Routing and switching chapter 1.ppt
Ethernet Routing and switching chapter 1.pptEthernet Routing and switching chapter 1.ppt
Ethernet Routing and switching chapter 1.ppt
azkamurat
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
Kerry Sado
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 
Online aptitude test management system project report.pdf
Online aptitude test management system project report.pdfOnline aptitude test management system project report.pdf
Online aptitude test management system project report.pdf
Kamal Acharya
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 

Recently uploaded (20)

Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
 
Fundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptxFundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptx
 
Swimming pool mechanical components design.pptx
Swimming pool  mechanical components design.pptxSwimming pool  mechanical components design.pptx
Swimming pool mechanical components design.pptx
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
 
Water billing management system project report.pdf
Water billing management system project report.pdfWater billing management system project report.pdf
Water billing management system project report.pdf
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
 
Series of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.pptSeries of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.ppt
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
 
Ethernet Routing and switching chapter 1.ppt
Ethernet Routing and switching chapter 1.pptEthernet Routing and switching chapter 1.ppt
Ethernet Routing and switching chapter 1.ppt
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 
Online aptitude test management system project report.pdf
Online aptitude test management system project report.pdfOnline aptitude test management system project report.pdf
Online aptitude test management system project report.pdf
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 

130707833146508191

  • 2. Repetitions Statements  while Loops  do-while Loops  for Loops  break and continue
  • 3. The while Statement  Syntax for the while Statement  while ( <boolean expression> ) <statement> while ( number <= 100 ) { sum = sum + number; number = number + 1; } Statement (loop body) Boolean Expression
  • 4. Control Flow of while int sum = 0, number = 1 number <= 100 ? false sum = sum + number; number = number + 1; true
  • 5. int age; Scanner scanner = new Scanner(System.in); System.out.print("Your Age (between 0 and 130): "); age = scanner.nextInt( ); while (age < 0 || age > 130) { System.out.println( "An invalid age was entered. Please try again."); System.out.print("Your Age (between 0 and 130): "); age = scanner.nextInt( ); } Example: Testing Input Data Priming Read For Integer input
  • 6. Caution  Don’t use floating-point values for equality checking in a loop control. Since floating-point values are approximations, using them could result in imprecise counter values and inaccurate results. This example uses int value for data. If a floating-point type value is used for data, (data != 0) may be true even though data is 0.  Make sure the loop body contains a statement that will eventually cause the loop to terminate.  Make sure the loop repeats exactly the correct number of times.  If you want to execute the loop body N times, then initialize the counter to 0 and use the test condition counter < N or initialize the counter to 1 and use the test
  • 7. Loop Pitfall - 1 Infinite Loops Both loops will not terminate because the boolean expressions will never become false.int count = 1; while ( count != 10 ) { count = count + 2; } 2 int product = 0; while ( product < 500000 ) { product = product * 5; } 1
  • 8. Loop Pitfall - 2 • Goal: Execute the loop body 10 times. count = 1; while ( count < 10 ){ . . . count++; } 1 count = 0; while ( count <= 10 ){ . . . count++; } 3 count = 1; while ( count <= 10 ){ . . . count++; } 2 count = 0; while ( count < 10 ){ . . . count++; } 4
  • 9. The do-while Statement do { sum += number; number++; } while ( sum <= 1000000 ); do <statement> while ( <boolean expression> ) ; Statement (loop body) Boolean Expression
  • 10. Control Flow of do-while int sum = 0, number = 1 sum += number; number++; sum <= 1000000 ? true false
  • 11. The for Statement for ( i = 0 ; i < 20 ; i++ ) { number = scanner.nextInt(); sum += number; } for ( <initialization>; <boolean expression>; <increment> ) <statement> Initialization Boolean Expression Increment Statement (loop body)
  • 12. The for Statement int i, sum = 0, number; for (i = 0; i < 20; i++) { number = scanner.nextInt( ); sum += number; } These statements are executed for 20 times ( i = 0, 1, 2, … , 19).
  • 13. Control Flow of for i = 0; false number = . . . ; sum += number; true i ++; i < 20 ?
  • 14. More for Loop Examples for (int i = 0; i < 100; i += 5)1 i = 0, 5, 10, … , 95 for (int j = 2; j < 40; j *= 2)2 j = 2, 4, 8, 16, 32 for (int k = 100; k > 0; k--) )3 k = 100, 99, 98, 97, ..., 1
  • 15. Which Loop to Use?  The three forms of loop statements, while, do, and for, are expressively equivalent; that is, you can write a loop in any of these three forms.  I recommend that you use the one that is most intuitive and comfortable for you. In general, a for loop may be used if the number of repetitions is known, as, for example, when you need to print a message 100 times. A while loop may be used if the number of repetitions is not known, as in the case of reading the numbers until the input is 0. A do-while loop can be used to replace a while loop if the loop body has to be executed before testing the continuation condition.
  • 16. Caution Adding a semicolon at the end of the for clause before the loop body is a common mistake, as shown below: for (int i=0; i<10; i++); { System.out.println("i is " + i); } Similarly, the following loop is also wrong: int i=0; while (i<10); { System.out.println("i is " + i); i++; } In the case of the do loop, the following semicolon is needed to end the loop. int i=0; do { System.out.println("i is " + i); i++; } while (i<10); Wrong Correct
  • 17. Loop-and-a-Half Repetition Control • Loop-and-a-half repetition control can be used to test a loop’s terminating condition in the middle of the loop body. • It is implemented by using reserved words while, if, and break.
  • 18. Example: Loop-and-a-Half Control String name; Scanner scanner = new Scanner(System.in); while (true){ System.out.print("Your name“); name = scanner.next( ); if (name.length() > 0) break; System.out.println("Invalid Entry." + "You must enter at least one character."); }
  • 19. Example: Loop-and-a-Half Control String name; Scanner scanner = new Scanner(System.in); while (true){ System.out.print("Your name“); name = scanner.next( ); if (name.length() > 0) break; System.out.println("Invalid Entry." + "You must enter at least one character."); }
  • 22. Chapter 4 Methods  Introducing Methods  Benefits of methods, Declaring Methods, and Calling Methods  Passing Parameters  Pass by Value
  • 23. Introducing Methods Method Structure A method is a collection of statements that are grouped together to perform an operation.
  • 24. Introducing Methods, cont. •parameter profile refers to the type, order, and number of the parameters of a method. •method signature is the combination of the method name and the parameter profiles. •The parameters defined in the method header are known as formal parameters. •When a method is invoked, its
  • 25. DeclaringDefinning Methods public static int max(int num1, int num2) { if (num1 > num2) return num1; else return num2; }
  • 26. Calling Methods, cont. public static void main(String[] args) { int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); } public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } pass i pass j
  • 27. Calling Methods, cont. The main method i: j: k: The max method num1: num2: result: pass 5 5 2 5 5 2 5 pass 2 parameters