05 - Iteration and Conditionals

# 05 - Iteration and Conditionals - Engineering 101 Quote of...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Engineering 101 Quote of the Day It is not hard to learn more. What is hard is to unlearn when you discover yourself wrong. - Martin H. Fischer Iteration and Conditionals Before we get started... A note on the increment/decrement operators: x++; is the same as x = x + 1; Increment Operator x--; x--; is the same as x = x - 1; Decrement Operator Before we get started... And some more nice little shorthands: shorthands: x += 5; is the same as x = x + 5; x -= 7; is the same as x = x - 7; Iteration So far we have only used sequence. sequence. Sequence allows for the computation of some complicated formulas, but not the creation of dynamic programs. Iteration allows the computer program to employ repetition to achieve a goal. Iteration often allows us to devise more general solutions to problems. while statement Iteration can be accomplished with a while statement in the form while (condition) loop_body The condition evaluates to True or False. The loop body is an expression or compound statement in {braces}. If the condition is true the loop body executes, then the condition is checked again. The loop body is repeatedly executed as long as the condition holds. holds. 1 Example: Combinatorics How many ways can you choose 5 balls in groups of 3? 123 124 125 134 135 145 234 235 245 345 10 ways! Example: Combinatorics In general if you are choosing from m things n at a time: m m! C = = n n!(m - n )! So choosing from 5 things 3 at a time C= 5! 5 4 3 2 1 120 = = = 10 3!2! (3 2 1)(2 1) 6 2 Example: Combinatorics To compute this Factorial Function // factorial, assumes N is positive int fact( int N){ int count=1, f=1; while (count <= N){ f = f * count; count = count + 1; int main( ){ } cout <<fact(5); return f; return 0; } } m m! C = = n n!(m - n )! We need to compute the factorial function. Write a function to compute N! Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ int count=1, f=1; while (count <= N){ f = f * count; count = count + 1; int main( ){ } cout <<fact(5); return f; return 0; } } Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 1 int count=1, f=1; while (count <= N){ f 1 f = f * count; count = count + 1; int main( ){ } cout <<fact(5); return f; return 0; } } 2 Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 1 int count=1, f=1; while (count <= N){ f 1 f = f * count; count = count + 1; int main( ){ } cout <<fact(5); return f; return 0; } } Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 1 int count=1, f=1; while (1 <= 5){ f 1 f = f * count; count = count + 1; int main( ){ } cout <<fact(5); return f; return 0; } } Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 1 int count=1, f=1; while (True){ f 1 f = f * count; count = count + 1; int main( ){ } cout <<fact(5); return f; return 0; } } Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 1 int count=1, f=1; while (count <= N){ f 1 f = f * count; count = count + 1; int main( ){ } cout <<fact(5); return f; return 0; } } Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 2 int count=1, f=1; while (count <= N){ f 1 f = f * count; count = count + 1; int main( ){ } cout <<fact(5); return f; return 0; } } Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 2 int count=1, f=1; while (2 <= 5){ f 1 f = f * count; count = count + 1; int main( ){ } cout <<fact(5); return f; return 0; } } 3 Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 2 int count=1, f=1; while (count <= N){ f 2 f = f * count; count = count + 1; int main( ){ } cout <<fact(5); return f; return 0; } } Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 3 int count=1, f=1; while (count <= N){ f 2 f = f * count; count = count + 1; int main( ){ } cout <<fact(5); return f; return 0; } } Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 3 int count=1, f=1; while (3 <= 5){ f 2 f = f * count; count = count + 1; int main( ){ } cout <<fact(5); return f; return 0; } } Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 3 int count=1, f=1; while (count <= N){ f 6 f = f * count; count = count + 1; int main( ){ } cout <<fact(5); return f; return 0; } } Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 4 int count=1, f=1; while (count <= N){ f 6 f = f * count; count = count + 1; int main( ){ } cout <<fact(5); return f; return 0; } } Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 4 int count=1, f=1; while (4 <= 5){ f 6 f = f * count; count = count + 1; int main( ){ } cout <<fact(5); return f; return 0; } } 4 Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 4 int count=1, f=1; while (count <= N){ f 24 f = f * count; count = count + 1; int main( ){ } cout <<fact(5); return f; return 0; } } Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 5 int count=1, f=1; while (count <= N){ f 24 f = f * count; count = count + 1; int main( ){ } cout <<fact(5); return f; return 0; } } Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 5 int count=1, f=1; while (5 <= 5){ f 24 f = f * count; count = count + 1; int main( ){ } cout <<fact(5); return f; return 0; } } Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 5 int count=1, f=1; while (count <= N){ f 120 f = f * count; count = count + 1; int main( ){ } cout <<fact(5); return f; return 0; } } Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 6 int count=1, f=1; while (count <= N){ f 120 f = f * count; count = count + 1; int main( ){ } cout <<fact(5); return f; return 0; } } Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 6 int count=1, f=1; while (6 <= 5){ f 120 f = f * count; count = count + 1; int main( ){ } cout <<fact(5); return f; return 0; } } 5 Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 6 int count=1, f=1; while (6 <= 5){ f 120 f = f * count; count = count + 1; int main( ){ } cout <<fact(5); return f; return 0; } } Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 6 int count=1, f=1; while (6 <= 5){ f 120 f = f * count; count = count + 1; int main( ){ } cout <<120; return f; return 0; } } Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 6 int count=1, f=1; while (6 <= 5){ f 120 f = f * count; count = count + 1; int main( ){ } cout <<120; return f; return 0; } } The Heart of fact() // factorial, assumes N is positive int fact( int N){ int count=1, f=1; count=1, while (count <= N){ f = f * count; count = count + 1; } return f; } The Heart of fact() // factorial, assumes N is positive int fact( int N){ int count=1; count=1; while (count <= N){ do something count = count + 1; } return f; } The Heart of fact() // factorial, assumes N is positive int fact( int N){ int count=1; count=1; intialization while (count <= N){ do something count = count + 1; } return f; } 6 The Heart of fact() // factorial, assumes N is positive int fact( int N){ int count=1; count=1; intialization while (count <= N){ do something count = count + 1; increment } return f; } The Heart of fact() // factorial, assumes N is positive int fact( int N){ int count=1; count=1; intialization while (count <= N){ continuation condition do something count = count + 1; increment } return f; } Using fact() m m! C = = n n!(m - n )! Combinatorics Our program is inefficient. Why? // factorial, assumes N is positive int fact( int N){ int main( ){ int count=1, f=1; int m, n, ans; cout <<"Input m & n"; while (count <= N){ cin >> m >> n; f = f * count; ans = fact(m) / fact(n); count = count + 1; ans = ans / fact(m-n); } cout << ans; return f; return 0; } } Which function computes m!/n! ? Combinatorics Our program is inefficient. Why? 6!/3! = 6 x 5 x 4 : two multiplications as opposed to 7 multiplications and 1 division. So, which of the following functions computes m!/n! ? int fact2( int M, int N); 7 A Little Fun A Little Fun Dr: Mom: Dr: Mom: Dr: I'm afraid your son has...the Knack. The Knack? The Knack - It's a rare condition characterized by an extreme intuition about all things mechanical and electrical, and...utter social ineptitude. Can he lead a normal life?!? No...he'll be an engineer...there there... Relational Operators Recall that a while loop always includes a condition. condition. As long as the condition is true the loop body continues to be executed. Relational Operators A condition is an expression that will evaluate to either true or false. false. The computer recognizes the literal expressions "true" and "false". They are Boolean values and there is a "false". simple type bool that can only adopt these two values true evaluates to 1 and false evaluates to 0. A condition typically involves a relational operator as part of an expression. Is A greater than B? Is C equal to 5? Relational Operators Operator Meaning less than greater than less than or equal to greater than or equal to is equal to is not equal to Relational Operators 10 10 10 10 9 9 Arity binary binary binary binary binary binary Placement infix infix infix infix infix infix Precedence < > <= >= == != The relational operators have lower precedence than the mathematical operators (+ - * /), but higher precedence than assignment (=) Note that "=" means assignment and "==" means "is equal to?" These are often confused. 5 + 4 == 9 3 * 6 <= 2 * 2 true false all associate left-to-right 8 Relational Operators You can assign the results of conditional expressions to identifiers. bool a; a = 5 <= 4; a = a == 0; cout << a; Relational Operators You can assign the results of conditional expressions to identifiers. bool a; a = false; false; a = a == 0; cout << a; Relational Operators You can assign the results of conditional expressions to identifiers. bool a; a = false; false; a = 0 == 0; cout << a; Relational Operators You can assign the results of conditional expressions to identifiers. bool a; a = false; false; a = true; true; cout << a; Relational Operators You can assign the results of conditional expressions to identifiers. bool a; a = false; false; a = true; true; cout <<true; <<true; Relational Operators You can assign the results of conditional expressions to identifiers. bool a; a = 5 <= 4; a = a == 0; cout << a; output: 1 9 True or False? Evaluate the expressions 6 + 5 == 5 * 2 4 * 3 + 2 >= 6 * 2 5 + 5 + 5 != pow(4.0, 2.0) 10 / 3 == 3 10.0 / 3.0 == 3.0 10 % 3 == 3 4>3>2 True or False? Evaluate the expressions 6 + 5 == 5 * 2 4 * 3 + 2 >= 6 * 2 5 + 5 + 5 != pow(4.0, 2.0) 10 / 3 == 3 10.0 / 3.0 == 3.0 10 % 3 == 3 4>3>2 11 == 10 False 14 >= 12 True 15.0 != 16.0 True 3 == 3 True 3.33... == 3 False 1 == 3 False 1 > 2 False Logical Operators Operator Alternate Arity unary binary binary Operator Precedence Precedence 15 5 4 Placement prefix infix infix Operators ! */% +<<>> <><= >= == != and or = Meaning not, sign change multiplication, division addition subtraction insertion extraction less than, greater than is equal / is not equal and or assignment Precedence 15 13 12 11 10 9 5 4 2 not and or ! && || and and or associate left-to-right not associates right-to-left Evaluate the Expression 5 <= 6 and 5 > 2 == 1 or 6 + 5 > 10 1- True 2- False Evaluate the Expression ! ! ! ! ! ( 5 == 4 or ! ( 3 > 2 ) ) 1- True 2- False 10 Evaluate the Expression ! ( (a=3) > 2 && a > 1 ) 1- True 2- False Selection Next Lecture 11 ...
View Full Document

Ask a homework question - tutors are online