how does the computer evaluate expressions with multiple operators multiple function calls or even nested function calls that's a function call inside the parentheses of another function call to examine this order of operations let's trace a program with some complex arithmetic Expressions as always the first thing the computer does when we run the program is load the first instruction into its working memory when it sees an instruction like this where there's nested function calls it's going to search for the innermost parentheses first then then it starts evaluating from the inside out here in the innermost parentheses
we have the string how many servings this is already a single value so it doesn't need simplifying okay so the computer's going to peek outside of those parentheses and ask what did you want me to do with this value how many servings and it sees the input function the computer takes that value displays it in the console pops up a prompt and then waits for the user to enter something the computer's done with this part of the instruction but it won't continue evaluating until it gets an answer back from the user let's say I type
in five and hit enter where does that five go well the input function returns out whatever value the user entered remember that the input function always returns that entered value as a string so this isn't the integer five it's the string five great now we have a much simpler expression the computer goes to the parenthesis first sees that it's already a single value so there's nothing to do Peaks outside the parentheses and sees the int function the in function takes that string five casts it to an integer and returns out the value five now we
just have a standard assignment statement this statement tells the computer to go off to its memory allocate a new chunk of it to remember a new value then it tags it with the name servings and sticks that value five in there this instruction is now complete computer did everything so it's going to clear out that working memory and look for that next line of the program okay we load the second instruction into working memory looks like we're just doing some math here we follow PEMDAS or gemas that's parentheses exponents then multiplication and division and then
addition and subtraction so we've got parentheses first the computer evaluates 6 - 2 then we're left with only multiplication and division operators which are at the same level of Precedence so the computer just evaluates left to right first thing it sees is this variable servings it needs to go off and access that chunk of memory that was associated with the name serving so it goes here see servings follow follow that line and it finds the value five so we substitute that in here 5 * 4 is 20 then we have 20 / by 3 this
is the float division operator so we get 6. 666 repeating now it sees that equal sign and it says Ah you want me to assign this value I finally have to the variable spices it checks its memory and sees that it has no recollection of a spices so it allocates a new chunk tags it with a name and sticks that value in there for later all done clear it out this next line is blank so the computer skips that and then the line after that is a comment because it starts with this hashtag character so
the computer ignores that too comments are for humans not for computers load in that next line and here we have two sets of parentheses but those parentheses are at the same level of Precedence they're not one inside of the other so that means we're just going to evaluate them left to right starting with those left parentheses the computer is going to to grab that value of spices from its memory substitute it in now it has two values inside these parentheses separated by a comma but they're both simplified all the way down so we're good here
we pop out the parenthesis and we see the Min function and the Min function returns out the minimum of those two input values now it jumps to the next set of parentheses substitute in the value of servings that's five two single values pops outside the parentheses sees that it wants to take the maximum 5 is greater than 4 and now we just have two numbers to add together we have a single value now on the right hand side of the equal sign so we can go ahead and do that assignment checks in its memory and
sees oh I already know a spices so I'm just going to erase what's in there and replace it with this new value cool clear out working memory move on next slide now that's a lot of parentheses we're starting in and we're moving out we've got an expression here that needs simplifying we're substituting in spices and then we're adding three to get that down to a single value now everything inside here has been simplified so we peek out and we see the round function the round function tells the computer to take that first value and round
it to the number of decimal places that the second value says here we're rounding to two decimal places now we're in the next innermost parenthesis we already have a single value so we pop outside those parentheses and we see the string function the string function casts this to a string and returns out that value now we're in the last set of parentheses we're concatenating two strings so we just smoosh them together and what should it do with this value oh print it there's no next line so it terminates the program execution and when it terminates
that means the computer also forgets everything in its short-term memory so all these values go away too that's it for this program remember if you ever have a complex expression the computer always evaluates innermost parentheses to out most