For loop java
-
Upload
andreeamolnar -
Category
Software
-
view
1.863 -
download
2
Transcript of For loop java
For Loop
CST200 – Week 2: For loop example
Instructor: Andreea Molnar
Print the numbers from 0 to 2
for (int i=0; i<3; i++) {
System.out.println(i);
}Step 1:
int i=0; // i is initialised with 0
//this is done only once
What happens when the program runs
0
i
Print the numbers from 0 to 2
for (int i=0; i<3; i++) {
System.out.println(i);
}
Step 2:
i<3; // this condition is verified
//i in this case is 0
//the codition becomes 0<3 which is true, therefore it exectures the loop statements (the statements between the brakets {}, or if no braket the first statement after the for
What happens when the program runs
0
i
Print the numbers from 0 to 2for (int i=0; i<3; i++) {
System.out.println(i);
} Step 3:
//notice that i++ is ignored at this step
System.out.println(i);
// the program prints i
//i is 0, therefore 0 will be printed
What happens when the program runs
0
i
Print the numbers from 0 to 2
for (int i=0; i<3; i++) {
System.out.println(i);
}Step 4:
//the program reaches the } and it returns to the for; it starts with the i++ part of the for loop
i++; // i is incremented by 1
// i was 0, and it becomes as a result 1
What happens when the program runs
0 1
i
Print the numbers from 0 to 2
for (int i=0; i<3; i++) {
System.out.println(i);
}
Step 6:
i<3; // this condition is verified
//i in this case is 1
//the codition becomes 0<3 which is true, therefore it exectures the loop statements (the statements between the brakets {}, or if no braket the first statement after the for
What happens when the program runs
1
i
Print the numbers from 0 to 2
for (int i=0; i<3; i++) {
System.out.println(i);
}Step 7:
System.out.println(i);
// the program prints i
//i is 1, therefore 1 will be printed
What happens when the program runs
1
i
Print the numbers from 0 to 2for (int i=0; i<3; i++) {
System.out.println(i);
}Step 8:
//the program reaches the } and it returns to the for; it starts with the i++ part of the for loop
i++; // i is incremented by 1
// i was 1, and it becomes as a result 2
What happens when the program runs
1 2
i
Print the numbers from 0 to 2
for (int i=0; i<3; i++) {
System.out.println(i);
}
Step 9:
i<3; // this condition is verified
//i in this case is 2
//the codition becomes 0<3 which is true, therefore it exectures the loop statements (the statements between the brakets {}, or if no braket the first statement after the for
What happens when the program runs
2
i
Print the numbers from 0 to 2
for (int i=0; i<3; i++) {
System.out.println(i);
}Step 10:
System.out.println(i);
// the program prints i
//i is 2, therefore 2 will be printed
What happens when the program runs
2
i
Print the numbers from 0 to 2for (int i=0; i<3; i++) {
System.out.println(i);
}Step 8:
//the program reaches the } and it returns to the for; it starts with the i++ part of the for loop
i++; // i is incremented by 1
// i was 2, and it becomes as a result 3
What happens when the program runs
2 3
i
Print the numbers from 0 to 2
for (int i=0; i<3; i++) {
System.out.println(i);
}Step 11:
i<3; // this condition is verified
//i in this case is 3
//the codition becomes 0<3 which is true, therefore it exectures the loop statements (the statements between the brakets {}, or if no braket the first statement after the for
What happens when the program runs
3
i
Summary