 Hello and welcome back. In this lecture, we are going to look at sequential execution of statements in C++ programs. Here is a quick recap of some relevant topics we have already studied earlier. We have seen the structure of a simple C++ program. Within it, we have seen how variables are declared and how their types are declared. We have seen the usage of assignment statements in C++ programs and the usage of arithmetic and logical expressions in assignment statements. In this lecture, we are going to look at sequential execution of statements in simple C++ programs and we are also going to have an initial glimpse into how to solve some interesting problems using C++ programming. Now, before we get going, let us recall some useful facts that we have already learned. We know that a program is basically a sequence of compiler directives, declarations and instructions. The compiler directives are instructions to the compiler that it uses when it tries to compile your C++ program to the machine's language, to the computer's internal language. Declarations tell the compiler what variables are to be used. Declarations are mostly about variables in the programs that we are going to see. A declaration tells how much memory to allocate for storing the value of a variable. For example, if a variable is declared as an integer, then 4 bytes of memory are to be allocated for it. And a declaration also tells how to interpret the bits stored in those memory locations allocated for a variable. For example, whether it is a signed integer or an unsigned integer or a floating point number, the compiler gets to know this information from the declarations. Instructions basically tell the computer or Mr. Dumbo, if you recall the Dumbo model of computing. Instructions tell the computer what it should do. And by sequencing instructions in a careful and intelligent manner, we can get a computer or Mr. Dumbo to do highly non-trivial tasks in an efficient way. And this is all what the art of programming is about to make instructions execute in the right sequence so that we can achieve non-trivial tasks in a simple and efficient way. Now, the simplest programs that we can write are basically linear sequence of instructions. And the computer executes these instructions in the same linear order in which they appear in the program. For example, on the left pane here, you can see our friendly summing program which reads two integers, adds them up and prints the sum out on your screen. And on the right hand side, here I have shown the computer screen as it might appear as the instructions in this program are executed sequentially. So, this is the main function. The operating system calls this main function, passes control to this main function. And when this main function executes, the first statement here is really a declaration. So, there is nothing for the computer to execute as far as this declaration is concerned. This is really a directive to the compiler to reserve appropriate amounts of storage in the memory and to interpret the bits stored in those locations in the appropriate way. This declaration is saying that there are going to be three variables, all of them of type integer. The next statement is really the first statement that the computer executes and I have sort of shown that here by a running icon. So, when the computer executes this statement, this is what you see on your screen. This is the prompt. It could be your favorite prompt. And then the computer prints after that give two numbers. The computer, after executing the statement, is going to execute the instruction that appears exactly after this in the sequence in which the instructions in the program are written. So, the next instruction the computer will execute is the reading of the values of a and b. Now, when the computer executes this statement, it is waiting for the user to input the values of a and b from the keyboard. So, perhaps the user enters 23 and then presses the enter key, but that gives the computer only the value of a. Whereas, this instruction was asking the computer to read both the values of a and b. So, the computer is still waiting and then the user enters minus 300 and enter. And now, the computer has read the values of both a and b. So, it is done executing this instruction. And it is now going to go and execute exactly the next instruction that appears in sequence after it. In this case, it is C assigned a plus b. When the computer executes this instruction, it does this addition internally. Nothing shows up on the screen, but after executing this instruction, the computer goes to this instruction. And as it executes it on the screen, we get this message sum is space followed by the value of C. In this case, it is minus 277. And then the computer executes return 0, which basically passes control back to the operating system. Recall that the operating system had called the function main. And when the control passes back to the operating system, we get back the prompt on our screen. So, this is exactly how a computer executes instructions in sequence as they are specified in the program. Note also that the semicolon is basically used to separate one executable statement from the next. So, you could put two executable statements on the same line. You do not need to break the line, but as long as you put a semicolon between them, it is clear that the execution to the left of the, the statement to the left of the semicolon will be executed first and then the statement to the right of the semicolon. Now, here is another example of sequential execution of statements in a program. This is our program, which we have seen earlier that takes three numbers and tells us whether the first number read is the largest of the three. So, here once again, the first statement here is a declaration. There is nothing for the computer to do. This basically tells the compiler how much storage to allocate for these variables. The second statement is also a declaration. This is saying is a largest is a Boolean variable. So, once again, nothing for the computer to execute. This is just telling the compiler how much memory to allocate for this variable in memory. The next statement is the real statement that the computer executes shown by this running icon and when it executes this statement, this is what we get on the screen. This is the end line, which means the cursor returns to the beginning of the next line. After that, the computer executes this statement where it is trying to read the values of a, b and c. Suppose, the user enters 23 and presses enter that gives the computer only the value of a. It still needs to read the values of b and c. So, it waits and the user can then enter minus 300 space 20 and then enter. This space basically tells the computer that minus 300 is to be read as the value of b and 20 is to be read as the value of c and once it has read all of these three integers, then the computer goes to execute the next instruction and this if you recall from an earlier lecture is nothing but a logical expression. So, the computer evaluates this logical expression for the given values of a, b and c. You can check for yourself that this logical expression evaluates to true. So, this Boolean variable is a largest gets set to the value true, but when the computer executes this statement, this assignment statement, there is really nothing that you can see on the screen. This is executed internally inside the computer. After executing this statement, it executes the next output statement and when the computer executes that, we get an output on the screen is a largest colon space and then the value of is a largest. For the given values of a, b and c, a is indeed the largest. So, this Boolean variable gets set to true and we have seen that when you are trying to output the value for Boolean variable that is true, you are going to get one and after this statement is executed, the computer executes return 0, control passes back to the operating system and you get your prompt back on your computer screen. So, what we need to remember about sequential execution of programs is that all the variables must be declared before we use them. The executable statements are going to be executed by the computer exactly in the same sequence from top to bottom as they appear in your program and if a statement changes the value of a variable and a later statement or a statement below it refers to that same variable, then because the statement that came earlier that appeared above in the top to bottom ordering, because that statement is already changed the value of the variable. So, the later statement is going to see the changed value when it refers to that variable and whenever you execute a return statement, control passes back to whoever called the function. We will see later that one function can call another function. In our case it was the operating system that called the main function. So, whenever we execute a return statement in our main function control returns back to the operating system and whatever statements you might have after the return statement are not going to be executed. So, this is important to remember that we are going to sequentially execute instructions until we encounter return on executing return control passes back to the caller and the subsequent statements are not going to be executed. Subsequent statements in the function after the return statement are not going to be executed. Now, having seen how statements execute sequentially in a given program, let us quickly try to see how we might use programming to solve some simple problems. Suppose, I give you this problem given two integers a and b, find out if b divides a. The first thing to do is to forget about computers, forget about programming and just think of the sequence of steps that you would take if you are solving this problem by pencil and paper. So, here is perhaps one way of solving this problem. You would find the remainder of a on division by b and if the remainder is 0, then you would say yes b divides a, otherwise you will say b does not divide a. Now, having come up with this recipe for solving this problem, we can then think in terms of program statements that we have seen so far and see can we sequence the program statements using the constructs that we have studied so far in a linear order such that we basically get the same effect as executing these two steps in our pencil and paper style of solving this problem. Now, one thing that you note here is that here I said if remainder is 0, then do something, otherwise do something. So, this is really conditional execution of statements and we did not learn how to do this so far in our programs. So, is it the case that we cannot solve this problem? Well, this is also an opportunity to think a little bit and find out is there a way to solve our problem without using conditional execution of statements just by a linear sequence of instructions. And in the case of this problem, it turns out that we can solve this problem by a linear sequence of instructions. So, we could do the following, we could calculate the remainder of a divided by b in C++, this can be used, this can be achieved by using the percent operator or the remainder operator. Then we can set a Boolean flag to true if r equals 0 and to false otherwise, this can be achieved in C++ through this assignment statement. Note that the right hand side of the assignment is a logical expression because I am using a comparison operator here. And finally, I could output the flag and clearly in this case the flag is true if and only if b divides a. So, now we could write a simple C++ program to solve the same problem. We start off by compiler directives, these we have already seen in an earlier lecture what these mean, I am not going to repeat them here. Then we write a comment, it is always a good idea to say in a comment what your program is doing. Here is our main function, here are the two braces between which the block of statements for this main function is going to appear. We first declare three integer variables and a Boolean variable, then we print out a message saying give the values of a and b followed by end of line. Then we read the values of a and b, we calculate the remainder of a divided by b, set the flag to true if r equals 0, if the remainder equals 0, otherwise this flag gets set to false and then we say does b divide a and we output the value of the flag. So, if b indeed divides a, we will see on our screen does b divide a question mark space 1 because the Boolean value true will be output as 1 and if b does not divide a, we will get does b divide a question mark space 0 and finally, we return 0. An important thing to note here is the sequence in which these two instructions appear r is a percent b and divides flag is r equal to equal to 0. So, it is an interesting question to ask what would happen if we swap the order of these two instructions and it is not hard to see that if divides flag assigned r equal to equal to 0 appears before r is assigned a percent b, then the value of r that is going to be used when I am executing this statement would be the garbage value in the memory locations for the variable r and that is not what we want to do. So, therefore, the sequencing of these instructions is very important. So, in summary what we saw in this lecture is sequential execution of statements in C plus plus programs and we also got an initial glimpse into how we could solve problems by programming. Of course, without conditional execution of statements, we are handicapped and we are going to look at conditional execution of statements in C plus plus in the next lecture. Nevertheless, some interesting problems can still be solved by just sequentially executing the instructions in a program. Thank you.