Conditionals 02-201!
Computing GCD GCD Problem: Compute the greatest common divisor of two integers. Input: Two integers a and b. Output: The greatest common divisor of a and b. Exercise: Design an algorithm in pseudocode solving this problem.
The World s First Nontrivial Algorithm (~300 BC) Theorem: If a > b, then gcd(a, b) = gcd(a b, b). Example gcd(378, 273) = gcd(105, 273) = gcd(105, 168) = gcd(105, 63) = gcd(42, 63) = gcd(42, 21) = gcd(21, 21) = 21 Euclid! Exercise: Implement Euclid s algorithm in pseudocode.
Recall: Recursive Gauss RecursiveGauss(n) if n = 0 return 0 else return n + RecursiveGauss(n-1)
Recursive Euclid in Go Comments describing! the function! Function Name! Parameters! // Gcd(a,b) computes the greatest common // divisor of a and b func Gcd(a int, b int) int { Return type! comes after the () and before the {! if a == b { return a else if a > b { return Gcd(a - b, b) else { return Gcd(a, b - a) Return statement: stops! executing the function! and gives its value! Function call! (this one is recursive because it calls the function it s in)!
Recursive Factorial Exercise: Write a recursive Go function that takes an integer n and returns n! = (n)(n-1) (2)(1).
Recursive Fibonacci Exercise: Write a recursive Go function that takes an integer n and returns the n-th Fibonacci number.
The Problem with Recursive Fibonacci http://www.introprogramming.info!
Can You Spot the Error? func Gcd(a int, b int) int { if a == b { return a else if a > b { var c int c = a - b return Gcd(c, b) else { c = b - a return Gcd(a, c)
Scope: How Long Variables Last Variables persist from when they are created until the end of the innermost { block that they are in. func Gcd(a int, b int) int { var c int if a == b { return a else if a > b { c = a - b return Gcd(c, b) else { c = b - a return Gcd(a, c) variable c created variable c destroyed
Scope: How Long Variables Last Variables persist from when they are created until the end of the innermost { block that they are in. func Gcd(a int, b int) int { if a == b { return a else if a > b { var c int c = a - b return Gcd(c, b) else { var c int c = b - a return Gcd(a, c) c created c destroyed c resurrected c destroyed
Can You Spot the Error? Variables persist from when they are created until the end of the innermost { block that they are in. func Gcd(a int, b int) int { var c int if a == b { return a else if a > b { var c int c = a - b return Gcd(c, b) else { c = b - a return Gcd(a, c)
Scope of Parameters func Gcd(a int, b int) int { if a == b { return a else if a > b { return Gcd(a - b, b) else { return Gcd(a, b - a) variables a and b created variables a and b destroyed
Scope of Parameters func Gcd(a int, b int) int { if a == b { return a else if a > b { return Gcd(a - b, b) else { return Gcd(a, b - a) variables a and b created variables a and b destroyed var x, y int = 378, 273 var d int = Gcd(x, y) fmt.println( x is, x) fmt.println( y is, y) Think: What is printed?
Additional Conditions Boolean Operator Meaning e1 > e2 e1 is greater than e2! e1 < e2 e1 is less than e2! e1 >= e2 e1 is greater than or equal to e2! e1 <= e2 e1 is less than or equal to e2! e1 == e2 e1 is equal to e2! e1!= e2 e1 is not equal to e2!!e1 true if and only if e1 is false! (e1 and e2 can be complicated expressions) Example conditions: a > 10 * b + c 10 == 10 square(10) < 101-1 + 2!(x*y < 33) Boolean expressions: evaluate to true or false
Boolean Operators: AND and OR Boolean Operator Meaning pipe character Often above \ on your keyboard e1 && e2 e1 e2 true if e1 is true AND e2 is true! true if e1 is true OR e2 is true! Truth Table e 1 e 2 e 1 && e 2 e 1 e 2 true! true! true! true! true! false! false! true! false! true! false! true! false! false! false! false!
Order of Operator Precedence Precedence Operator 5! * / % 4! + - 3! ==!= < <= > >= 2! && 1! Example: 2*a + b!= 3 && a*c 3/z < -5 c + a/2 >= 15
Order of Operator Precedence Precedence Operator 5! * / % 4! + - 3! ==!= < <= > >= 2! && 1! Example: 2*a + b!= 3 && a*c 3/z < -5 c + a/2 >= 15
Order of Operator Precedence Precedence Operator 5! * / % 4! + - 3! ==!= < <= > >= 2! && 1! Example: 2*a + b!= 3 && a*c 3/z < -5 c + a/2 >= 15
Order of Operator Precedence Precedence Operator 5! * / % 4! + - 3! ==!= < <= > >= 2! && 1! Example: 2*a + b!= 3 && a*c 3/z < -5 c + a/2 >= 15
Order of Operator Precedence Precedence Operator 5! * / % 4! + - 3! ==!= < <= > >= 2! && 1! Example: 2*a + b!= 3 && a*c 3/z < -5 c + a/2 >= 15
Order of Operator Precedence Precedence Operator 5! * / % 4! + - 3! ==!= < <= > >= 2! && 1! Example: 2*a + b!= 3 && a*c 3/z < -5 c + a/2 >= 15
Order of Operator Precedence Precedence Operator 5! * / % 4! + - 3! ==!= < <= > >= 2! && 1! Example: 2*a + b!= 3 && (a*c 3/z < -5 c + a/2 >= 15) Like in math, we can use parentheses to force a different order.!
True/False Quiz a = 10 b = 50 a > 10 && b > 20 b==50 a == 10 && b >= 100 a==10 && b < 100 && a*b > 1000 a>5 && b<20 a==0 && b==0 a>20 b < 51 b-a*b > 0 a>5 b>20 && a==0 b==0 a=10 && b=50 a>5 (b>20 && a==0) b==0 a==10 && b >= 100 b == 50
Another If/Else Example // returns the smallest even number // among 2 ints; returns 0 if both are odd func SmallestEven(a, b int) int { if a % 2 == 0 && b % 2 == 0 { // both a and b are even, so return smaller one if a < b { return a else { return b else if a % 2 == 0 { // a is even but b is not return a else if b % 2 == 0 { // only b is even return b else { // both a and b are odd return 0 Why is this line OK? What would happen if we swapped this with the first case? % is the modulus operator: x % y is the remainder when integer x is divided by integer y.
Switch Statement switch statements let you express mutually exclusive tests compactly. // Returns the smallest even number // among 2 ints; returns 0 if both are odd func SmallestEven(a, b int) int { switch { case a % 2 == 0 && b % 2 == 0: if a < b { return a else { return b case a % 2 == 0: return a case b % 2 == 0: return b default: return 0 Each case part contains a condition, followed by a : and then a sequence of statements. The statements associated with the first true case will be executed. The optional default case is executed if none of the others are.
wikipedia user EMDX! Switch Is a Railroad Analogy
Switch Statement with Doomsday // KeyDay takes the month as input (between 1 and 12) // It returns the key doomsday of the month func KeyDay(month int) int { switch month { case 1: return 31 case 2: return 28 case 3: return 0 case 4: return 4 case 5: return 9 case 6: return 6 case 7: return 11 case 8: return 8 case 9: return 5 case 10: return 10 case 11: return 7 case 12: return 12
Switch Statement with the Skew Array 2 skew 1 0 1 2 0 5 10 15 20 C ATGGGCATCGGCCATACGCC // SkewValue computes the contribution of an individual // nucleotide to the computation of skew func SkewValue(symbol string) int { switch symbol { case A : return 0 case C : return -1 case G : return 1 case T : return 0
Guidelines for Programming 1. Get running programs first, then focus on formatting. 2. Comment your code more often than you think you should. 3. Don t write a single line of code before you plan (think about writing a short story). English first, then pseudocode, then Go. 4. Code the pieces that you know how to code first. 5. Code a little bit every day rather than a lot the night before an assignment is due. 6. Don t get bogged down on a single piece for more than 15-20 minutes. 7. Compile your code frequently. 8. Negative space is important you should be able to read code easily. 9. One way of doing something well > ten ways of doing it poorly.