Slide 1

Slide 1 text

CSE110 Principles of Programming with Java Lecture 13: Loops: for statement Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com Office Hours: By appointment

Slide 2

Slide 2 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 2 Topics class global variables methods statements instructions local variables conditional Statements if-else switch ?: loop Statement while do-while for

Slide 3

Slide 3 text

for Statement

Slide 4

Slide 4 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 4 The for Statement The for statement has the following syntax:

Slide 5

Slide 5 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 5 The for Statement • A for loop is functionally equivalent to the following while loop structure: //initialization while ( condition ) { // statement // increment or update }

Slide 6

Slide 6 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 6 Example 1

Slide 7

Slide 7 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 7 Logic of a for loop

Slide 8

Slide 8 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 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

Slide 9

Slide 9 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 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.

Slide 10

Slide 10 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 10 Example 1

Slide 11

Slide 11 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 11 Example 2

Slide 12

Slide 12 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 12 Example 3

Slide 13

Slide 13 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 13 Example 4 for ( ; ; ) { System.out.println(”I am here! "); }

Slide 14

Slide 14 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 14 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

Slide 15

Slide 15 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 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 ); } }

Slide 16

Slide 16 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 16 One more thing • variable = variable + 1; variable++; • variable = variable - 1; variable--;

Slide 17

Slide 17 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 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 ); } }

Slide 18

Slide 18 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 18 Reference Chapter 4

Slide 19

Slide 19 text

CSE110 - Principles of Programming Javier Gonzalez-Sanchez [email protected] Summer 2020 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.