SlideShare a Scribd company logo
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?

More Related Content

PPT
Programming loop
University of Potsdam
 
PPTX
Looping statements
AbhishekMondal42
 
PPT
15-Loops.ppt
harshsolankurephotos
 
PPT
Looping statements in Java
Jin Castor
 
PPTX
Looping statements
Marco Alzamora
 
PPTX
While , For , Do-While Loop
Abhishek Choksi
 
PDF
cpu.pdf
RAJCHATTERJEE24
 
PPT
C Programming Looping Statements For Students
SoumyaKantiSarkar2
 
Programming loop
University of Potsdam
 
Looping statements
AbhishekMondal42
 
15-Loops.ppt
harshsolankurephotos
 
Looping statements in Java
Jin Castor
 
Looping statements
Marco Alzamora
 
While , For , Do-While Loop
Abhishek Choksi
 
C Programming Looping Statements For Students
SoumyaKantiSarkar2
 

Similar to Introduction to Java Programming - Lecture 11.pptx (20)

PPT
Ch4_part1.ppt
RuthikReddyGarlapati
 
PPT
Ch4_part1.ppt
ssuserc70988
 
PPT
object orineted programming looping .ppt
zeenatparveen24
 
PPT
Lecture on repetition statements (loops)
MustajabHussain9
 
PDF
DSA 103 Object Oriented Programming :: Week 3
Ferdin Joe John Joseph PhD
 
PPT
Repetition Structure
PRN USM
 
PDF
Java Repetiotion Statements
Huda Alameen
 
PPTX
130707833146508191
Tanzeel Ahmad
 
PDF
how to write loops in java explained vividly
shadtarq07
 
PPTX
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
DiwakaranM3
 
PPT
Java presentation
Muhammad Saleem Nagri
 
PPTX
Chapter 5 Loops by z al saeddddddddddddddddddddddddddddddddddd
zainaimadsaed
 
PPTX
for loop in java
Majid Ali
 
PPTX
Java loops for, while and do...while
Jayfee Ramos
 
PPSX
Control structures
SarahLiza Rose Decasa
 
PPT
Loops Do While Arduino Programming Robotics
JhaeZaSangcapGarrido
 
PPTX
Loops c++
Shivani Singh
 
PPTX
Loop(for, while, do while) condition Presentation
Badrul Alam
 
PPT
Computer Programming, Loops using Java
Mahmoud Alfarra
 
PPT
9781439035665 ppt ch05
Terry Yoast
 
Ch4_part1.ppt
RuthikReddyGarlapati
 
Ch4_part1.ppt
ssuserc70988
 
object orineted programming looping .ppt
zeenatparveen24
 
Lecture on repetition statements (loops)
MustajabHussain9
 
DSA 103 Object Oriented Programming :: Week 3
Ferdin Joe John Joseph PhD
 
Repetition Structure
PRN USM
 
Java Repetiotion Statements
Huda Alameen
 
130707833146508191
Tanzeel Ahmad
 
how to write loops in java explained vividly
shadtarq07
 
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
DiwakaranM3
 
Java presentation
Muhammad Saleem Nagri
 
Chapter 5 Loops by z al saeddddddddddddddddddddddddddddddddddd
zainaimadsaed
 
for loop in java
Majid Ali
 
Java loops for, while and do...while
Jayfee Ramos
 
Control structures
SarahLiza Rose Decasa
 
Loops Do While Arduino Programming Robotics
JhaeZaSangcapGarrido
 
Loops c++
Shivani Singh
 
Loop(for, while, do while) condition Presentation
Badrul Alam
 
Computer Programming, Loops using Java
Mahmoud Alfarra
 
9781439035665 ppt ch05
Terry Yoast
 
Ad

Recently uploaded (20)

PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
Sourav Kr Podder
 
PPTX
Strengthening open access through collaboration: building connections with OP...
Jisc
 
PDF
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
MariellaTBesana
 
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PPTX
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
PPTX
Presentation on Janskhiya sthirata kosh.
Ms Usha Vadhel
 
PDF
5.Universal-Franchise-and-Indias-Electoral-System.pdfppt/pdf/8th class social...
Sandeep Swamy
 
PPTX
Understanding operators in c language.pptx
auteharshil95
 
PDF
Module 3: Health Systems Tutorial Slides S2 2025
Jonathan Hallett
 
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
Sourav Kr Podder
 
PPTX
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
PDF
Landforms and landscapes data surprise preview
jpinnuck
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
Open Quiz Monsoon Mind Game Final Set.pptx
Sourav Kr Podder
 
Strengthening open access through collaboration: building connections with OP...
Jisc
 
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
MariellaTBesana
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
Presentation on Janskhiya sthirata kosh.
Ms Usha Vadhel
 
5.Universal-Franchise-and-Indias-Electoral-System.pdfppt/pdf/8th class social...
Sandeep Swamy
 
Understanding operators in c language.pptx
auteharshil95
 
Module 3: Health Systems Tutorial Slides S2 2025
Jonathan Hallett
 
Open Quiz Monsoon Mind Game Prelims.pptx
Sourav Kr Podder
 
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
Landforms and landscapes data surprise preview
jpinnuck
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
Ad

Introduction to Java Programming - Lecture 11.pptx

  • 1. Introduction to Java Programming Lecture 11: Repetition Statements (Loops) Ahmad Jawid Jami
  • 2. Outline The while Statement The do while Statement The for Statement
  • 3. 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
  • 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 a while 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 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;
  • 8. 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;
  • 9. Another loop example int x = 1; int y = 2; while (x < 10) { System.out.println(x + “ “ + y); y *= 2; x++; }
  • 10. Quiz • Sum the numbers from 1 to 100 • Find the average of a series of numbers
  • 11. 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
  • 12. 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
  • 13. 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
  • 14. 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++; }
  • 15. Outline The while Statement The do while Statement The for Statement
  • 16. 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
  • 17. Logic of a do while Loop statement true condition evaluated false
  • 18. 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
  • 19. Comparing while and do while The while Loop The do while Loop statement true false condition evaluated true condition evaluated statement false
  • 20. Outline The while Statement The do 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 a for 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 // // 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(); } }
  • 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. 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++)