0% found this document useful (0 votes)
49 views

Java For Android: Indefinite Loops: Julie L. Johnson

The document discusses indefinite loops in Java for Android applications. It introduces the while and do-while loops, which allow code to repeat indefinitely as long as a logical test condition remains true. The while loop checks the condition before executing the loop body, while the do-while loop checks after executing the body at least once. Both are useful when the number of iterations is unknown beforehand, such as when prompting a user for input until a correct response.

Uploaded by

neeraj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Java For Android: Indefinite Loops: Julie L. Johnson

The document discusses indefinite loops in Java for Android applications. It introduces the while and do-while loops, which allow code to repeat indefinitely as long as a logical test condition remains true. The while loop checks the condition before executing the loop body, while the do-while loop checks after executing the body at least once. Both are useful when the number of iterations is unknown beforehand, such as when prompting a user for input until a correct response.

Uploaded by

neeraj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Java for Android:

Indefinite Loops
Julie L. Johnson
[email protected]
Java for Android
What if?
•  What if I want to continually reduce some number n by 3
until the number is less than 19? How many times should
my loop execute?

•  What if I want to ask the user to keep guessing a number,


and keep asking them until they guess correctly? How
can I make this loop keep running?

2
Java for Android
a loop that can run indefinitely

test false

true
executable executable
statements statements

3
Java for Android
the while loop: syntax
while loop: Repeatedly executes its
body as long as a logical test is true.
while (test) {
statement(s); false
test
}
true
statements statements

4
Java for Android
the while loop: an example

while (num > 19) {


num = num - 3; num>19 false
}
out.print(num); true
num = out.print(num)
num-3

5
Java for Android
the while loop and the for loop
The while loop can do all that the for loop can do

int total = 0; int total = 0;


for (int i = 0; i<5; i++){ int i = 0;
total = total + i; while (i < 5){
} total = total + i;
i++;
}

6
Java for Android
the while loop and the for loop
The for loop can do all that the while loop can do
but in a very clumsy way

for (int i = 0; i>=0; i++){ while (num > 19){


num = num -3; num = num – 3;
if (num <= 19){ }
i = -2;
}
}
must set i to -2, when i++ is executed before it is tested again,
it will be equal to -1 and the loop will terminate

7
Java for Android
Adopt the practice of using a while loop when the
number of iterations is not known before you begin

•  an indefinite loop
false
•  best used when the number test
of iteration is unknown true
executable executable
statements statements

8
Java for Android
Notice that the syntax is similar to an if statement
but the result is very different!
int total = 0; int total = 0;
if (total < 5) while (total < 5){
total = total + cost; total = total + cost;
} }

total<5 false total<5 false

true true
total = total =
total + cost total + cost

9
Java for Android
while loops are useful when testing user input

Out.print(“Enter a number between 0 and 5”)


int num = In.nextInt();
while ((num <0) || (num >5)){
Out.print(“Your number is out of range.”);
Out.print(“please try again: ”);
num = In.nextInt();
}

10
Java for Android

Checking user input


read input

•  notice there was some


false
redundancy in the code while not valid

•  this can be avoided true


read input

11
Java for Android
the do while loop

statement
•  an indefinite loop
true
•  best used when the number test
of iteration is unknown
•  use when you will execute false
the loop at least once

12
Java for Android
the do while loop: syntax
do while loop: executes its body and continues doing so as
long as a logical test is true.
do { statement
statement(s);
true
}while (test) ;
test

false

13
Java for Android
The do while loop: an example
if (num > 3){
i = 1;
Out.print(“Positive multiples of 3 less than the input”);
do{
Out.print(3*i);
statement
Out.print(“ ”);
i++; statement
}while (3*i < num);
true
}
test

false
14
Java for Android
indefinite loop constructs

false statement
test
true true
statements test

false
statements
statements

15

You might also like