SlideShare a Scribd company logo
1 of 19
CSE110
Principles of Programming
with Java
Lecture 12:
Loops: for statement
Javier Gonzalez-Sanchez
javiergs@asu.edu
javiergs.engineering.asu.edu
Office Hours: By appointment
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 2
Topics
class
global
variables
methods statements
instructions
local
variables
conditional
Statements
if-else
switch
?:
loop
Statement
while
do-while
for
for Statement
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 4
The for Statement
The for statement has the following syntax:
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 5
The for Statement
• A for loop is functionally equivalent to the following
while loop structure:
//initialization
while ( condition ) {
// statement
// increment or update
}
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 6
Logic of a for loop
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 7
Example 1
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 8
The for Statement
• Like a while loop, the condition of a for statement is
tested prior to executing the loop body
• Therefore, the body of a for loop will execute zero
or more times
• It is well suited for executing a loop a specific
number of times that can be determined in
advance
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 9
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
Both semi-colons are always required in the for loop
header.
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 10
Example 2
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 11
Example 3
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 12
Choosing a Loop Structure
• When you can’t determine how many times you
want to execute the loop body, use a while
statement or a do statement
• If it might be zero or more times, use a while
statement
• If it will be at least once, use a do statement
•
If you can determine how many times you want to
execute the loop body, use a for statement
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 13
break Statement
• We can use a “break” statement to get out of the
loop
for (int i=1; i<=100; i=i+2) {
System.out.println(i);
if (i % 15 == 0) {
break;
}
}
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 14
continue Statement
• After executing a continue statement, the rest of
the statements within the loop will be skipped, then
the loop condition will be evaluated again.
for (int i=1; i<=4; i+=1) {
System.out.println(i);
System.out.println("Before");
if (i % 2 == 0) {
continue;
}
System.out.println("After");
}
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 15
Nested loops
• We can have a loop inside of another loop
for (int i=1; i<=3; i=i+1) {
for (int j=4; j>=1; j=j+1) {
System.out.println(i + "," + j);
}
}
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 16
One more thing
• variable = variable + 1;
variable++;
• variable = variable - 1;
variable--;
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 17
One more thing
• We can have a loop inside of another loop
for (int i=1; i<=3; i++) {
for (int j=4; j>=1; j--) {
System.out.println(i + "," + j);
}
}
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 18
Reference
Chapter 4
CSE110 - Principles of Programming
Javier Gonzalez-Sanchez
javiergs@asu.edu
Summer 2017
Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.

More Related Content

Similar to 201707 CSE110 Lecture 12

Week04
Week04Week04
Week04
hccit
 
rtsoft solutions.ppt(1).pptx
rtsoft solutions.ppt(1).pptxrtsoft solutions.ppt(1).pptx
rtsoft solutions.ppt(1).pptx
panderohit000
 
M C6java6
M C6java6M C6java6
M C6java6
mbruggen
 
Lecture 6Decision_Control_OR_Conditional_Control_Structures.ppt
Lecture 6Decision_Control_OR_Conditional_Control_Structures.pptLecture 6Decision_Control_OR_Conditional_Control_Structures.ppt
Lecture 6Decision_Control_OR_Conditional_Control_Structures.ppt
LaxmiVaraprasad1
 
C++ unit-1-part-14
C++ unit-1-part-14C++ unit-1-part-14
C++ unit-1-part-14
Jadavsejal
 
Chapter 6 - More conditionals and loops
Chapter 6 - More conditionals and loopsChapter 6 - More conditionals and loops
Chapter 6 - More conditionals and loops
DanWooster1
 

Similar to 201707 CSE110 Lecture 12 (20)

Week04
Week04Week04
Week04
 
Programming loop
Programming loopProgramming loop
Programming loop
 
Loops c++
Loops c++Loops c++
Loops c++
 
201707 CSE110 Lecture 08
201707 CSE110 Lecture 08  201707 CSE110 Lecture 08
201707 CSE110 Lecture 08
 
rtsoft solutions.ppt(1).pptx
rtsoft solutions.ppt(1).pptxrtsoft solutions.ppt(1).pptx
rtsoft solutions.ppt(1).pptx
 
Looping statements
Looping statementsLooping statements
Looping statements
 
201707 CSE110 Lecture 20
201707 CSE110 Lecture 20  201707 CSE110 Lecture 20
201707 CSE110 Lecture 20
 
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
 
M C6java6
M C6java6M C6java6
M C6java6
 
201707 CSE110 Lecture 02
201707 CSE110 Lecture 02  201707 CSE110 Lecture 02
201707 CSE110 Lecture 02
 
Asynchronous Apex Salesforce World Tour Paris 2015
Asynchronous Apex Salesforce World Tour Paris 2015Asynchronous Apex Salesforce World Tour Paris 2015
Asynchronous Apex Salesforce World Tour Paris 2015
 
Lecture 6Decision_Control_OR_Conditional_Control_Structures.ppt
Lecture 6Decision_Control_OR_Conditional_Control_Structures.pptLecture 6Decision_Control_OR_Conditional_Control_Structures.ppt
Lecture 6Decision_Control_OR_Conditional_Control_Structures.ppt
 
Week2 ch4 part1edited 2020
Week2 ch4 part1edited 2020Week2 ch4 part1edited 2020
Week2 ch4 part1edited 2020
 
Week2 ch4 part1edited 2020
Week2 ch4 part1edited 2020Week2 ch4 part1edited 2020
Week2 ch4 part1edited 2020
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
 
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
 
201707 CSE110 Lecture 03
201707 CSE110 Lecture 03  201707 CSE110 Lecture 03
201707 CSE110 Lecture 03
 
C++ unit-1-part-14
C++ unit-1-part-14C++ unit-1-part-14
C++ unit-1-part-14
 
Chapter 6 - More conditionals and loops
Chapter 6 - More conditionals and loopsChapter 6 - More conditionals and loops
Chapter 6 - More conditionals and loops
 
Repetition loop
Repetition loopRepetition loop
Repetition loop
 

More from Javier Gonzalez-Sanchez

More from Javier Gonzalez-Sanchez (20)

201804 SER332 Lecture 01
201804 SER332 Lecture 01201804 SER332 Lecture 01
201804 SER332 Lecture 01
 
201801 SER332 Lecture 03
201801 SER332 Lecture 03201801 SER332 Lecture 03
201801 SER332 Lecture 03
 
201801 SER332 Lecture 04
201801 SER332 Lecture 04201801 SER332 Lecture 04
201801 SER332 Lecture 04
 
201801 SER332 Lecture 02
201801 SER332 Lecture 02201801 SER332 Lecture 02
201801 SER332 Lecture 02
 
201801 CSE240 Lecture 26
201801 CSE240 Lecture 26201801 CSE240 Lecture 26
201801 CSE240 Lecture 26
 
201801 CSE240 Lecture 25
201801 CSE240 Lecture 25201801 CSE240 Lecture 25
201801 CSE240 Lecture 25
 
201801 CSE240 Lecture 24
201801 CSE240 Lecture 24201801 CSE240 Lecture 24
201801 CSE240 Lecture 24
 
201801 CSE240 Lecture 23
201801 CSE240 Lecture 23201801 CSE240 Lecture 23
201801 CSE240 Lecture 23
 
201801 CSE240 Lecture 22
201801 CSE240 Lecture 22201801 CSE240 Lecture 22
201801 CSE240 Lecture 22
 
201801 CSE240 Lecture 21
201801 CSE240 Lecture 21201801 CSE240 Lecture 21
201801 CSE240 Lecture 21
 
201801 CSE240 Lecture 20
201801 CSE240 Lecture 20201801 CSE240 Lecture 20
201801 CSE240 Lecture 20
 
201801 CSE240 Lecture 19
201801 CSE240 Lecture 19201801 CSE240 Lecture 19
201801 CSE240 Lecture 19
 
201801 CSE240 Lecture 18
201801 CSE240 Lecture 18201801 CSE240 Lecture 18
201801 CSE240 Lecture 18
 
201801 CSE240 Lecture 17
201801 CSE240 Lecture 17201801 CSE240 Lecture 17
201801 CSE240 Lecture 17
 
201801 CSE240 Lecture 16
201801 CSE240 Lecture 16201801 CSE240 Lecture 16
201801 CSE240 Lecture 16
 
201801 CSE240 Lecture 15
201801 CSE240 Lecture 15201801 CSE240 Lecture 15
201801 CSE240 Lecture 15
 
201801 CSE240 Lecture 14
201801 CSE240 Lecture 14201801 CSE240 Lecture 14
201801 CSE240 Lecture 14
 
201801 CSE240 Lecture 13
201801 CSE240 Lecture 13201801 CSE240 Lecture 13
201801 CSE240 Lecture 13
 
201801 CSE240 Lecture 12
201801 CSE240 Lecture 12201801 CSE240 Lecture 12
201801 CSE240 Lecture 12
 
201801 CSE240 Lecture 11
201801 CSE240 Lecture 11201801 CSE240 Lecture 11
201801 CSE240 Lecture 11
 

Recently uploaded

TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps Productivity
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software Engineering
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate Guide
 
Quantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingQuantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation Computing
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
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
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using Ballerina
 
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...
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 

201707 CSE110 Lecture 12

  • 1. CSE110 Principles of Programming with Java Lecture 12: Loops: for statement Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu Office Hours: By appointment
  • 2. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 2 Topics class global variables methods statements instructions local variables conditional Statements if-else switch ?: loop Statement while do-while for
  • 4. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 4 The for Statement The for statement has the following syntax:
  • 5. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 5 The for Statement • A for loop is functionally equivalent to the following while loop structure: //initialization while ( condition ) { // statement // increment or update }
  • 6. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 6 Logic of a for loop
  • 7. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 7 Example 1
  • 8. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 8 The for Statement • Like a while loop, the condition of a for statement is tested prior to executing the loop body • Therefore, the body of a for loop will execute zero or more times • It is well suited for executing a loop a specific number of times that can be determined in advance
  • 9. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 9 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 Both semi-colons are always required in the for loop header.
  • 10. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 10 Example 2
  • 11. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 11 Example 3
  • 12. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 12 Choosing a Loop Structure • When you can’t determine how many times you want to execute the loop body, use a while statement or a do statement • If it might be zero or more times, use a while statement • If it will be at least once, use a do statement • If you can determine how many times you want to execute the loop body, use a for statement
  • 13. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 13 break Statement • We can use a “break” statement to get out of the loop for (int i=1; i<=100; i=i+2) { System.out.println(i); if (i % 15 == 0) { break; } }
  • 14. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 14 continue Statement • After executing a continue statement, the rest of the statements within the loop will be skipped, then the loop condition will be evaluated again. for (int i=1; i<=4; i+=1) { System.out.println(i); System.out.println("Before"); if (i % 2 == 0) { continue; } System.out.println("After"); }
  • 15. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 15 Nested loops • We can have a loop inside of another loop for (int i=1; i<=3; i=i+1) { for (int j=4; j>=1; j=j+1) { System.out.println(i + "," + j); } }
  • 16. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 16 One more thing • variable = variable + 1; variable++; • variable = variable - 1; variable--;
  • 17. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 17 One more thing • We can have a loop inside of another loop for (int i=1; i<=3; i++) { for (int j=4; j>=1; j--) { System.out.println(i + "," + j); } }
  • 18. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 18 Reference Chapter 4
  • 19. CSE110 - Principles of Programming Javier Gonzalez-Sanchez javiergs@asu.edu Summer 2017 Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.