For loop java 2
Upcoming SlideShare
Loading in...5
×
 

For loop java 2

on

  • 981 views

 

Statistics

Views

Total Views
981
Views on SlideShare
957
Embed Views
24

Actions

Likes
0
Downloads
11
Comments
0

1 Embed 24

http://vizedhtmlcontent.next.ecollege.com 24

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

For loop java 2 For loop java 2 Presentation Transcript

  • 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); What happens when the program runs } i 0 Step 1: int i=0; // i is initialised with 0 //this is done only once
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • Summary