19
Introduction to Programming Class 4 Paul Brebner

Introduction to programming - class 4

Embed Size (px)

Citation preview

Introduction to ProgrammingClass 4

Paul Brebner

Housekeeping

• The Programming course shared directory is located at XXX. I’ll update this with information, sub directories are:

– Exercises

– Lessons

– Textbook

– Students

• The processing environment has a shortcut on desktops, so you won’t need to reinstall it again.

Repetition (of repetition!)

• What does this program do?println(“1”);println(“2”);println(“3”);println(“4”);println(“5”);println(“Once I caught a fish alive”);println(“6”);println(“7”);println(“8”);println(“9”);println(“10”);println(“Then I let it go again (etc)”);

Repetition (of repetition!)

• This12345Once I caught a fish alive678910Then I let it go again (etc)

Using a while loopwith 2 bugs

int number = 1; // start from 1

while (number <= 10) // while number <= 10 repeat the block{ // a block – do everything between { and }

println(number); // print the number each timeprintln(“Once I caught a fish alive”);println(“Then I let it go again (etc)”);

}

Repetition (of repetition!)1Once I caught a fish aliveThen I let it go again (etc)1Once I caught a fish aliveThen I let it go again (etc)1Once I caught a fish aliveThen I let it go again (etc)1Once I caught a fish aliveThen I let it go again (etc)1Once I caught a fish aliveThen I let it go again (etc)...

Using a while loop

int number = 1; // start from 1

while (number <= 10) // while number <= 10 repeat the block{ // a block – do everything between { and }

println(number); // print the number each timeprintln(“Once I caught a fish alive”);println(“Then I let it go again (etc)”);number = number + 1; // must increment number by 1 each time

}

Better - slightly1Once I caught a fish aliveThen I let it go again (etc)2Once I caught a fish aliveThen I let it go again (etc)3Once I caught a fish aliveThen I let it go again (etc)4Once I caught a fish aliveThen I let it go again (etc)5Once I caught a fish aliveThen I let it go again (etc)...10Once I caught a fish aliveThen I let it go again (etc)

Using a while loopand an if

int number = 1; // start from 1

while (number <= 10) // while number <= 10 repeat the block{ // a block – do everything between { and }

println(number); // print the number each timeif (number == 5)

println(“Once I caught a fish alive”);else if (number == 10)

println(“Then I let it go again (etc)”);

number = number + 1; // must increment number by 1 each time}

Correct

12345Once I caught a fish alive678910Then I let it go again (etc)

for loopthis while loop is identical to for loop

int number = 1; // start from 1

while (number <= 10) // while number <= 10 repeat the block{ // a block – do everything between { and }

println(number); // print the number each timeif (number == 5)

println(“Once I caught a fish alive”);else if (number == 10)

println(“Then I let it go again (etc)”);

number = number + 1; // must increment number by 1 each time}

for loop// start from 1// while number <= 10 repeat the block// at end increment number by 1 and repeat

for (int number = 1; number <= 10; number = number + 1) {

println(number);if (number == 5)

println(“Once I caught a fish alive”);else if (number == 10)

println(“Then I let it go again (etc)”);}

comparison// while loopint number = 1; // start from 1

while (number <= 10) // while number <= 10 repeat the block{ // a block – do everything between { and }

println(number); // print the number each timeif (number == 5)

println(“Once I caught a fish alive”);else if (number == 10)

println(“Then I let it go again (etc)”);

number = number + 1; // must increment number by 1 each time}

// for loop

for (int number = 1; number <= 10; number = number + 1) {

println(number);if (number == 5)

println(“Once I caught a fish alive”);else if (number == 10)

println(“Then I let it go again (etc)”);}

number++// start from 1// while number <= 10 repeat the block// at end increment number by 1 and repeat -> number++

for (int number = 1; number <= 10; number++) {

println(number);if (number == 5)

println(“Once I caught a fish alive”);else if (number == 10)

println(“Then I let it go again (etc)”);}

What does this version do?For loops can be NESTED

for (int number = 1; number <= 10; number++) {

println(number);if (number == 5)

println(“Once I caught a fish alive”);else if (number == 10){

println(“Then I let it go again (etc)”);for (int countDown = 10; countDown >= 0; countDown--)

println(countDown);println(“SPLASH!”);

}}

12345Once I caught a fish alive678910Then I let if go again (etc)109876543210SPLASH!

Simulation (using humans) exercise

• Need roles for– For (control)– Initialisation– Condition– End action– Println number– If/else– Number == 5– Println Once I ...– Number == 10– println Then I ...

Roles highlighted in different colours

for (int number = 1; number <= 10; number++) // roles 1, 2, 3, 4 {

println(number); // role 5if (number == 5) // role 6 7

println(“Once I caught a fish alive”); // role 8else // role 5if (number == 10) // role 5 9

println(“Then I let it go again (etc)”); // role 10}

• Line up in order• For is the controller (keeps track of what to do next) order is 1, 2, 3, (not 4), 5-10, then 4, and back to 3 (not 20, 5-10, etc.• Println writes on whiteboard• Variable declaration, assignment, conditions (<= and ==), and maths (e.g. ++) change or check variable value on whiteboard

Exercises

• Textbook

– Page 42ff

– Example 4.6 to 4.13

– Then go back to fruit bowl problem

– Save your fruit bowl problem to work on later

– Next time Robot 2 Page 49