+ All Categories
Home > Software > For loop java

For loop java

Date post: 25-May-2015
Category:
Upload: andreeamolnar
View: 1,854 times
Download: 2 times
Share this document with a friend
Popular Tags:
13
For Loop CST200 – Week 2: For loop example Instructor: Andreea Molnar
Transcript
Page 1: For loop java

For Loop

CST200 – Week 2: For loop example

Instructor: Andreea Molnar

Page 2: For loop java

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

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

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

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

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

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

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

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

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

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

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

Summary


Recommended