SlideShare a Scribd company logo
Programming in Java
5-day workshop
Loops
Matt Collison
JP Morgan Chase 2021
PiJ1.4: Loops
Session overview
Loops
• for loops
• while loops
break and continue
Loops
• A loop can be used to execute a block of statements repeatedly. If the
repeating time is:
• predictable: usually use for loop
• unpredictable: usually use while or do-while loop
for ( initialization; condition; expression ){ ... }
• Example:
for( int i=1 ; i<10 ; i++ ){
...
}
Alternatives for loops
• Q1: How about only printing the odd numbers smaller than 10?
for ( initialization; condition; expression ){ ... }
for( int i=1; i<10; i++ ){
if( I % 2 != 0 ){
System.out.println( i );
}
}
• Q2: How about printing the numbers in a descending order?
for( int i=9; i>=1; i--){ ... }
for-each loops
for ( Type var : array ) {
...
}
• Example:
int[] numArray = {7,2,6,0}
for ( int i : numArray ) {
System.out.println("number is: " + i);
}
Note: the use of a colon
while and do while loops
while(Boolean expression) {
...
}
do {
...
} while ( <Boolean expression> )
• When the condition becomes false, program control passes to the line
immediately following the loop.
• while loop might not ever run.
• do-while loop runs at least once.
Examples
Calculate the factorial of n (=1*2*...*n) using the three types of loops.
Option 1: for loop:
int n = 5;
int factorial = 1;
for (int number = 1; number <= n; ++number) {
factorial *= number;
}
Examples
Calculate the factorial of n (=1*2*...*n) using the three types of loops.
Option 2: while loop:
int n = 5;
int factorial = 1;
int number = 1;
while (number<=n) {
factorial *= number;
number++; //update
}
Examples
Calculate the factorial of n (=1*2*...*n) using the three types of loops.
Option 3: do-while loop:
int n = 5;
int factorial = 1;
int number = 1;
do{
factorial *= number;
number++; //update
} while (number<=n);
Should we print or return
the factorial?
break and continue
• The break statement is used to jump out of the loop or switch
statement.
• The continue statement is used to skip to the next iteration of the
loop.
break
• Note: Only jump out of the innermost enclosing loop of the statement.
int n = 5;
for ( int i = 1 ; i < n ; i++ ){
for ( int j = 1 ; j < n ; j++ ){
if ( j > i) {
break;
} else {
System.out.print( j + " ");
}
} // break comes here if it runs
System.out.println();
}
What is the output?
continue
• The continue statement skips the current iteration of a loop. Only skip the
innermost enclosing loop of the statement.
for (int i=0;i<=10;i++){
if (i==5) {
continue;
}
System.out.println(i);
}
System.out.println("Done!");
1. What is the output?
2. What is the output if replacing continue; with break;?
3. What is the output if replacing continue; with return;?
Summary
• Loop control statements
• for loop
• for ( int i=0; i<n; i++) { … }
• while loop and do-while loop
• Both for and while loops might not ever run, do-while loop runs at least once
• break and continue
• break: jump out of the loop or switch statement.
• continue: skips the current iteration of a loop.
• Both only have effect on the innermost enclosing loop.
Learning resources
The workshop homepage
https://mcollison.github.io/JPMC-java-intro-2021/
The course materials
https://mcollison.github.io/java-programming-foundations/
• Session worksheets – updated each week
Additional resources
• Think Java: How to think like a computer scientist
• Allen B Downey (O’Reilly Press)
• Available under Creative Commons license
• https://greenteapress.com/wp/think-java-2e/
• Oracle central Java Documentation –
https://docs.oracle.com/javase/8/docs/api/
• Other sources:
• W3Schools Java - https://www.w3schools.com/java/
• stack overflow - https://stackoverflow.com/
• Coding bat - https://codingbat.com/java

More Related Content

PPTX
C# Loops
PPT
12 doloops
PPT
Week2 ch4 part1edited 2020
PDF
The Ring programming language version 1.8 book - Part 24 of 202
PPTX
chap4 ; Flow Control, Assertions, and Exception Handling (scjp/ocjp)
PPTX
Java covers syntax, data types, object-oriented concepts, control flow, excep...
PPTX
dizital pods session 5-loops.pptx
PPTX
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
C# Loops
12 doloops
Week2 ch4 part1edited 2020
The Ring programming language version 1.8 book - Part 24 of 202
chap4 ; Flow Control, Assertions, and Exception Handling (scjp/ocjp)
Java covers syntax, data types, object-oriented concepts, control flow, excep...
dizital pods session 5-loops.pptx
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC

Similar to Pi j1.4 loops (20)

PPTX
Java chapter 3
PPTX
for loop in java
PDF
how to write loops in java explained vividly
PPTX
Chapter 5 Loops by z al saeddddddddddddddddddddddddddddddddddd
PPTX
07 flow control
PPTX
DAY_1.2.pptx
PPTX
Control statements in java
PPTX
Chapter 5 java
PPTX
Java loops for, while and do...while
PPT
Java Programming: Loops
PPT
Chap05
PPTX
Unit-02 Selection, Mathematical Functions and loops.pptx
DOCX
Java loops
PPTX
PPTX
Pj01 5-exceution control flow
PPT
Computer Programming, Loops using Java
PPTX
22H51A6755.pptx
PPT
Eo gaddis java_chapter_05_5e
PPT
Eo gaddis java_chapter_05_5e
PPTX
Lesson 6 Understanding Loops and Conditional Statements.pptx
Java chapter 3
for loop in java
how to write loops in java explained vividly
Chapter 5 Loops by z al saeddddddddddddddddddddddddddddddddddd
07 flow control
DAY_1.2.pptx
Control statements in java
Chapter 5 java
Java loops for, while and do...while
Java Programming: Loops
Chap05
Unit-02 Selection, Mathematical Functions and loops.pptx
Java loops
Pj01 5-exceution control flow
Computer Programming, Loops using Java
22H51A6755.pptx
Eo gaddis java_chapter_05_5e
Eo gaddis java_chapter_05_5e
Lesson 6 Understanding Loops and Conditional Statements.pptx
Ad

More from mcollison (11)

PPTX
Pi j4.2 software-reliability
PPTX
Pi j4.1 packages
PPTX
Pi j3.1 inheritance
PPTX
Pi j3.2 polymorphism
PPTX
Pi j3.4 data-structures
PPTX
Pi j2.3 objects
PPTX
Pi j2.2 classes
PPTX
Pi j1.0 workshop-introduction
PPTX
Pi j1.3 operators
PPTX
Pi j1.2 variable-assignment
PPTX
Pi j1.1 what-is-java
Pi j4.2 software-reliability
Pi j4.1 packages
Pi j3.1 inheritance
Pi j3.2 polymorphism
Pi j3.4 data-structures
Pi j2.3 objects
Pi j2.2 classes
Pi j1.0 workshop-introduction
Pi j1.3 operators
Pi j1.2 variable-assignment
Pi j1.1 what-is-java
Ad

Recently uploaded (20)

PPTX
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
PPTX
Digestion and Absorption of Carbohydrates, Proteina and Fats
PDF
RMMM.pdf make it easy to upload and study
PDF
Hazard Identification & Risk Assessment .pdf
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PDF
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
Indian roads congress 037 - 2012 Flexible pavement
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Lesson notes of climatology university.
PDF
SOIL: Factor, Horizon, Process, Classification, Degradation, Conservation
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
Empowerment Technology for Senior High School Guide
PDF
advance database management system book.pdf
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Cell Types and Its function , kingdom of life
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PDF
1_English_Language_Set_2.pdf probationary
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
Digestion and Absorption of Carbohydrates, Proteina and Fats
RMMM.pdf make it easy to upload and study
Hazard Identification & Risk Assessment .pdf
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Indian roads congress 037 - 2012 Flexible pavement
What if we spent less time fighting change, and more time building what’s rig...
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Lesson notes of climatology university.
SOIL: Factor, Horizon, Process, Classification, Degradation, Conservation
Weekly quiz Compilation Jan -July 25.pdf
Empowerment Technology for Senior High School Guide
advance database management system book.pdf
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
Chinmaya Tiranga quiz Grand Finale.pdf
Cell Types and Its function , kingdom of life
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
1_English_Language_Set_2.pdf probationary

Pi j1.4 loops

  • 1. Programming in Java 5-day workshop Loops Matt Collison JP Morgan Chase 2021 PiJ1.4: Loops
  • 2. Session overview Loops • for loops • while loops break and continue
  • 3. Loops • A loop can be used to execute a block of statements repeatedly. If the repeating time is: • predictable: usually use for loop • unpredictable: usually use while or do-while loop for ( initialization; condition; expression ){ ... } • Example: for( int i=1 ; i<10 ; i++ ){ ... }
  • 4. Alternatives for loops • Q1: How about only printing the odd numbers smaller than 10? for ( initialization; condition; expression ){ ... } for( int i=1; i<10; i++ ){ if( I % 2 != 0 ){ System.out.println( i ); } } • Q2: How about printing the numbers in a descending order? for( int i=9; i>=1; i--){ ... }
  • 5. for-each loops for ( Type var : array ) { ... } • Example: int[] numArray = {7,2,6,0} for ( int i : numArray ) { System.out.println("number is: " + i); } Note: the use of a colon
  • 6. while and do while loops while(Boolean expression) { ... } do { ... } while ( <Boolean expression> ) • When the condition becomes false, program control passes to the line immediately following the loop. • while loop might not ever run. • do-while loop runs at least once.
  • 7. Examples Calculate the factorial of n (=1*2*...*n) using the three types of loops. Option 1: for loop: int n = 5; int factorial = 1; for (int number = 1; number <= n; ++number) { factorial *= number; }
  • 8. Examples Calculate the factorial of n (=1*2*...*n) using the three types of loops. Option 2: while loop: int n = 5; int factorial = 1; int number = 1; while (number<=n) { factorial *= number; number++; //update }
  • 9. Examples Calculate the factorial of n (=1*2*...*n) using the three types of loops. Option 3: do-while loop: int n = 5; int factorial = 1; int number = 1; do{ factorial *= number; number++; //update } while (number<=n); Should we print or return the factorial?
  • 10. break and continue • The break statement is used to jump out of the loop or switch statement. • The continue statement is used to skip to the next iteration of the loop.
  • 11. break • Note: Only jump out of the innermost enclosing loop of the statement. int n = 5; for ( int i = 1 ; i < n ; i++ ){ for ( int j = 1 ; j < n ; j++ ){ if ( j > i) { break; } else { System.out.print( j + " "); } } // break comes here if it runs System.out.println(); } What is the output?
  • 12. continue • The continue statement skips the current iteration of a loop. Only skip the innermost enclosing loop of the statement. for (int i=0;i<=10;i++){ if (i==5) { continue; } System.out.println(i); } System.out.println("Done!"); 1. What is the output? 2. What is the output if replacing continue; with break;? 3. What is the output if replacing continue; with return;?
  • 13. Summary • Loop control statements • for loop • for ( int i=0; i<n; i++) { … } • while loop and do-while loop • Both for and while loops might not ever run, do-while loop runs at least once • break and continue • break: jump out of the loop or switch statement. • continue: skips the current iteration of a loop. • Both only have effect on the innermost enclosing loop.
  • 14. Learning resources The workshop homepage https://mcollison.github.io/JPMC-java-intro-2021/ The course materials https://mcollison.github.io/java-programming-foundations/ • Session worksheets – updated each week
  • 15. Additional resources • Think Java: How to think like a computer scientist • Allen B Downey (O’Reilly Press) • Available under Creative Commons license • https://greenteapress.com/wp/think-java-2e/ • Oracle central Java Documentation – https://docs.oracle.com/javase/8/docs/api/ • Other sources: • W3Schools Java - https://www.w3schools.com/java/ • stack overflow - https://stackoverflow.com/ • Coding bat - https://codingbat.com/java

Editor's Notes

  • #12: Answer: 1 1 2 1 2 3 1 2 3 4
  • #13: Answer 1: 0 1 2 3 4 Done Answer 2: 0 1 2 3 4 Answer 3: 0 1 2 3 4
  • #15: All resources hang from the ELE pages. How many of you have looked through them?