 In the last segment, we discussed the break statement and the continue statement. Now we are going to discuss another statement called the for statement. This is a bit of a complicated statement and so let me state a problem which will sort of need the statement and where this statement will fit very nicely. So here is a problem, we would like to write a program to print a table of cubes of the numbers from 1 to 100. So what do we want? We want on the first line 1 and 1, on the second line 2 and 8, on the third line 3 and 27, on the fourth line 4 and 64, 5 and 125 and so on, all the way till 100. Now of course you can write this using the vial or you can also write it even using the repeat because you know that you are going to do exactly 100 iterations. So there is no real mystery about that. So what would you do? So for example you would write this, int i equal to 1, repeat 100 times, c out i then i times i times i and l and have a space in between. So this is and of course then you would have to increment i. So that would be the program or that would be the program fragment which will do what we want. Now it so happens that this idiom and by that I mean doing something for every number between some x and y. So this idiom occurs very frequently and therefore c++ provides you a way to describe it very succinctly and that is exactly the fourth statement. So this is how the statement works. So we want things to go between 1 and 100 and we want that variable to be called i. So we are going to write for int i equal to 1, i less than 100 and we want the value of i to change by i++. So we also specify that right here and then what we actually want to happen we specify after this and this is the body of the form. So we will get the exact statement in a minute. But this code is equivalent to the four lines I have written earlier and this code is very stylized and as a programmer you will get very, very familiar with it and just by looking at it you will know that oh i is going from 1 to 100 and this is going to be done for each value. Whereas in the previous statement these limits and this systematic update is kind of lost whereas over here it is not exactly a predictable place and therefore this makes for better readability. So exactly how this work we will see in a minute. So the general structure of the four statement so it looks like the following. So for then inside parenthesis first we have the initialization, initialization followed by a semicolon followed by a condition followed by an update and then the body. The initialization and the update are typically assignments they could be other things but they could be typically assignments and of course the semicolons are not included. Of course there is a semicolon immediately after the initialization but after the update there is no semicolon so that is something that you have to keep in mind. Condition as usual is a Boolean expression and the body well the body could be a single statement or it could be a block. So how does this execute? So before the first iteration of the loop the initialization is executed. So we will see the example in a minute but first the initialization is executed then the iteration begins then at the beginning of each iteration the condition is tested. If the condition fails if the condition turns out to be false then the rest of the iteration which includes the body is not going to be executed and in fact the loop will end the loop execution will end and you will go to execute the next statement following the false statement. If on the other hand the condition turns out to be true then the body is executed and after the body is executed the update is also executed then after the execution of the update the next iteration begins. What does that mean? Again the condition is going to be tested again if it fails the loop ends. So let us look at the flow chart. So shown here is the flow chart for the for then the for statement itself and an example. So what is happening over here? Well this is the for statement. So the initialization is executed first as in the flow chart then the condition is checked. If the condition is false then the statement goes to the next statement in the program. If the condition is true then the body is executed. So this body is executed and then if after the body is executed the update is executed. So the update is stated earlier but it is executed at this point and after that again the entire condition testing happens and so on. So what happens over here? So i equal to 1 is the initialization. So we start by saying int i equal to 1. So i becomes 1. Then we are going to check the condition. So this step happens if this condition is true which in the current case it is because 1 is less than or equal to 100. So then we are going to execute the body. So see that is what is happening over here. After we execute the body then we are going to execute the update. So at the end of the body after the body really this update is executed. So after this we increment i. So now i becomes 2. So we printed for i equal to 1 we printed 1 and 1 now i has become 2. So i has become 2 and we go back and we start from the condition check again. So we again execute this is i less than 100. So 2 is still less than 100. So we again print 2 and 8 and so on until we get to a value of 101 over here. So 100 will pass this and so we will print 100 and 100 cube but 101 we will not this condition will not be true and therefore we will exit this and we will go on to the next statement following the for. Now some remarks are in order. So as we saw in our example new variables can be defined in initialization. Now these variables are accessible inside the loop body including the condition and update but they are not accessible outside. So they are within the scope of that for statement. So that is something to be kept in mind. The intention is that those are sort of internal to the for statement and therefore we should not expose them outside. So sometimes so by and large this is useful and sometimes if we need something different we can of course do something different. Now variables which are defined outside can be used inside unless they are shadowed by the newly defined variables. So in the previous case over here I defined that i over here but suppose an i was defined over here then inside the body I am going to get this i not the i defined outside. On the other hand if I just wrote i equal to 1 and I did not write this int i then that means this i will refer to the variable that was outside. So then no shadowing will happen. So I am not obliged to declare a new variable over here but if I do then that variable may shadow a variable which of the same name if any defined outside otherwise I will just get a new name and that new name will vanish as soon as my for loop execution ends and break and continue can be used with the natural interpretation that is the moment you come to a break the statement of the for will end if you come to a continue then you do the update. So the rest of the body is going to be skipped. The typical use of the for is that a single variable is initialized and updated and the condition tests whether it has reached a certain value and the loop execution really depends very strongly on the value of this variable and therefore this variable is called the control variable of the for statement. So in the previous table of cubes program i whose cubes we were printing so that i the variable i was the control variable for this for statement. Now I am going to take a somewhat elaborate example a problem that you have seen before determining whether a number n is prime. So the manual algorithm is simple and in fact you have seen this manual algorithm earlier. So the algorithm was check whether any of the numbers between 2 and n minus 1 divides n. Systematically go from 2 to n minus 1 and check whether those numbers any of those numbers divides n. We looked at this problem last week but there our program was a little bit inefficient. Once we found that a number between 2 and n minus 1 divides n we know already that n is composite and we do not need to do any addition, need to do any additional divisions. Now our program will be efficient in that it will indeed not do any additional divisions whereas our last week last week's program would be doing those additional divisions. Even though the answer last time was also correct the last times last week's program was a little bit inefficient and this time we will be eliminating that inefficiency. So let us begin by making a flow chart of the manual algorithm and then we will try to use the FOR statement because it seems that the FOR statement is rather nice over here. You could think that the divisors which we are which you are going to try out should sort of be the control variables for a FOR statement that you might write. So that is our goal to see if this flow chart can be put into the format of the FOR statement. So here is the flow chart for the manual algorithm. So let me sort of tell you how this works. So first we are going to read n and then we are going to set a variable called divisor to 2. So this is the current divisor with which we are testing. Now if this divisor is smaller than n then we have to do something but if it is already become larger than n it has become n or larger then we take this branch. So when does it become n or larger? Well it becomes n or larger if we have tried out all the numbers between 2 and n and they have not been able to divide. So there was always a remainder when we tried to divide n by those numbers. So in that case we know that we can get out of the loop and we can in fact print the message prime. If on the other hand the current divisor that we are trying out is smaller than n then we are not yet sure that the number is prime. So we are going to check is n mod divisor equal to 0 or does the is n divided exactly by the divisor. If it is true then we have a founded divisor and therefore we can immediately declare that the number is a composite and we can stop. So that is what this branch is doing. But if it is false then we know now that this divisor did not work. But at this point we still cannot be sure, we still do not know whether the number is prime or composite. So far we have not found any divisors but we may find some divisors going further. So what do we do? Well we are going to increment the divisor by 1 so we are going to try the next divisor and then we are going to go and check again. So again if the new divisor is smaller than n or let us say it is bigger than n then we go out on this branch but then that means we have tried out all divisors only then has divisor become equal to n. So in which case we have tried out all divisors and we can get out and we can stop after printing prime over here. So that is the overall logic. Now this logic is perfectly fine. However flowcharts that you draw are expected to be structured and let me explain to me what that means. So basically when you draw a flowchart you can have paths flowing all over the play page. So let me show you one possible flowchart. So I am going to test something, sorry then from here if it is then I am going to do and execute, go and execute this block. Then again I am going to test something. Then if this is false I am going to go and execute this block and then from here maybe I come back and I merge with this point and if this was false maybe I go off and check something and then maybe from here also I merge at this point and otherwise I continue maybe I do another check, maybe I execute something and so on. So if your flowchart is really complicated like this then it is rather hard to understand. So such a flowchart or such a flow of program is often called spaghetti code. Why? Because you could imagine that these lines which are going in sort of random manners all over the place are like that pasta, noodles or whatever which are just lying around and I mean it just looks like a mess. So you really do not want your code to look like this. So what kind of code do you want? Well the code that you want is called a structured code or structured program sometimes. So what does that mean? So it means that I am going to do something, I am going to do something next. Maybe after at some point I am going to have a test but the test I may do something over here and eventually I am going to come back to the same point over here. I may do something, I may go back but so there might be a complicated blob over here but again I am going to come back to the same point and this complicated blob should be again some kind of a vile loop. So sorry it should be a vile loop or it should be a for loop or something like that. So I might have nesting. So I might have straight line code like this one of which one of the elements in it is a for loop and inside the body of the for loop maybe I can have a vile loop but the point is that this nesting should be sort of perfect in that manner. So it should not be like this where suddenly this line comes back and merges with here and this line comes back and merges with over here because if it is complicated like this then it is harder to reason with. So basically we want that even if we have multiple conditions so if we have a blob over here then things should enter over here and only one path of control should exit from this. So now if we go back in this code two paths of controller is exiting one part is exiting over here another path is exiting over here we do not want that. We have one entry into this so coming from the top and we would also like the code to exit in exactly one place. So how do we do this? So here is what we should do we should merge those two merge these two paths but now if we merge these two paths then on one side I am printing prime on the other side I am printing composite so I cannot just merge them I have to somehow print only one of the messages. So what we need to do is we could merge them but we could decide based on some information that we might have what message to print. So that is exactly what we are going to see next. So here this earlier portion is the same but instead of having two paths going out we have the same path coming into this two paths coming into this same block and here we are going to check is the divisor greater than or equal to n. If the divisor is greater than or equal to n then we know we came out on this path. But if we came out on this path we wanted to print prime so we print prime otherwise we must have come out on this path and therefore we want to print composite so we are printing composite. So with this simplification or rather maybe this is not a simplification maybe this is a complication but let us say with this modification our new flow chart it matches the structure of a force statement. So let me tell you how. So here for example I can think of as the condition in the force statement. This divisor equals 2 is the initialization in the force statement. This divisor plus plus is the update part of the force statement and what is this? Well this is the body but the body now contains a break and this break is going to take me outside but you know that the condition also takes me outside. So this is really the outside part. I should draw it over here saying that it is coming next after the force statement but it would be really congested over here to draw and therefore I have drawn it over here. But that is really the idea that this is the condition check this divisor equals 2 is the initialization part of the force. This is the body of the force and this is the update part of the force. So now we are ready to write the program. So here is the program. So we are going to read in N then we are going to initialize the divisor. Well here we have pulled out the initialization and what we have done is we have kept the initialization empty. But I could have declared the divisor over here and I could have initialized it over here. It does not really matter. So both are equivalent. So long as I put I do not redefine divisor over here then things are fine. There is a control variable and I will set its value over here. So what does this loop say? It says that start with value 2 for divisor and if that value so long as that value is smaller than n execute this body after this body is executed execute this update statement and then go back and repeat. And at the end of the for divisor now has a certain value. So if divisor has already become n if it is no longer smaller than n then we know that we must have tried out all the values between 2 and n minus 1 and so we print out prime. Otherwise we are going to print out composite and we are going to stop. So we have left the initialization part of the statement empty and this is allowed as we said earlier and we could have placed divisor equal to 2 in the initialization but not the end. And why do we not want the end to be placed? Because we want divisor to be used over here. So that divisor had better be defined earlier. If it is defined here then we would not be able to use it here. So an exercise for you suppose we indeed wrote end divisor equal to 2 what would this program do? So this is something for you to think about. Then here is one more exercise write a for loop which prints out the sequence 1 2 4 all the way till 65536 which is simply 2 raise to 16 and the end given to you is that the update part of the for does not have to be addition. It can be other operations like multiplication for example. So what did we discuss in this segment? We said that many times we need to iterate such that in each iteration a certain variable takes a simple sequence of values. Say the variable values a variable i goes from 1 to n. In such cases the for statement is very useful. The variable whose values form the sequence is called the control variable. Essentially that sequence is driving what is happening in each iteration. And we also noticed here as well as in vile that matching the flow chart of the manual algorithm to the structure of the vile or for is going to take some amount of work. So we will stop over here for this segment.