Upload
paul-brebner
View
78
Download
1
Embed Size (px)
Citation preview
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)”);
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}
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!”);
}}
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