SlideShare a Scribd company logo
1 of 21
Looping Statements
Prepared by:
Jean Michael Castor
5-1
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
5-2
The while Statement
ā€¢ A while statement has the following syntax:
5-3
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
5-4
statement
true false
condition
evaluated
The while Statement
ā€¢ An example of a while statement:
5-5
int count = 1;
while (count <= 5){
System.out.println (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 Statement
ā€¢ Let's look at some examples of loop
processing
ā€¢ A loop can be used to maintain a running sum
ā€¢ A sentinel value is a special input value that
represents the end of input
ā€¢ A loop can also be used for input validation,
making a program more robust
5-6
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 (semantic) error
ā€¢ You should always double check the logic of a
program to ensure that your loops will terminate
normally
5-7
Infinite Loops
ā€¢ An example of an infinite loop:
5-8
int count = 1;
while (count <= 25){
System.out.println (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
ā€¢ Your second course project involves a while
loop nested inside of a for loop
5-9
Nested Loops
ā€¢ How many times will the string "Here" be printed?
5-10
count1 = 1;
while (count1 <= 10){
count2 = 1;
while (count2 <= 20) {
System.out.println ("Here");
count2++;
}
count1++;
}
10 * 20 = 200
Outline
5-11
The while Statement
Other Repetition Statements
The do-while Statement
ā€¢ A do-while statement (also called a do loop)
has the following syntax:
5-12
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
5-13
true
condition
evaluated
statement
false
The do Statement
ā€¢ An example of a do loop:
5-14
ā€¢ The body of a do loop executes at least once
int count = 0;
do{
count++;
System.out.println (count);
} while (count < 5);
Comparing while and do
5-15
statement
true false
condition
evaluated
The while Loop
true
condition
evaluated
statement
false
The do Loop
The for Statement
ā€¢ A for statement has the following syntax:
5-16
for ( initialization ; condition ; increment ){
statement;
}
The initialization
is executed once
before the loop begins
The statement is
executed until the
condition becomes false
The increment portion is
executed at the end of each
iteration
Logic of a for loop
5-17
statement
true
condition
evaluated
false
increment
initialization
The for Statement
ā€¢ A for loop is functionally equivalent to the
following while loop structure:
5-18
initialization;
while ( condition ){
statement;
increment;
}
The for Statement
ā€¢ An example of a for loop:
5-19
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
5-20
ā€¢ A for loop is well suited for executing statements
a specific number of times that can be calculated
or determined in advance
for (int num=100; num > 0; num -= 5){
System.out.println (num);
}
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
ā€“ We usually call this a ā€œforever loopā€
ā€¢ If the increment is left out, no increment operation is
performed
5-21

More Related Content

What's hot

Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
Ā 
OOP java
OOP javaOOP java
OOP java
xball977
Ā 
Looping statements in C
Looping statements in CLooping statements in C
Looping statements in C
Jeya Lakshmi
Ā 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
Abhilash Nair
Ā 

What's hot (20)

Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
Ā 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
Ā 
Java loops
Java loopsJava loops
Java loops
Ā 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
Ā 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
Ā 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
Ā 
for loop in java
for loop in java for loop in java
for loop in java
Ā 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
Ā 
C functions
C functionsC functions
C functions
Ā 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
Ā 
OOP java
OOP javaOOP java
OOP java
Ā 
Java basics and java variables
Java basics and java variablesJava basics and java variables
Java basics and java variables
Ā 
Basics of JAVA programming
Basics of JAVA programmingBasics of JAVA programming
Basics of JAVA programming
Ā 
Looping statements in C
Looping statements in CLooping statements in C
Looping statements in C
Ā 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
Ā 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
Ā 
C# loops
C# loopsC# loops
C# loops
Ā 
Nested loops
Nested loopsNested loops
Nested loops
Ā 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementation
Ā 
Data types in java
Data types in javaData types in java
Data types in java
Ā 

Similar to Looping statements in Java

Week04
Week04Week04
Week04
hccit
Ā 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
JavvajiVenkat
Ā 
Going loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptxGoing loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptx
Amy Nightingale
Ā 

Similar to Looping statements in Java (20)

Looping statements
Looping statementsLooping statements
Looping statements
Ā 
Programming loop
Programming loopProgramming loop
Programming loop
Ā 
Looping statements
Looping statementsLooping statements
Looping statements
Ā 
Week04
Week04Week04
Week04
Ā 
cpu.pdf
cpu.pdfcpu.pdf
cpu.pdf
Ā 
15-Loops.ppt
15-Loops.ppt15-Loops.ppt
15-Loops.ppt
Ā 
Looping
LoopingLooping
Looping
Ā 
Loops c++
Loops c++Loops c++
Loops c++
Ā 
3. Flow Controls in C (Part II).pdf
3. Flow Controls in C (Part II).pdf3. Flow Controls in C (Part II).pdf
3. Flow Controls in C (Part II).pdf
Ā 
Repetition, Basic loop structures, Loop programming techniques
Repetition, Basic loop structures, Loop programming techniquesRepetition, Basic loop structures, Loop programming techniques
Repetition, Basic loop structures, Loop programming techniques
Ā 
Visula C# Programming Lecture 4
Visula C# Programming Lecture 4Visula C# Programming Lecture 4
Visula C# Programming Lecture 4
Ā 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
Ā 
Going loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptxGoing loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptx
Ā 
Lesson 5
Lesson 5Lesson 5
Lesson 5
Ā 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in C
Ā 
Loops
LoopsLoops
Loops
Ā 
While loop
While loopWhile loop
While loop
Ā 
whileloop-161225171903.pdf
whileloop-161225171903.pdfwhileloop-161225171903.pdf
whileloop-161225171903.pdf
Ā 
Lec7 - Loops updated.pptx
Lec7 - Loops updated.pptxLec7 - Loops updated.pptx
Lec7 - Loops updated.pptx
Ā 
Ch4_part1.ppt
Ch4_part1.pptCh4_part1.ppt
Ch4_part1.ppt
Ā 

More from Jin Castor

Introduction to search engine optimization
Introduction to search engine optimizationIntroduction to search engine optimization
Introduction to search engine optimization
Jin Castor
Ā 
Web services protocols
Web services protocolsWeb services protocols
Web services protocols
Jin Castor
Ā 
Web application security
Web application securityWeb application security
Web application security
Jin Castor
Ā 
Introduction to xampp
Introduction to xamppIntroduction to xampp
Introduction to xampp
Jin Castor
Ā 
Drupal introduction
Drupal introductionDrupal introduction
Drupal introduction
Jin Castor
Ā 
Web security
Web securityWeb security
Web security
Jin Castor
Ā 
Control statements in Java
Control statements  in JavaControl statements  in Java
Control statements in Java
Jin Castor
Ā 
Switch statements in Java
Switch statements  in JavaSwitch statements  in Java
Switch statements in Java
Jin Castor
Ā 
Java input
Java inputJava input
Java input
Jin Castor
Ā 
Java arrays
Java arraysJava arrays
Java arrays
Jin Castor
Ā 

More from Jin Castor (17)

Information security
 Information security Information security
Information security
Ā 
Introduction to E-commerce
Introduction to E-commerceIntroduction to E-commerce
Introduction to E-commerce
Ā 
Introduction to Infographics Designing
Introduction to Infographics DesigningIntroduction to Infographics Designing
Introduction to Infographics Designing
Ā 
Creative designing using Adobe Products
Creative designing using Adobe ProductsCreative designing using Adobe Products
Creative designing using Adobe Products
Ā 
Introduction to Adobe Illustrator
Introduction to Adobe IllustratorIntroduction to Adobe Illustrator
Introduction to Adobe Illustrator
Ā 
SEO Advanced and scalable link building
SEO  Advanced and scalable link building SEO  Advanced and scalable link building
SEO Advanced and scalable link building
Ā 
Introduction to Web Designing
Introduction to Web DesigningIntroduction to Web Designing
Introduction to Web Designing
Ā 
Introduction to search engine optimization
Introduction to search engine optimizationIntroduction to search engine optimization
Introduction to search engine optimization
Ā 
Web services protocols
Web services protocolsWeb services protocols
Web services protocols
Ā 
Web application security
Web application securityWeb application security
Web application security
Ā 
Introduction to xampp
Introduction to xamppIntroduction to xampp
Introduction to xampp
Ā 
Drupal introduction
Drupal introductionDrupal introduction
Drupal introduction
Ā 
Web security
Web securityWeb security
Web security
Ā 
Control statements in Java
Control statements  in JavaControl statements  in Java
Control statements in Java
Ā 
Switch statements in Java
Switch statements  in JavaSwitch statements  in Java
Switch statements in Java
Ā 
Java input
Java inputJava input
Java input
Ā 
Java arrays
Java arraysJava arrays
Java arrays
Ā 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(ā˜Žļø+971_581248768%)**%*]'#abortion pills for sale in dubai@
Ā 

Recently uploaded (20)

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
Ā 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
Ā 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
Ā 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
Ā 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
Ā 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Ā 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
Ā 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Ā 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
Ā 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
Ā 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
Ā 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Ā 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
Ā 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Ā 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
Ā 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
Ā 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
Ā 
Scaling API-first ā€“ The story of a global engineering organization
Scaling API-first ā€“ The story of a global engineering organizationScaling API-first ā€“ The story of a global engineering organization
Scaling API-first ā€“ The story of a global engineering organization
Ā 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Ā 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
Ā 

Looping statements in Java

  • 2. 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 5-2
  • 3. The while Statement ā€¢ A while statement has the following syntax: 5-3 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
  • 4. Logic of a while Loop 5-4 statement true false condition evaluated
  • 5. The while Statement ā€¢ An example of a while statement: 5-5 int count = 1; while (count <= 5){ System.out.println (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
  • 6. The while Statement ā€¢ Let's look at some examples of loop processing ā€¢ A loop can be used to maintain a running sum ā€¢ A sentinel value is a special input value that represents the end of input ā€¢ A loop can also be used for input validation, making a program more robust 5-6
  • 7. 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 (semantic) error ā€¢ You should always double check the logic of a program to ensure that your loops will terminate normally 5-7
  • 8. Infinite Loops ā€¢ An example of an infinite loop: 5-8 int count = 1; while (count <= 25){ System.out.println (count); count = count - 1; } ā€¢ This loop will continue executing until interrupted (Control-C) or until an underflow error occurs
  • 9. 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 ā€¢ Your second course project involves a while loop nested inside of a for loop 5-9
  • 10. Nested Loops ā€¢ How many times will the string "Here" be printed? 5-10 count1 = 1; while (count1 <= 10){ count2 = 1; while (count2 <= 20) { System.out.println ("Here"); count2++; } count1++; } 10 * 20 = 200
  • 11. Outline 5-11 The while Statement Other Repetition Statements
  • 12. The do-while Statement ā€¢ A do-while statement (also called a do loop) has the following syntax: 5-12 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
  • 13. Logic of a do-while Loop 5-13 true condition evaluated statement false
  • 14. The do Statement ā€¢ An example of a do loop: 5-14 ā€¢ The body of a do loop executes at least once int count = 0; do{ count++; System.out.println (count); } while (count < 5);
  • 15. Comparing while and do 5-15 statement true false condition evaluated The while Loop true condition evaluated statement false The do Loop
  • 16. The for Statement ā€¢ A for statement has the following syntax: 5-16 for ( initialization ; condition ; increment ){ statement; } The initialization is executed once before the loop begins The statement is executed until the condition becomes false The increment portion is executed at the end of each iteration
  • 17. Logic of a for loop 5-17 statement true condition evaluated false increment initialization
  • 18. The for Statement ā€¢ A for loop is functionally equivalent to the following while loop structure: 5-18 initialization; while ( condition ){ statement; increment; }
  • 19. The for Statement ā€¢ An example of a for loop: 5-19 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
  • 20. The for Statement ā€¢ The increment section can perform any calculation 5-20 ā€¢ A for loop is well suited for executing statements a specific number of times that can be calculated or determined in advance for (int num=100; num > 0; num -= 5){ System.out.println (num); }
  • 21. 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 ā€“ We usually call this a ā€œforever loopā€ ā€¢ If the increment is left out, no increment operation is performed 5-21