 In the last segment, we discussed some general information about the course, how to install CPP and we wrote a program for drawing a square. So now I am going to show you a better way of drawing a square. So here you can see that we have the same first three lines but instead of typing in forward 10 write 90 several times, I have put it inside a repeat statement, a so called repeat statement. So as you can see, after repeat there is four which tells you how many times to execute what is in the braces following the repeat. So basically this statement is going to execute forward 10 write 90 four times and after that it is going to wait for 10 seconds. This will also draw a square but it is a nicer way to draw a square because I do not have to type it so much. If I wanted to draw a 100 sided polygon, I would have had to type things 100 times but with a repeat statement, I can just say repeat 100 times. Well, what does a repeat statement do in general and what does it look like? Its form in general is going to be this, repeat followed by parenthesis inside which there is going to be a number, let us call it x and then there are these braces inside which there can be as many statements as you like and collectively let us call these statements y, y, y. So what a repeat statement does? Well, it causes these y, y, y statements inside the braces to be executed x times. So we already saw an example, we had x equal to 4 and y, y, y was forward 100 write 90. So those two statements were executed four times. Now the statements y, y, y in general whatever is inside those braces is called the body of the repeat statement and each single execution of y, y, y is called an iteration. So this repeat statement causes x iterations of the body y, y, y. Now we can use the repeat statement quite nicely to draw a polygon. So here is the program and we will execute this later but let us just see this. So it begins with include simple cpp and main program as before then we start up turtle sim and now there is a different statement. So this statement is a see out statement and it says see out its form is see out less than less than message. Now see out over here can be thought of as a command but you can also think of it as the screen. So this statement will print whatever you type after those less than less than on the screen. So in this case you typed how many sides. So this message how many sides will go on the screen. After that another statement that we have not seen int and sides. int and sides is a rather complicated interesting statement. So first of all it tells the computer please reserve a cell in memory for me in which I am going to store some integer value. How to store that value I will tell you a little bit later but what we are doing over here is we are reserving a cell in memory. Not only are we reserving but we are going to tell the computer that look from now on if I use the term insides you should treat it as referring to this cell. So essentially I am giving insides as the name for the cell that I just reserved. Now you can choose the name as you wish almost we will see that in some later lecture and I just want to point out over here that int is an abbreviation of integer. So you are telling the computer that I am going to store an integer and instead of writing down int eger you can just type int which is the abbreviation. After that there is another new statement see in greater than greater than insides. Well what does this statement do? Well it commands the computer to wait until the user types in a value from the keyboard. So the statement asks the computer to wait until a value is typed. So the user will see the message how many sites and therefore the user will type a value. So this value whatever it is typed let us say the user types 10 that value 10 will come in and into the computer and the see in statement will throw it into the cell insides which we just reserved. So think of the greater than greater than as arrows and think of see in as representing the keyboard. So this statement is going to cause a value to flow from the keyboard to the cell in memory called insides. Of course this statement will cause the program to wait until you actually do the typing. If you do not type then the program will just wait so you should type. After that we have a repeat statement but now the repeat statement has gotten more interesting. We do not have a number inside the parenthesis but we have the name of a cell. So what does this mean? So it says repeat as many times as whatever the value is contained in the cell insides. So we just said that the user might have typed 10 when the see in inside statement executed. So if 10 was sitting in insides then the loop the body that is going to be following will be repeated 10 times. So what is in the body? Well we are going to go forward 100 and now we are going to turn right but instead of turning right 90 or some fixed value we are going to turn right 360 divided by insides. What is this 360 divided by insides? Well it is a mathematical expression and you are instructing the computer to evaluate this mathematical expression. So you are turning the computer divide 360 by whatever is contained in the variable insides. So if we type 10 then insides will contain 10 so as a result of this we will have 36. So the computer will turn 36 degrees. Notice that if there are 10 sides to a polygon then there are 10 exterior angles. If there are 10 exterior angles each of which adds to 360 then each angle has to be 36. So this is exactly the angle that we need in order to draw a decagon. So this is what this repeat loop is going to do. After that the program will wait for 10 seconds and then the program is over. So let us again see a demonstration of this. So this is the same editor and this is the program that we just saw. So I am going to go to the new program polygon.cpp which is what we just wrote. So you can see it is reserving a cell in memory called insides and these cells in which we store values are often called variables. And then it is turning right by 360 by insides and the repeat loop goes insides times. And I put in a weight 5 over here whereas earlier I had said weight 10 does not really matter. So let us compile that program. So this is polygon and let us execute. So we have our turtle. So it has asked us a question. How many times? How many sides? So I am going to respond 10. Let me just move this so that I can see both. So I am going to type 10 but the statement does not work just by typing 10. I have to hit return as well. So now it ended up drawing a polygon. Well there is some problem over here that polygon was a little too big and so it did not fit in that window that we had. Well that is not a big deal even if the polygon part of the polygon went outside the turtle knows how to come back. But let us just execute it one more time so that you can see a full polygon. So this time when it says how many sides let me type 6. So now it draws hexagon and you will see that it has turned exactly the right amount. How did it know how much to turn? Well it turned 360 by n sides and so in that case it turned 360 by 6 which is 60 degrees. So we have seen one more program to draw polygon and I just now want to point out that there are many more commands available at our disposal. So we do not have to always turn right we can turn left and if you want to turn left through 8 degrees we can say turn left 8 degrees. Well actually we do not really need that left command because if we say right minus a then that is really equivalent but just for convenience that left command is also provided. Two additional commands are there pen up and pen down and these respectively cause the pen to be raised and lowered. What does that mean? Well the drawing is going to happen only if the turtle moves while the pen is low. If you raise the pen if you issue the pen up command and then move only the turtle will move no drawing will be done. So after that you need to put down the pen again by giving the pen down command and only then will any drawing happen from that point onwards. You can do mathematical calculations so for example you can use the command square root of x. So this is going to cause the square root of x to be computed and instead of this square root of x essentially that actual square root will appear and so then you can use it directly in your calculations. Similarly you can use sine x cosine x tangent x and so on. So here x should be in degrees in scientific computation it is more customary to use radians so actually the abbreviated forms are allowed. There also are commands for arc sine arc cosine and lots of useful mathematical functions. So for that please see the book. So some remarks. Now you can use commands without worrying about exactly how they do the work. So if I write square root of 17.35 or indeed whatever number it will get calculated somehow. You do not have to worry about it. The computer knows already how to calculate square roots. Similarly when I say forward 100 the computer already knows how to move that triangle forward 100. So you do not have to worry about exactly in which direction it is and so on. So that is the point of what happens when we say when we talk about a command. It promises that it will do something and you do not have to worry about how that something is actually done it will just happen. So here is a small exercise based on what we have seen so far. So draw a square of side length 100 as before but on top of this you are to draw a square obtained by joining the midpoints of the sides of the first square. So now you will have to calculate what the side lengths of this new square are. So you can use Pythagoras' theorem to determine the lengths of the sides of the inner square and then you will typically need to do a square root calculation but you know how to do square roots and so you can use that to decide how much the turtle is supposed to move. Here is another exercise. Instead of drawing a straight line draw a dash line say for example a dash of 10 pixels then a gap of 10 pixels and so on 10 times. So what this will require you to do is you will have to raise and lower the pen 10 times but do not and I emphasize do not write these commands 10 times put them inside repeat statement. So that is really a very important part of saying that I know programming. One of the major things about I know programming is that I know how to use repeat statements. So what have we discussed? So we discussed the repeat statement which is a really important statement which causes repeated executions of some statements which are in its body. C in and C out statements also we discussed and these can be used to read from the keyboard and to type messages to the screen. C++ has commands to compute mathematical functions as well as lift the pen up and down. So we will stop here for a short break.