Upcoming SlideShare
×

For loop java 2

1,385 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
1,385
On SlideShare
0
From Embeds
0
Number of Embeds
784
Actions
Shares
0
24
0
Likes
0
Embeds 0
No embeds

No notes for slide

For loop java 2

1. 1. For Loop CST200 – Week 2: For loop example Instructor: Andreea Molnar
2. 2. Print the numbers from 0 to 2 for (int i=0; i<3; i++) { System.out.println(i); What happens when the program runs } i 0 Step 1: int i=0; // i is initialised with 0 //this is done only once
3. 3. Print the numbers from 0 to 2 for (int i=0; i<3; i++) { System.out.println(i); Step 2: } i 0 What happens when the program runs 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
4. 4. Print the numbers from 0 to 2 for (int i=0; i<3; i++) { System.out.println(i); } What happens when the program runs Step 3: i //notice that i++ is ignored at this step 0 System.out.println(i); // the program prints i //i is 0, therefore 0 will be printed
5. 5. Print the numbers from 0 to 2 for (int i=0; i<3; i++) { System.out.println(i); } What happens when the program runs Step 4: i 01 //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
6. 6. Print the numbers from 0 to 2 for (int i=0; i<3; i++) { System.out.println(i); Step 6: } i 1 What happens when the program runs 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
7. 7. Print the numbers from 0 to 2 for (int i=0; i<3; i++) { System.out.println(i); What happens when the program runs } i 1 Step 7: System.out.println(i); // the program prints i //i is 1, therefore 1 will be printed
8. 8. Print the numbers from 0 to 2 for (int i=0; i<3; i++) { System.out.println(i); } What happens when the program runs Step 8: i 12 //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
9. 9. Print the numbers from 0 to 2 for (int i=0; i<3; i++) { System.out.println(i); Step 9: } i 2 What happens when the program runs 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
10. 10. Print the numbers from 0 to 2 for (int i=0; i<3; i++) { System.out.println(i); What happens when the program runs } i 2 Step 10: System.out.println(i); // the program prints i //i is 2, therefore 2 will be printed
11. 11. Print the numbers from 0 to 2 for (int i=0; i<3; i++) { System.out.println(i); } What happens when the program runs Step 8: i 23 //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
12. 12. Print the numbers from 0 to 2 for (int i=0; i<3; i++) { System.out.println(i); } Step 11: i 3 What happens when the program runs 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
13. 13. Summary