KEMBAR78
Introduction to Java Programming - Lecture 11.pptx
Introduction to Java Programming
Lecture 11: Repetition Statements (Loops)
Ahmad Jawid Jami
Outline
The while Statement
The do while
Statement The for
Statement
Repetition Statements
• Repetition statements allow us to execute a statement multiple times
• Often they are referred to as loops
• Like conditional statements, they are controlled by boolean expressions
• Java has three kinds of repetition statements:
• the while loop
• the do loop
• the for loop
• The programmer should choose the right kind of loop for the situation
The while Statement
• A while statement has the following syntax:
while ( condition )
statement;
• If the condition is true, the statement is
executed
• Then the condition is evaluated again, and if it is
still true, the statement is executed again
• The statement is executed repeatedly until the
condition becomes false
Logic of a while Loop
statement
true false
condition
evaluated
The while Statement
• An example of a while statement:
int count = 1;
while (count <= 5)
{
System.out.prin
tln (count);
count++;
}
• If the condition of a while loop is false initially,
the statement is never executed
• Therefore, the body of a while loop will
execute zero or more times
The while Repetition Structure
• Flowchart of while
loop
product <= 1000 product = 2 * product
true
false
int product = 2
int product = 2;
while ( product <= 1000 )
product = 2 * product;
Parts of a while loop
int x = 1;
while (x < 10) {
System.out.println(x);
x++;
}
int product = 2;
while ( product <= 1000 )
product = 2 * product;
Another loop example
int x = 1;
int y = 2;
while (x < 10)
{
System.out.println(x + “ “ + y);
y *= 2;
x++;
}
Quiz
• Sum the numbers from 1 to 100
• Find the average of a series of numbers
Infinite Loops
• 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
Infinite Loops
• An example of an infinite loop:
int count = 1;
while (count <= 25)
{
System.out.print
ln (count);
count = count -
1;
}
• This loop will continue executing until interrupted
(Control-C) or until an underflow error occurs
Nested Loops
• Similar to nested if statements, loops can be nested as
well
• That is, the body of a loop can contain another loop
• For each iteration of the outer loop, the inner loop iterates
completely
Nested Loops
• How many times will the string "Here" be
printed?
count1 = 1;
while (count1 <= 10)
{
count2 = 1;
while (count2 <= 20)
{
System.out.println ("Here");
count2++;
}
count1++;
}
Outline
The while Statement
The do while
Statement The for
Statement
The do while Statement
• A do statement has the following syntax:
do
{
statement;
}
while ( condition )
• The statement is executed once initially, and
then the condition is evaluated
• The statement is executed repeatedly until the
condition becomes false
Logic of a do while Loop
statement
true
condition
evaluated
false
The do while Statement
• An example of a do while loop:
int count = 0;
do
{
count++;
System.out.
println
(count);
} while (count
< 5);
• The body of a do while loop executes at
least once
Comparing while and do while
The while Loop
The do while Loop
statement
true false
condition
evaluated
true
condition
evaluated
statement
false
Outline
The while Statement
The do while
Statement The for
Statement
The for Statement
• A for statement has the following syntax:
for ( initialization ; condition ; increment )
statement;
The increment portion is
executed at the end of each
iteration
The initialization
is executed once
before the loop begins
The statement is
executed until the
condition becomes
false
Logic of a for loop
statement
true
condition
evaluated
false
increment
initialization
The for Statement
• A for loop is functionally equivalent to the following while
loop
structure: initialization;
while ( condition )
{
statement;
increment;
}
The for Statement
• An example of a for loop:
for (int count=1; count <= 5; count++)
System.out.println (count);
• The initialization section can be used to
declare a
variable
• Like a while loop, the condition of a for loop
is tested prior to executing the loop body
• Therefore, the body of a for loop will execute
zero or more times
The for Statement
• The increment section can perform any calculation
for (int num=100; num > 0; num -= 5)
System.out.println (num);
• A for loop is well suited for executing
statements a specific number of times that can be
calculated or determined in advance
Print Stars
//
// Prints a triangle shape using asterisk
(star)
// characters.
//
public static void main (String[] args)
{
final int MAX_ROWS = 10;
for (int row = 1; row <= MAX_ROWS; row++)
{
for (int star = 1; star <= row; star+
+) System.out.print ("*");
System.out.println();
}
}
The for Statement
• Each expression in the header of a for loop is optional
• If the initialization is left out, no initialization is performed
• If the condition is left out, it is always considered to be true, and
therefore creates an infinite loop
• If the increment is left out, no increment operation is performed
for loop Exercises
• How many times is the loop body repeated?
• for (int x = 3; x <= 15; x += 3)
System.out.println(x);
• for (int x = 1; x <= 5; x += 7)
System.out.println(x);
• for (int x = 12; x >= 2; x -= 3)
System.out.println(x);
• Write the for statement that print the following sequences
of values.
• 1, 2, 3, 4, 5, 6, 7
• 3, 8, 13, 18, 23
• 20, 14, 8, 2, -4, -10
• 19, 27, 35, 43, 51
Nested loops. What do these print?
• for (int i = 1; i < 4; i++) for
(int j = 1; j < i; j++)
for (int j = 1; j < i; j++)
for (int j = 1; j < i; j++)
System.out.println(i + “ “
+ j);
System.out.println(“******”)
;
• int T = 0;
for (int i = 1; i < 4; i++) {
for (int j = 1; j < 2*i; j += 2)
T += j * i;
System.out.println(“T = “ + T);
}
System.out.println(i + “ “ + j);
• for (int i = 0; i < 4; i++)
System.out.println(i + “ “ + j);
• for (int i = 1; i < 4; i++)
Questions?

Introduction to Java Programming - Lecture 11.pptx

  • 1.
    Introduction to JavaProgramming Lecture 11: Repetition Statements (Loops) Ahmad Jawid Jami
  • 2.
    Outline The while Statement Thedo while Statement The for Statement
  • 3.
    Repetition Statements • Repetitionstatements allow us to execute a statement multiple times • Often they are referred to as loops • Like conditional statements, they are controlled by boolean expressions • Java has three kinds of repetition statements: • the while loop • the do loop • the for loop • The programmer should choose the right kind of loop for the situation
  • 4.
    The while Statement •A while statement has the following syntax: while ( condition ) statement; • If the condition is true, the statement is executed • Then the condition is evaluated again, and if it is still true, the statement is executed again • The statement is executed repeatedly until the condition becomes false
  • 5.
    Logic of awhile Loop statement true false condition evaluated
  • 6.
    The while Statement •An example of a while statement: int count = 1; while (count <= 5) { System.out.prin tln (count); count++; } • If the condition of a while loop is false initially, the statement is never executed • Therefore, the body of a while loop will execute zero or more times
  • 7.
    The while RepetitionStructure • Flowchart of while loop product <= 1000 product = 2 * product true false int product = 2 int product = 2; while ( product <= 1000 ) product = 2 * product;
  • 8.
    Parts of awhile loop int x = 1; while (x < 10) { System.out.println(x); x++; } int product = 2; while ( product <= 1000 ) product = 2 * product;
  • 9.
    Another loop example intx = 1; int y = 2; while (x < 10) { System.out.println(x + “ “ + y); y *= 2; x++; }
  • 10.
    Quiz • Sum thenumbers from 1 to 100 • Find the average of a series of numbers
  • 11.
    Infinite Loops • Thebody 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
  • 12.
    Infinite Loops • Anexample of an infinite loop: int count = 1; while (count <= 25) { System.out.print ln (count); count = count - 1; } • This loop will continue executing until interrupted (Control-C) or until an underflow error occurs
  • 13.
    Nested Loops • Similarto nested if statements, loops can be nested as well • That is, the body of a loop can contain another loop • For each iteration of the outer loop, the inner loop iterates completely
  • 14.
    Nested Loops • Howmany times will the string "Here" be printed? count1 = 1; while (count1 <= 10) { count2 = 1; while (count2 <= 20) { System.out.println ("Here"); count2++; } count1++; }
  • 15.
    Outline The while Statement Thedo while Statement The for Statement
  • 16.
    The do whileStatement • A do statement has the following syntax: do { statement; } while ( condition ) • The statement is executed once initially, and then the condition is evaluated • The statement is executed repeatedly until the condition becomes false
  • 17.
    Logic of ado while Loop statement true condition evaluated false
  • 18.
    The do whileStatement • An example of a do while loop: int count = 0; do { count++; System.out. println (count); } while (count < 5); • The body of a do while loop executes at least once
  • 19.
    Comparing while anddo while The while Loop The do while Loop statement true false condition evaluated true condition evaluated statement false
  • 20.
    Outline The while Statement Thedo while Statement The for Statement
  • 21.
    The for Statement •A for statement has the following syntax: for ( initialization ; condition ; increment ) statement; The increment portion is executed at the end of each iteration The initialization is executed once before the loop begins The statement is executed until the condition becomes false
  • 22.
    Logic of afor loop statement true condition evaluated false increment initialization
  • 23.
    The for Statement •A for loop is functionally equivalent to the following while loop structure: initialization; while ( condition ) { statement; increment; }
  • 24.
    The for Statement •An example of a for loop: for (int count=1; count <= 5; count++) System.out.println (count); • The initialization section can be used to declare a variable • Like a while loop, the condition of a for loop is tested prior to executing the loop body • Therefore, the body of a for loop will execute zero or more times
  • 25.
    The for Statement •The increment section can perform any calculation for (int num=100; num > 0; num -= 5) System.out.println (num); • A for loop is well suited for executing statements a specific number of times that can be calculated or determined in advance
  • 26.
    Print Stars // // Printsa triangle shape using asterisk (star) // characters. // public static void main (String[] args) { final int MAX_ROWS = 10; for (int row = 1; row <= MAX_ROWS; row++) { for (int star = 1; star <= row; star+ +) System.out.print ("*"); System.out.println(); } }
  • 27.
    The for Statement •Each expression in the header of a for loop is optional • If the initialization is left out, no initialization is performed • If the condition is left out, it is always considered to be true, and therefore creates an infinite loop • If the increment is left out, no increment operation is performed
  • 28.
    for loop Exercises •How many times is the loop body repeated? • for (int x = 3; x <= 15; x += 3) System.out.println(x); • for (int x = 1; x <= 5; x += 7) System.out.println(x); • for (int x = 12; x >= 2; x -= 3) System.out.println(x); • Write the for statement that print the following sequences of values. • 1, 2, 3, 4, 5, 6, 7 • 3, 8, 13, 18, 23 • 20, 14, 8, 2, -4, -10 • 19, 27, 35, 43, 51
  • 29.
    Nested loops. Whatdo these print? • for (int i = 1; i < 4; i++) for (int j = 1; j < i; j++) for (int j = 1; j < i; j++) for (int j = 1; j < i; j++) System.out.println(i + “ “ + j); System.out.println(“******”) ; • int T = 0; for (int i = 1; i < 4; i++) { for (int j = 1; j < 2*i; j += 2) T += j * i; System.out.println(“T = “ + T); } System.out.println(i + “ “ + j); • for (int i = 0; i < 4; i++) System.out.println(i + “ “ + j); • for (int i = 1; i < 4; i++)
  • 30.