Up until now, we have seed how to declare, initialize and assign a value to a variable Now, we will focus on how to print the value of a vaiable and how to read it from the keyboard. Let us begin with the printing : if we glance at this simple example program, a printing line is, for example, such a line. Let us examine it : This line begins with the reserved keyword "cout"; this is a variable representing, in the program, the standard output device, generally, the terminal. Then, we can see several printing sections. We must systematically regroup them like this, with the " << " sign (double less-than sign). meaning that we wish to print something. From a mnemonic point of view, this represents the flow through which the information circulates. The information will go from what follows to "cout". This should make it easier to remember the direction of these signs. So, behind this sign, meaning "print", we can have different possible printing values. Here is a "literal" value, in these double quotes. This means that the sentence will be printed exactly as it is. Then, we have a variable name, n, written here, representing the value of the variable, stored in the memory under the name n. Therefore, we will print the value contained in the variable n. Be careful not to mistake this with "n". (double quote, n, double quote). which means "print the letter 'n', the word 'n'". Then, we have the period written as it is, as a character string. Finally, we have the reserved keyword "endl", meaning "end of line". This permits to jump to the next line, to generate a line break. The printing of the variable n is actually a specific case of a more complex expression. Actually we can use any C++ expression in "cout". For example, we can print 2*n (2 times n). The following line is thus read : on cout, print the message "le double de", print the value of n, print the message "est" (= "is"), with here, two blank spaces. Then, print the expression 2*n (two times n), print a period and, finally, a line break. (TN: "le double de" means "twice as many as") "cout" and "endl" are actually called std::cout (S-T-D, colon, colon, "cout") and std:.endl (S-T-D, colon, colon, "endl"). We can write them simply "cout" and "endl" because, at the beginning of the program, we added the instruction: "using namespace std;" thanks to think, we do not have to write "std::" before the reserved keywords belonging to the so-called "standard library namespace", the namespace std. Without the line "using namespace std;", we would need to write std::cout and std::endl. We wanted you to know that because, in code from the outside, you could encounter this syntax. Now, to illustrate all this, let us examine step by step, the course of our example program. First of all, we declare an int-type variable n and initialize it to the value 4. This will create in the memory, an area where we will store an integer n, inside which we put the value 4. Then, we will declare a int-type variable n_carre (means "n_square"). Since it is not initialized, we do not know a priori what value it initially contains. Then, we execute the instruction n*n (n times n) goes into n_carre, that is, n_carre = n*n. By the way, remember that writing the "equal to" sign here means that we evaluate the expression on the right and copy it inside the variable, here, on the left. Therefore, the variable n_carre, will receive the value 4*4, that is, 16. Now, let us move on to the printing instructions. First of all, we will print here : "la variable n contient" (TN: "la variable n contient" means "the variable n contains") then, we will print the expression of the variable n, namely its value, 4; then, we will print a period and finally, here, we will generate a line break thanks to this "endl". Now, we move on to the next printing, which will print : "le carré de", n, which is an expression, corresponding to the value 4 stored in the memory (TN: "le carré de" means "the square of"), "est", n_carre, which is another expression, the value of which in the memory is 16 (TN: "est" means "is"), and finally a line break. Now we can print the last message. "le double de n est" (TN: which means "twice as much as n is") By the way, here n is a letter included in the message, therefore, it will be printed as such. It is the word, the letter "n" which will be printed here and not an expression as it happened on the previous line, or as we could have here. So, "le double de n est", then the printing instruction 2*n. Thus, we will search for the value of n, 4, 2*4, which, of course, will be evaluated to 8. Therefore, 8 wil be printed here. Then, we print the period as a character and finally we generate a line break. A question now. Please look at the following code and tell us what it will print. Here is the step by step answer. First of all, we declare an int-type variable a initialized to the value 2. Then, we declare an int-type variable b, intialized to the value 1. We then have the assignment here, starting by evaluating the expression a*(b+2). That is, two times the value of b (which is 1) plus 2. That is, 2 times 3. This expression is copied inside b, which is now 6. Now, we print the value of a, "comma", the value of b. Another question : What will this program print ? Here is, again, the step by step answer. First of all, we declare a variable a initialized to the value 1. Then, we declare an int-type variable b initialized to the value 2. Then, we copy the value of b, inside a. Therefore, a will be 2. Then copy the value of a, which has just been modified, inside b. Therefore, b will still be 2. Finally, thus "2, 2" wil be printed. A question we could ask is how to swap the value of two variables ? Let us suppose that we have a variable a, containing 1, initially and a variable b, containing 2, initially. How do we proceed so that, after the swap, a will contain 2, and b will contain 1 ? Let us suppose that we have to deal with water glasses. Let us suppose that we have a glass a filled with grenadine and glass b filled with mint. How would you proceed in order to transfer the mint into the glass a and the grenadine into the glass b ? The solution is, of course, to resort to a third glass, which we will call "tmp". It is not any more complicated for variables. One simpy needs to introduce an intermediate variable. Let us do this. First of all, we will create an intermediate variable "temp" in which we will copy the value of a. So, we will initialize temp to the value of a. Here, temp will thus be 1. Then, we will copy b into a. Finally, we will copy the value of temp into b. Now, let us move on to reading of the value of variables from the keyboard. Let us go back to our example program and suppose that we do not only wish to calculate the square of 4, but wish to be able to enter the value of n through the keyboard, by asking it to the user. How would we do that ? Why, we will simply add here, in the program, something letting the user enter this value. First of all, we will start by printing a message : "Entrez une valeur pour n : " (TN: which means "enter a value for n:"). Then, thanks to this instruction "cin redirected towards n", we will be able to read the value of n from the keyboard. Thus, the reading from the keyboard requires the instruction "cin". "cin" representes the standard input device, "in" as in "input"; this represents the reading from the keyboard. After the "cin", we see a sign similar to the one used for printing; the direction is reversed, though! Here, the information goes from the keyboard to the variable n; this a good way to remember the direction of this symbol. Finally comes the name of the variable inside which we will store the value read from the keyboard. "cin" thus represents the keyboard inside the memory, the program, of the computer. The symbol here represents the fact that we are going from "cin" to the memory, then, the reading direction and, then the name of the variable. By the way, it is important to understand that what follows the reading direction here, is indeed the name of a variable. It cannot be a message or anything else. This is rigorously forbidden. If you want to print something, you have to use the "cout" instruction. You will begin by printing a message, then a line break -it is optional- and then, with a second instruction, we will read from the keyboard into the variable n. The message will indeed be printed on the terminal "cout" and not on the keyboard! Meanwhile, the reading instruction is indeed here, and contains the "cin" here. By the way, it is possible to read several variables successively, one behind the others. We simply need to add the reading sign every time. Here, it means : "from cin, read n1, then read n2, then read n3." Personally, we advise you to write it in several clearly separated lines. Thus writing : reading from "cin" into n1. Then : reading from "cin" into n2. Then : reading from "cin" into n3. Mayhaps it is a little more tedious, but we do believe that it is clearer at the beginning. Now, let us examine our example step by step. We initially declared the variable n. Then we print the question : "enter a value for n : ". This will result in the printing of this message. Then, we will read from the keyboard. Let us suppose that the user types the value 2. Thanks to this "cin into n" instruction, the value 2, typed on the keyboard will be stored inside the variable n, here. Once this instruction has been executed, the variable n will contain the value read from the keyboard. From this point, the program will perform exactly as before. We declare the variable n_carre. Then, we calculate n squared, n times n, and assign it to the variable n_carre. Then, we will proceed to the printings, starting with : "the variable n contains",then printing the value of n, which is 2, in our example. And the program will resume exactly as before.