for loop java
TRANSCRIPT
![Page 1: For loop java](https://reader035.vdocument.in/reader035/viewer/2022081419/55620789d8b42a7d028b4744/html5/thumbnails/1.jpg)
For Loop
CST200 – Week 2: For loop example
Instructor: Andreea Molnar
![Page 2: For loop java](https://reader035.vdocument.in/reader035/viewer/2022081419/55620789d8b42a7d028b4744/html5/thumbnails/2.jpg)
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
![Page 3: For loop java](https://reader035.vdocument.in/reader035/viewer/2022081419/55620789d8b42a7d028b4744/html5/thumbnails/3.jpg)
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
![Page 4: For loop java](https://reader035.vdocument.in/reader035/viewer/2022081419/55620789d8b42a7d028b4744/html5/thumbnails/4.jpg)
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
![Page 5: For loop java](https://reader035.vdocument.in/reader035/viewer/2022081419/55620789d8b42a7d028b4744/html5/thumbnails/5.jpg)
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
![Page 6: For loop java](https://reader035.vdocument.in/reader035/viewer/2022081419/55620789d8b42a7d028b4744/html5/thumbnails/6.jpg)
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
![Page 7: For loop java](https://reader035.vdocument.in/reader035/viewer/2022081419/55620789d8b42a7d028b4744/html5/thumbnails/7.jpg)
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
![Page 8: For loop java](https://reader035.vdocument.in/reader035/viewer/2022081419/55620789d8b42a7d028b4744/html5/thumbnails/8.jpg)
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
![Page 9: For loop java](https://reader035.vdocument.in/reader035/viewer/2022081419/55620789d8b42a7d028b4744/html5/thumbnails/9.jpg)
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
![Page 10: For loop java](https://reader035.vdocument.in/reader035/viewer/2022081419/55620789d8b42a7d028b4744/html5/thumbnails/10.jpg)
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
![Page 11: For loop java](https://reader035.vdocument.in/reader035/viewer/2022081419/55620789d8b42a7d028b4744/html5/thumbnails/11.jpg)
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
![Page 12: For loop java](https://reader035.vdocument.in/reader035/viewer/2022081419/55620789d8b42a7d028b4744/html5/thumbnails/12.jpg)
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
![Page 13: For loop java](https://reader035.vdocument.in/reader035/viewer/2022081419/55620789d8b42a7d028b4744/html5/thumbnails/13.jpg)
Summary