 So, these are lectures 23, 24, 25 and you should be able to answer these questions if you have watched these lectures. After we complete this, we will go back and actually have a quick recap of these lectures and then we will also discuss these answers, but right now you have to write the answers. So, this question is consider the following for loop in C plus plus for x semicolon y semicolon z w is the body, which of the following is correct? So, a says x, y and z may be empty, but w cannot be, b says x, y, z and w may all be empty, c says if y is not empty, x and z cannot be empty and d says none of the above. So, you just have to recall that lecture, you should be able to answer this question because this was explained in that lecture. So, done with this next question, which of the following is a C plus plus expression? There may be more than one correct answer, so you will have to write all those which apply, the choice a is x equal to y plus z, b is x plus plus, c is while true x plus plus and d is break, so which of the following is a valid C plus plus expression? There was considerable discussion on the concept of expression in one of the lectures, those of you have seen that should be able to very quickly identify. Let us go to the third question, as you know an equal to symbol, single equal to symbol is often used in the assignment statement, but it also represents an expression as an operator. So, the precedence of equal to operator is same as that of plus and minus, is less than that of plus and minus, more than that of star and slash and less than that of and and and odd, logical and and logical. So, when you do an assignment, the assignment precedence is where? That is the question, the equal to symbol which stands for assignment, what is its precedence? Highest, lowest, higher, lower, higher than something, lower than something, these are the choices, again more than one choice may be correct, please be careful when you take, all right. So, this is about the continuous statement in a for loop, so what does the continuous statement specify, the first choice says it specifies that the loop must execute continuously or infinitely, the second choice specifies that it causes the next loop iteration to start, choice C says causes the program to terminate immediately and choice D says causes the loop to exit immediately, so comment on what does the continuous statement in a for loop do, by ticking one or more of the appropriate choices. Again, those of you who have watched the lecture which explained the for loop and the continuous statement used inside the for loop should be able to answer this very quickly, should not take more than 10, 15 seconds, you rewritten answer it. This is the fifth question on the lectures, say for loop is given here, notice that the for loop has multiple statements in the initial thing, multiple actions in the initialization part and multiple actions on the termination part, so you say i equal to 0 comma j equal to 10, j greater than equal to i, j minus minus i plus plus, these are post decrement and post increment operators and x is equal to x plus y, which is the body of the loop, now which of the following are correct, first choice is i equal to 0, j equal to 10 will cause a compilation error, b, j minus minus i plus plus will cause a compilation error, C both a and b are true that means each one will cause a compilation error and D none of a and b, there will be no compilation, straight forward choices, so basically you have to decide upon the legitimacy or otherwise of writing such expressions as component of the for specific, again you should be able to answer this question straight away, if you have seen the lecture, here is the last of the recap with questions, question 6, which asks you that when we specify loop invariance as commands, loop invariant is a term which was introduced in the lecture and explained, when we specify loop invariance as commands, the purpose that it serve is again could be more than one, a helps the compiler to optimize the loop, b helps us reason about the loops correctness, c helps the compiler ignore errors in the loop and d helps the computer skip executing the loop, please read the question carefully, specifying loop invariance as commands in your program serves which of the following purpose, again this should be a straight forward question, so have you finished writing the answer, not yet, even if you have never heard the term loop invariant, you should still be able to answer this question, alright now have you finalized your answer, so here is now a recap, for those of you who missed seeing these lectures or missed understanding them, is a quick recap of what was covered in these lectures, so we have seen that c plus plus allows assignment to be as an operator in an expression, but it has side effect, so the point is that when assignment is treated as an operator in an expression, then it has side effects, so x is equal to y plus z is an assignment, now as a statement if a semicolon comes, it becomes a statement and it assigns the value of expression y plus z to x, however as an operator when we just write x equal to y plus z, it has a side effect that the value of expression y plus z is stored in x and type and value is same as those of y plus z or r h s of equal to z, so please note that this entire thing treated as an expression will have type and value which is determined by the right hand side, although the left hand side determines the conversion before the assignment, but as an expression the whole thing is treated like this, this is a nasty side effect in c plus plus but can be exploited, suppose I have four statement with such assignments, so I have count equal to something loop condition and count equal to count plus one and then block of statements, now this is also an expression with side effect and the expression has the value which is the value of r h s, same thing here, again we had seen the special operators which are actually nothing but short cut symbols, so instead of saying x is equal to x plus one, you can just say x plus plus, the value is that of x before incrementing, this is called the post increment, so y is equal to x plus plus, x will be incremented and will be assigned to y, so x plus plus as an expression, the value of y is ten, value of x is two, the value of y will now become two and value of x will be three, so please understand the meaning, x itself will be incremented but it will be incremented after its value is used, so the value of this expression is two which is assigned to y, only after assigning two to y, x will be increased, that is why it is called post increment, ordinarily one should never use such statements, very dangerous because of the impact that you have, you might think that y will actually become three, it does not, so you use increment operator only when you want to increment the value of a single variable, don't use it as part of the normal long assignment statements and so on, extremely dangerous, pre increment on the other hand will increase the value of x before it is utilized, so value of x will become three in any case but y will also be assigned value three because before assignment the increment will take place, so increment decrement variable by an expression, you can write this as x plus equal to x minus equal to x star equal to x slash equal to x percent equal to, this is same as x equal to x percent phi, this is same as x equal to x divided by phi, so basically whenever you want to change the value of a variable by any operation of adding something to it, subtracting something from it, dividing it by something or multiplying it by something or taking a model of it, you can use this, these are shortcuts only in terms of symbolism, again I would submit that is a good programming practice, you use this very clearly and succinctly and not in any complicated expression, as a standalone thing this could be, the reasoning about the loop, a loop iteratively transforms relations between variables and the precondition holds when we start iterating for first time and post condition holds when we exit the loop, by the way this is very important to understand how the loop functions, you invariably will write a loop, execute the loop for certain values of input variables, changing variables etcetera and figure out that your logic is correct, but mathematically you can actually reason out that some condition has to be write before you enter the loop, some condition has to be write after you exit the loop and these are called preconditions and post condition and these are special cases of relation which hold invariantly every time, so we call them loop invariant, it is desirable integer valued metric monotoleically changes towards fixed value, so loop variant ensures loop terming, so if nothing varies inside the loop, loop will become an infinite, nothing that is of consequence in terms of discernment, so within the loop there has to be a loop variant and there are some loop invariant, now consider these questions very quickly, anybody to share the answer for this x, y and z may be empty, but w cannot be, so you can always say for in bracket semicolon semicolon bracket close w, what does this stand for and infinite loop, w will be executed infinite, b x, y, z and w may all be empty, if y is not empty x and z cannot be empty, again here, so which of the following is a c expression, x is equal to y plus z, x plus plus, while true x plus plus, break the precedence of equal to as an operator is less than that of logical and logical or same as that of plus and minus, but less than that of plus and minus is true, in fact as an operator, the assignment operator is a very kadu fellow, it has a very low precedence, unless everything else is done, the compiler doesn't look at equal to, the continue statement in the for loop, does it specify that the loop must continue infinitely no, it causes the next loop iteration to start, program does not terminate, does not exit immediately, if you want to exit the loop, what the statement will use, break, none of a and b, there will be no compilation error, what will be the behavior of the program by the way, so here is a constructive question, why and where would you ever think of using such a structure, very difficult to answer it unless you have actually written programs which require such construct, so please remember so far in this course, we have been discussing again and again and again, very emphatically how things work inside c plus plus, how different commands are executed, what is the interpretation, what are the side effects and so on, from now onwards will be actually writing programs, so using these features to write program, when you write programs, the standard thumb rule is don't use any complicated logic, however you must understand what happens if in some program such statement, this one, specifying loop invariance as commands, b it helps us reason about the loop's correctness, for those of you who are looking blank, please remember that a command is meant for human being, compiler completely ignores that command, so it is impossible that the compiler can optimize the loop by looking at a command, it is impossible for compiler to ignore errors in the loop and it cannot help computer skip anything, a command is a command meant only for human beings, alright. So now we start practice problems, so there is a fragment of the program, yeah this is not a quiz but this is almost like a quiz, meaning this is homework and this is lonely homework, so no discussion at this juncture, you have to solve this problem, the problem says consider the following program fragment, variables i, j and n are declared as integer, I read the value of n and given an n I have to do the following, for i equal to 0, j equal to something, semicolon, so this is obviously the whole thing is the initial part, then the condition for loop continuing or loop exit, this is the second set of question mark and the last one, the post iteration activity and then you are outputting j, please note that you are outputting j every time an iteration executes, so if the iteration executes 10 times, it will have 10 output set, if iteration executes 2 times it will have 2 output set, fill in the missing parts, so that the program fragment outputs the first 5 powers of n, that is n raise to 1, n raise to 2, n raise to 3, n raise to 4 and n raise to 5, whatever is n, if you were to write a program to generate output given an n, write a program which will produce n raise to 1, n raise to 2, n raise to 3, n raise to 4, n raise to 5, you would have written a program on your own but here somebody has written a program with missing thing, so fill up the gaps, please note that you are not supposed to write your own program, this problem can be solved in 5 different ways, this is one way in which the problem solution is given but some parts are missing, all right, may I have your attention please, I think all of you should have figured it out, I randomly picked up 2 notebooks, one from one ribu and the other from shital jn, both solve the problem in exactly the same fashion, he starts with j equal to n along with i equal to 0 and increments j by multiplying it by n, so that the output is successive power of n, exactly the same thing done even here, so starts with i equal to 0 and j equal to n, please note that i equal to 0, i less than 5 and i plus plus are actually the specifications to execute the loop 5 times, the j is multiplied by n and assigned back to j, so j finds out successive power, everybody got this, now this is important, you must understand how such a construct would work, however if you were to write a program, would it not be better to write something like this, so I define in n i n power, so instead of using j or some such variable names, I would rather use a variable name which actually reflects what I am trying to compute, so I could use power of n, power n, n rest whatever, I will initialize that to n because that represents n rest to 1, for i equal to 0, i less than 5, i plus plus, I multiply power by n and I output the power, will I get the correct answer and how can it be corrected, it can be corrected in one of the two ways, one I can start with power equal to 1, in which case the first multiplication of n will get me the power to be n rest to 1 and will get printed, the other I could swap the output statement with the computation of the new power, the easiest thing would be to start with power equal to 1, the point being made is a complex for statement almost without any body or very limited body, perhaps a better programming way is to write what you intend to compute separately and use for only for controlling the iterations, so this will take some evaluation, what will be the output on executing the following C plus plus program fragment, so this question you have to attempt on your own for next two minutes, it is best to write x and y on the right hand side of that program and keep writing the values of x and y as the program proceeds, so initially in the declaration itself x is 10 y is 2, now as you execute statements the values of x and y may change, so write down where they change, how they change and what output is produced, not a very difficult question, so you should be able to get it, so here is an answer from Priyanka Lal and his neighbour, they think that they will get 10, 1, 12, 2, how many of you do not agree with this answer, a large number of people, so Priyanka you and your neighbour had a complete understanding that your answer is correct, now you will have to rethink, so let us quickly reason this out, so I start with x equal to 10 and y equal to 2, I am outputting x plus plus, so what will be output, 10 because it is a post increment, so the value of x will change after the value has been used, the usage will be with x equal to 10, but at this point, after this point x has become 11, minus y y, 1 because it is a pre increment, so first the value of y will be decremented by 1, the y becomes 1 now and then that value will be used, so the first two numbers there is no dispute 10 and 1, please note that when I come out of this the value of x is, value of x is 11 and value of y is 1, alright, now I come here x plus y plus plus, this is a post increment, the y was 1 after completion of this entire thing y may eventually become 2, but the value of y that is used is the value which was there before it came here and that was 1, so the value 1 will be used, what is the value of x at this point, 10, so 11 because it was incremented, so 11 plus 1 will become 12 and 12 is what will be printed, however after completing this task if you come here the value of y would be 2, so this is the impact of the post increment, now since the value of y has become 2, when you come here you will print y which is 2, the brackets in y plus plus have an effect that y plus plus will be executed first, so isn't c out executed from right to left? c out is an output statement and it outputs things as it encounters them, so this is one redirection operator, this is another insertion operator, it will insert this value in the stream first, then it will insert this value in the stream, it will always output in the order in which the elements are written, c out is not an expression where there is a precedence and thing like that, each one of these entities are independent entities, the first thing it will output will be the one which is immediately written after this, then it will output this, then it will output this, then it will output this, that is the order in which c out will execute, alright let's go to the next problem, so this is a problem which requires you to do some execution in a loop, what will be the output on executing this code fragment, so I have num declared and initialized to 0 and i both of them are integer variables, I have a simple iteration for i equal to 0 i less than 10 i plus plus, so this much is obvious that the body of the loop will execute 10 times, now what will happen inside, if i modulo 2 equal equal 0 continue, else output num plus equal to i followed by a comma, when this loop is completely executed what will be the output, I suppose all of you will agree that the output will be a sequence of numbers, but the loop is executed 10 times, will it produce 10 numbers as a sequence or will it produce less, it will produce less because there is an if condition, so work that out, write down the value of i as it changes from 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, please note that the moment value of i becomes 10 you will exit, for i equal to 10 you will not execute this loop and now write down what will be the output, note that num starts with 0, this is not a difficult question but you will have to look out the possibility, quickly do that should take more than a minute actually, just have to like the computer like c plus plus you just have to execute these statements one after another, so how many of you have agreement on your answers, large number of them, now everybody has figured out the answer, I just show you two slides this is the answer that 149, 16, 25 wrong, what is the right answer, so what should be the answer 0, 1, 4, 9, 16 and 25, any other answer 1, 4, so what is ashu missing here, rest of it is ok, so ashu you missed the comma, but somebody has come up with an answer 0, how many people start with 0, let us go back to the question, i starts with 0, initial value of num is 0, so if at all in the first execution of this iteration, if c out statement is executed, then c out statement is executed, so what is the answer 0, 1, 4, 9, 6, 7, 8, 9, 9, 9, 9, 9, 9, 9, 10, 11, 12, 13, 14, 14, 14, 15, 16, 16, 17, 18, 19, 19, 19, 19, 19, 19, 19, 19, total execution of this iteration if c-out statement is executed then since i is also 0 num is also 0, num is equal to num plus 0 will still be 0, and 0 will be output, how, however when you calculate this what is 0, modulo 2, it is 0, so if it is 0 the loop will simply So, if it is zero, the loop will simply continue for the next iteration. You will never come to the see-out statement. Is that clear? So you will not execute the output instruction at that point in time. Alright. Now this is the next question. Now let's consider this question. We want to extend the problem of computing the nth power of m using repeated sparing from the previous lecture. We had seen that last time that if n is a power of two, then we can compute m raised to n using log n to the base multiplicates. So for example, if I have to compute the power 8, then I can first square, then square with square, and then multiply the whole thing with again, so I should be able to get that. Now if n is not a power of two, we would like to compute m raised to n using minimal number of multiplicates. So this symbol, mathematically many of you would know, what is this? It's called the seal function. There is a floor function, there is a seal function. So log n to the base two, whatever is the largest value, using two into this multiplicates. So you actually do as less number of multiplications as possible to compute m to the power n. Now there is a hint on how this could be done. You have to of course loop and iterate. But what you can do is, suppose n, you have to find out m raised to n, and n may not be an exact power of two, that is the contention. What you do is, you look at the unsigned integer representation of n in binary. It is one one zero one. So one one zero one is actually the value of n, then m raised to n can be computed as m raised to eight into m raised to four into m raised to one. So please note, n is not a power of two, but n represented as a binary number is one one zero one. And m raised to n actually can be computed as m raised to eight into m raised to four into m raised to one. Now you know how to compute m raised to r, where r is a power of two. So you can successfully use that algorithm first to compute m raised to one, which is power of two to the power zero, m raised to four, which is two to the power two, and m raised to eight is two to the power three. And multiplying all of them, you would get m raised to, how much is one one zero one in decimal. So if you want to compute m raised to n, where n is some such odd number, you can actually do this by calculating a binary representation extract. So basically you can implement the algorithm of finding out a binary representation of a decimal number given, pick out the ones and the corresponding powers and do the iteration that many times. This is complex, you have to write the program, there is no quiz here, you have to write the program to implement this. It is a tricky question, but the trick is not for the sake of a trick. It actually involves serious consideration of efficiency of an algorithm. A computer can perform certain computation by doing something ten thousand times, but if you can write an algorithm which can do exactly the same thing by making the computer do something only thousand times, then obviously you are writing a better algorithm. And if you can make the computer do the same thing by repeating some activity only for hundred times, not even thousand times, not ten thousand times, but you achieve exactly the same result, then obviously your program is much better. Computers are fast, but there are tasks which are so huge in size that they would take a very long time to execute if your algorithm is not efficient. So this is a direction in which how you can make certain computations more efficient by using tricks like this. The question is very simple, if n is not a power of two, we would like to compute m raised to n using these many multiplications. So what you are doing, you are optimizing on the number of applications that the computer has to do. We are perilously close to the end time, so I would suggest please write down this question on your notebooks. The question is compute m to the power n, compute m to the power n and compute m to the power n if n is not a power of two, compute m to the power n using the number of multiplications which are at most given by this, two into log n to the base two, see you note that log n to the base two may not be an integer number, otherwise you don't require a seal function. So that should be the maximum number of multiplications that are required. Why are such things important? Suppose you achieve a result in five multiplications whereas otherwise it would require twelve multiplications. Now five multiplications and twelve multiplications both would be done in few microseconds, but the difference between five and twelve is more than double. Then now if you have to execute twenty billion operations like this, then if you do those twenty billion times those operations, one involving only five multiplications, other involving twelve multiplications, which efficient program is better. Basically in terms of real time, if an algorithm takes one hour to do some computations using the normal long method, it would take one hour if your algorithm is efficient, that's the point. What I would suggest is start discussing among yourselves and then go back home and then complete the solution, but use the next three, four, five minutes to discuss this among yourselves to formulate an approach or the algorithm. Thank you so much.