Your SlideShare is downloading. ×
  • Like
For loop java 2
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply
Published

 

Published in Technology , Business
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
933
On SlideShare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
12
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. For Loop CST200 – Week 2: For loop example Instructor: Andreea Molnar
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Summary