Copyright © Texas Education Agency, 20131 Computer Programming For Loops.

Post on 21-Dec-2015

213 views 0 download

transcript

Copyright © Texas Education Agency, 2013 1

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

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

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

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

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

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

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

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

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

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

Let’s see a for loop in action

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

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

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

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

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

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.

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.

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

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

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

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

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

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

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

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

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

Review

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

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

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

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

Can you write a for loop?

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