Lecture 5 - Loops
Lecture 5 - Loops
Loops
1
Outline
System.out.println("Welcome to Java!");
Problem:
System.out.println("Welcome to Java!");
System.out.println("Welcome to Java!");
System.out.println("Welcome to Java!");
System.out.println("Welcome to Java!");
System.out.println("Welcome to Java!");
System.out.println("Welcome to Java!");
1000 …
times …
…
…
…
System.out.println("Welcome to Java!");
System.out.println("Welcome to Java!");
System.out.println("Welcome to Java!");
int count = 0;
while (count < 1000)
{
System.out.println("Welcome to Java!");
count++;
}
The while and do-while loops are also called conditional loops since
they use boolean expressions to control the loop behavior
The while and do-while loops run un-determined (unknown) number
of iterations (some call them non-deterministic loops)
The for loop, on the other hand, runs a pre-determined (known)
number of iterations (some call it deterministic loop or counting loop)
while (condition)
statement block; //loop body
• If the condition is true, the statement block is
executed
condition
evaluated
Note: If the initial evaluation
of the condition is false,
true the loop body executes
false
zero times. Therefore, the
Statement while loop executes zero or
block more times
(loop body)
Next Line
Initialize count
int count = 0;
while (count < 2) {
System.out.println("Welcome to Java!");
count++;
}
Increase count by 1
int count = 0; count is 1 now
Increase count by 1
int count = 0; count is 2 now
int count = 1;
while (count <= 5)
{
System.out.println (count);
count = count + 1;
}
• If the condition is false initially, the statement
(loop body) is never executed
• Therefore, the body of a while loop will execute
zero or more times
A sentinel value is a special input value that represents the end of inputs
from the user
The sentinel value should be included in the prompt so that the user knows
how to stop the loop. For example,
System.out.println(“Enter a grade (type 9999 to quit):
”);
A sentinel value gives the user control over the loop
See Average.java next slide
do
{
statement block;
} while (condition)
Statement condition
Block evaluated
Loop body
true false
true
Statement
block
condition
evaluated
While Loop
false
Next Line
An example of a do loop:
int count = 0;
do
{
count = count +1;
System.out.println (count);
} while (count < 5);
do
{
lastDigit = number % 10;
reverse = (reverse * 10) + lastDigit;
number = number / 10;
} while (number > 0);
While Loop
initialization condition
evaluated
statement block
Like a while loop, the condition of a for
increment loop is tested prior to executing the loop
body. Therefore, the for loop body will
execute zero or more times
Declare i
int i;
for (i = 0; i < 2; i++) {
System.out.println("Welcome to Java!");
}
Execute initializer
i is now 0
int i;
for (i = 0; i < 2; i++) {
System.out.println("Welcome to Java!");
}
(i < 2) is true
since i is 0
int i;
for (i = 0; i < 2; i++) {
System.out.println("Welcome to Java!");
}
int i;
for (i = 0; i < 2; i++) {
System.out.println("Welcome to
Java!");
}
int i;
for (i = 0; i < 2; i++) {
System.out.println("Welcome to
Java!");
}
(i < 2) is false
since i is 2
int i;
for (i = 0; i < 2; i++) {
System.out.println("Welcome to
Java!");
}
initialization;
while (condition)
{
statement block;
increment;
}
int count = 1;
while (count <= 5)
{
System.out.println (count);
count = count + 1;
}
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
for Loop Example
The body of a while loop eventually must make the condition false
If not, it is called an infinite loop, which will execute until the user
interrupts the program
This is a common logical error
You should always double check the logic of a program to ensure that
your loops will terminate normally
int count = 1;
while (count <= 25)
{
System.out.println (count);
count = count - 1; //Error
}
That is, the body of a loop can contain other loop statements
For each iteration of the outer loop, the inner loop iterates completely
int count1 = 1;
while (count1 <= 10)
{
int count2 = 1;
while (count2 <= 5)
{
System.out.println("I am here!");
count2 = count2 + 1;
}
System.out.println(); // blank line
count1 = count1 + 1;
}
}
} Md. Morshed Ali, Lecturer, Dept. of CSE, UU
7. Using break and continue
}
}
Md. Morshed Ali, Lecturer, Dept. of CSE, UU
End of Lecture 5
Thank You
55