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
}
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.
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);
}
}
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.