Control Flow and Functions Overview of this lecture

  • Slides: 36
Download presentation
Control Flow and Functions

Control Flow and Functions

Overview of this lecture • Variables recap and Operators • Control Flow • Branches

Overview of this lecture • Variables recap and Operators • Control Flow • Branches (if-else, switch) • Loops (while, do-while, for) • Functions Dept. of Industrial Design

Variables and Operators / Departement of Industrial Design 4 -9 -2021 PAGE 2

Variables and Operators / Departement of Industrial Design 4 -9 -2021 PAGE 2

Variables: recap • A variable is a typed and named storage location • <type>

Variables: recap • A variable is a typed and named storage location • <type> <name> ; • <type> <name> = <value> ; • Types • byte • int • float • char ( -128 , 127 ) ( -10 , 2147483647 ) ( 3. 1425 , 2. 15) ( ´a´ , ´!´) ( "this is a string" ) • boolean ( true , false ) • String / Departement of Industrial Design 4 -9 -2021 PAGE 3

Boolean variables • In computer science, the Boolean data type has two values (usually

Boolean variables • In computer science, the Boolean data type has two values (usually denoted true and false), intended to represent the truth values of logic and Boolean algebra. • It is named after George Boole, who first defined an algebraic system of logic in the mid 19 th century. The Boolean data type is primarily associated with conditional statements, which allow different actions and change control flow depending on whether a programmer-specified Boolean condition evaluates to true or false. Source: http: //www. cs. auckland. ac. nz/historydisplays/ Time. Line/Time. Line 2/Tim e. Line 2 Main. php from Wikipedia / Departement of Industrial Design 4 -9 -2021 PAGE 4

Operators: overview Operators perform transformations on variables = +, -, *, /, %, (assignment)

Operators: overview Operators perform transformations on variables = +, -, *, /, %, (assignment) (arithmetic) +=, > , &&, ++, ( ? (cumulative arithmetic) (comparison) (boolean logic) (increment, decrement) (conditional expression) -=, *=, /=, %= >= , <, <=, ==, != || -: ) () / Departement of Industrial Design (grouping for priority) 4 -9 -2021 PAGE 5

Operators: comparison (x == y) means x is compared to y for equality true

Operators: comparison (x == y) means x is compared to y for equality true if x is equal to y, otherwise false (x < y) means x is compared to y for < true if x is smaller than y, otherwise false (x >= y) means x is compared to y for ≥ true if x is equal or bigger than y, otherwise false Comparison operators: ==, !=, <, >, <=, >= / Departement of Industrial Design 4 -9 -2021 PAGE 6

Operators: Boolean logic (a && b) means conjunction of a and b true if

Operators: Boolean logic (a && b) means conjunction of a and b true if a is true and also b is true, otherwise false example: (x > 0 && x < 10) is true if x is between 0 and 10 (a || b) means disjunction of a and b true if a is true or both, otherwise false example: (x < 0 || x > 10) is true if x is “outside” 0 and 10 / Departement of Industrial Design 4 -9 -2021 PAGE 7

Operators: Boolean logic (!a) means negation of a true if a is false, otherwise

Operators: Boolean logic (!a) means negation of a true if a is false, otherwise false negation “flips” the Boolean value example: (x > 0 && x < 10) is true if x is between 0 and 10 (!(x > 0 && x < 10)) is true if x is not between 0 and 10 Truth table / Departement of Industrial Design 4 -9 -2021 PAGE 8

Conditionals and Loops / Departement of Industrial Design 4 -9 -2021 PAGE 9

Conditionals and Loops / Departement of Industrial Design 4 -9 -2021 PAGE 9

Conditionals: overview • if ( <boolean condition> ) { <statement or statements> ; }

Conditionals: overview • if ( <boolean condition> ) { <statement or statements> ; } else { <statement or statements> ; } • if ( <boolean condition> ) { <statement or statements> } else if (<boolean condition> <statement or statements> } else { <statement or statements> } / Departement of Industrial Design ; ) { ; ; 4 -9 -2021 PAGE 10

EXERCISE “Draw a rectangle and let it bounce off the boundaries of the canvas.

EXERCISE “Draw a rectangle and let it bounce off the boundaries of the canvas. ” / Departement of Industrial Design 4 -9 -2021 PAGE 11

Conditionals: overview • switch ( <variable> ) { case <value> : <statement or statements>

Conditionals: overview • switch ( <variable> ) { case <value> : <statement or statements> ; break; … default : <statement or statements> ; } / Departement of Industrial Design 4 -9 -2021 PAGE 12

EXERCISE “Write a function that takes a number for a weekday as a parameter

EXERCISE “Write a function that takes a number for a weekday as a parameter and returns the textual day. ” Ex. day. Of. Week. To. Text(0) “Sunday” day. Of. Week. To. Text(1) “Monday” / Departement of Industrial Design 4 -9 -2021 PAGE 13

Loops: overview for ( <start> ; <condition> ; <action> ) { <statement or statements>

Loops: overview for ( <start> ; <condition> ; <action> ) { <statement or statements> ; } while ( <condition> ) { <statement or statements> ; } do { <statement or statements> ; } while ( <condition> ); / Departement of Industrial Design 4 -9 -2021 PAGE 14

while() {} example int count = 10; while (count > 0){ print(”count down ”

while() {} example int count = 10; while (count > 0){ print(”count down ” + count); count--; } yes count > 0 no print 4 -9 -2021 PAGE 15

do while() {} example int count = 10; do { print(”count down ” +

do while() {} example int count = 10; do { print(”count down ” + count); count--; } while (count > 0); print yes count > 0 no 4 -9 -2021 PAGE 16

for (; ; ) {} example for (int i=0; i < 10; i++){ print("count:

for (; ; ) {} example for (int i=0; i < 10; i++){ print("count: " + i); } i = 0 yes i<10 no print("count: " + i) i++ 4 -9 -2021 PAGE 17

Another creative example

Another creative example

50 100 125 165 250 100 150 200 225

50 100 125 165 250 100 150 200 225

size(300, 300); int int x. Min x. Max y. Min y. Max = =

size(300, 300); int int x. Min x. Max y. Min y. Max = = 50 100; 150; 250; int total = 10000; for (int i=0; i<total; i++) { int x = int(random(width)); int y = int(random(height)); if (x >= x. Min && x < x. Max) if (y >= y. Min && y < y. Max) ellipse(x, y, 3, 3); } 250 100 150

fill(255); background(0); size(300, 300); stroke(255, 0, 0); int int x. Min x. Max y.

fill(255); background(0); size(300, 300); stroke(255, 0, 0); int int x. Min x. Max y. Min y. Max = = 100; 150; 250; int total = 10000; for (int i=0; i<total; i++) { if (i == total/3) { x. Min = 150; x. Max = 225; y. Min = 50; y. Max = 100; } if (i == 2*total/3) { x. Min = 150; x. Max = 200; y. Min = 125; y. Max = 165; } int x = int(random(width)); int y = int(random(height)); if (x >= x. Min && x < x. Max) if (y >= y. Min && y < y. Max) ellipse(x, y, 3, 3); }

Functions / Departement of Industrial Design 4 -9 -2021 PAGE 24

Functions / Departement of Industrial Design 4 -9 -2021 PAGE 24

Functions: what? • • DRY principle: Don’t Repeat Yourself! Reusable blocks of code Add

Functions: what? • • DRY principle: Don’t Repeat Yourself! Reusable blocks of code Add structure and flexibility to your programs Make your programs better understandable Has parameters and can return a value void setup(){ size(400, 400); background(255); draw. Rectangle(150, 100, 100); Function call } void draw. Rectangle(float x, float y, float w, float h){ rect(x, y, w, h); Function definition } Dept. of Industrial Design

Functions: general structure return_type function_name(optional parameters){ code to execute when function is called optional

Functions: general structure return_type function_name(optional parameters){ code to execute when function is called optional return } int add(int a, int b) { return a + b; } • • A function starts with a return type (use “void” if no return is needed) An identifier (the name of the function) Open and closed parentheses: optional parameters Open and closed curly braces Dept. of Industrial Design

Functions: Another example void setup(){ size(400, 400); background(255); for(int i=0; i<100; i++){ draw. Rectangle(random(width),

Functions: Another example void setup(){ size(400, 400); background(255); for(int i=0; i<100; i++){ draw. Rectangle(random(width), random (heigth), random(200)); } } • How many function definitions and how many function calls do you see? • What will happen? Dept. of Industrial Design

Functions: strengths • • • Use of different parameters Efficient by avoiding redundant code

Functions: strengths • • • Use of different parameters Efficient by avoiding redundant code Less errors by writing the code only once (in the function) Freed from writing linear code Once a function is defined, you can call it whenever you need it Dept. of Industrial Design

Functions: parameters & arguments • Parameters in function definition should match arguments passed in

Functions: parameters & arguments • Parameters in function definition should match arguments passed in function call void my. Function(int x, int y){…} void my. Function(int x, int y, int z){…} void my. Function(float x, float y, float z){…} my. Function(2, 5); my. Function(2, 5, 7); my. Function(2. 0, 3. 4, 2. 33); • What happens with the call my. Function(2, 2. 3); ? • What happens with the call my. Function(2, 2. 0); ? Dept. of Industrial Design

Functions: return • Each function that has a return type other than void must

Functions: return • Each function that has a return type other than void must have a return statement (often as the last line) int sum; int compute. Sum(int x 1, int x 2, int x 3){ // you could do all sorts of stuff before the return x 1 + x 2 + x 3; } sum = compute. Sum(4, 5, 6); Dept. of Industrial Design

Functions: do not use hard coded numbers • Does not facilitate evolution and flexibility

Functions: do not use hard coded numbers • Does not facilitate evolution and flexibility of code Bad example: size(200, 200); background(255); stroke. Weight(5); stroke(20); fill(100); rect(50, 100, 100); Dept. of Industrial Design

Functions: do not use hard coded numbers, ctd. • Use parameterized function void setup(){

Functions: do not use hard coded numbers, ctd. • Use parameterized function void setup(){ size(200, 200); background(255); create. Rect(50, 100, 20, 5, 100); } // parameterized function void create. Rect(int xpos, int ypos, int width, int height, int stroke. Color, int stroke. Weight, int fill. Color) { stroke(stroke. Color); … rect(xpos, ypos, width, height); } Dept. of Industrial Design

EXERCISE I “Write a function that takes a number for a weekday as a

EXERCISE I “Write a function that takes a number for a weekday as a parameter and returns the textual day. Allow for short or long version. ” Ex. day. Of. Week. To. Text(0, true) “Sunday” day. Of. Week. To. Text(1, false) “Mon” / Departement of Industrial Design 4 -9 -2021 PAGE 33

EXERCISE II • Write a Processing program • With a function that draws the

EXERCISE II • Write a Processing program • With a function that draws the left bird from example Ex_03_21 (Examples >> Books >> Getting Started >> Chapter 3) • Position of bird given as x, y coordinates // example calls draw. Left. Bird(0, 0); draw. Right. Bird(0, 100); draw. Left. Bird(0, 200); • More hands-on: Write function for right bird Write function for birds with scaling (changing the size) / Departement of Industrial Design 4 -9 -2021 PAGE 34

Homework "Write a program that prints the numbers from 1 to 100. But for

Homework "Write a program that prints the numbers from 1 to 100. But for multiples of three print “Snip” instead of the number and for the multiples of five print “Snap”. For numbers which are multiples of both three and five print “Snip. Snap”. ” • Use println(“ ”); to print numbers or text • Write each a version for while, do-while and for loops • Extra: • Write a function for the comparison • Write the program using graphical output / Departement of Industrial Design 4 -9 -2021 PAGE 35