copyright © texas education agency, 20131 computer programming for loops

32
Copyright © Texas Education Agency, 2013 1 Computer Programming For Loops

Upload: aubrey-paul

Post on 21-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

Copyright © Texas Education Agency, 2013 1

Computer Programming

For Loops

Page 2: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

List some tasks that you have to do over and over again

Mow the lawn Brush your teeth Take out the trash

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 2

Page 3: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

Wouldn’t it be great if you only had to do the task one time, then it repeated itself automatically?

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 3

Page 4: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

Looping is a central idea in programming

‘Looping’ is the programming term for repeating a task more than once.

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 4

Page 5: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

The ‘for’ loop is a basic structure for repetition

for ( int j = 0; j < 7; j++)

System.out.println(“Counting ” + j);

// The loop above goes from 0 to 6

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 5

Page 6: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

for loops are used when…

The same task must be executed several times.

You know how many iterations* are needed

* An iteration is a single execution of a loop, which may execute many times.

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 6

Page 7: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

The ‘for’ loop starts with the reserved word…

for ( int j = 0; j < 7; j++)

System.out.println(“Counting ” + j);

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 7

Page 8: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

Then comes a header with 3 parts…

for ( int j = 0; j < 7; j++)

System.out.println(“Counting ” + j);

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 8

Page 9: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

The first header item is the initialization expression

for ( int j = 0; j < 7; j++)

System.out.println(“Counting ” + j);

This determines where the loop starts counting. In this case, the loop starts at 0.

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 9

Page 10: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

The second header item is the condition or control expression

for ( int j = 0; j < 7; j++)

System.out.println(“Counting ” + j);

This determines where the loop stops counting. In this case, the loop stops when j is not less than 7. The condition must be true in order for the loop to keep running. If the condition is not true to start with, the loop will never run.

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 10

Page 11: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

The third header item is the step expression

for ( int j = 0; j < 7; j++)

System.out.println(“Counting ” + j);

This determines how much the loop changes by. In this case, ‘j’ changes by 1 each time.

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 11

Page 12: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

Let’s see a for loop in action

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 12

Page 13: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

for ( int j = 0; j < 5; j++) System.out.println(“Counting ” + j);

Counting 0

Counting 1

Counting 2

Counting 3

Counting 4

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 13

Page 14: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

The initialization does not have to start with 0

for ( int j = 0; j < 7; j++)

for ( int j = 4; j < 7; j++)

for ( int j = 1; j < 32; j++)

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 14

Page 15: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

The control must evaluate to true/false

for ( int j = 0; j < =7; j++)

for ( int j = 4; j*2 < 100; j++)

for ( int j = 1; j < x; j++)

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 15

Page 16: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

The step must move the condition towards false

for ( int j = 0; j < =7; j++)

for ( int j = 400; j > 0; j--)

for ( int j = 1; j < 100; j=j+4)

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 16

Page 17: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

When the condition is false, the loop is finished running

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 17

for ( int j = 0; j < 7; j--)

System.out.println(“Counting ” + j);

The loop above will never stop running, because j is becoming more negative, it will always be less than 0. This is known as an infinite loop.

Page 18: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

When the loop condition begins as false, the loop will never run

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 18

for ( int j = 0; j > 7; j++)

System.out.println(“Counting ” + j);

The loop above will never start running, because j is not more than 7.

Page 19: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

There is special step syntax

for (int j = 0; j < 100; j=j+1)

for (int j = 0; j < 100; j++) // means the same thing

for (int j = 0; j < 100; j=j+8)

for (int j = 0; j < 100; j+=8) // means the same thing

for (int j = 0; j < 100; j=j*2)

for (int j = 0; j < 100; j*=2) // means the same thing

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 19

Page 20: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

Do you know the special syntax for the expressions below?

j = j – 1

j = j – 2

j = j / 2

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 20

Page 21: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

Did you guess right?

j = j – 1 same as j - = 1 or j --

j = j – 2 same as j -= 2

j = j / 2 same as j /=2

j = j +1 same as j += 1 or j++

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 21

Page 22: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

How do you make more than one line execute?

for ( int j = 0; j < 7; j++)

System.out.println(“Counting ” + j);

System.out.println(“Printing ” + j);

// Only the “Counting” line is in the for loop.

// The “Printing” line runs after the loop ends

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 22

Page 23: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

Use curly brackets to make a block statement

for ( int j = 0; j < 7; j++) {

System.out.println(“Counting ” + j);

System.out.println(“Printing ” + j);

}

// Now both “Counting” and “Printing” execute inside the loop. The line or lines that execute inside a loop are known as the ‘body’ of the loop.

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 23

Page 24: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

Initialization Variables

for ( int j = 0; j < 7; j++)

The initialization variable tells the loop where to start.

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 24

Page 25: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

The variable ‘j’ is only valid inside the for loop, where it was declared, but not outside the loop

for ( int j = 0; j < 7; j++) {

System.out.println( j);

}

// System.out.println(j); // this would not work

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 25

Page 26: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

The variable ‘k’ is valid outside the for loop, where it was declared, as well as inside the loop

int k;

for ( k = 0; j < 7; k++) {

System.out.println( k);

}

System.out.println(k); // this works

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 26

Page 27: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

The loop count variable ‘k’ is incremented until the condition fails -- when it fails the loop is over

int k;

for ( k = 0; j < 3; k++) {

System.out.println( k); // k=0, 1, 2

}

System.out.println(k); // k is 3

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 27

Page 28: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

Review

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 28

Page 29: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

What is this part called and what doesit do?

for ( int j = 4; j < 9; j++)

System.out.println(“Counting ” + j);

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 29

Page 30: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

What is this part called and what does it do?

for ( int j = 2; j < 9; j++)

System.out.println(“Counting ” + j);

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 30

Page 31: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

What is this part called and what does it do?

for ( int j = 14; j > 7; j -=2)

System.out.println(“Counting ” + j);

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 31

Page 32: Copyright © Texas Education Agency, 20131 Computer Programming For Loops

Can you write a for loop?

Copyright © Texas Education Agency, 2013 IT: [Computer Programming] - [For Loops] 32