EXERCISE+3

**Exercise 3** **Sequential Structures: //Assignment Statements//**

Think of variables as if they were boxes. There are only a certain amount of boxes available to the computer -- the amount is determined by the amount of memory (RAM) the computer possesses. A declaration statement tells the computer that first there is a box; it has a name; we might put things in it; and the variable type is the sort of thing we're going to put in it if we decide to do so.

If we wish to put something in the box we have to use an assignment statement.

The statement **x := 3 + 8** is an example of an assignment statement.

It is called that because it assigns **values** to **variables (or constants).**. The way to think of an assignment statement is ** "x becomes ___"__ ** __or **"x is assigned the value of**__ ".

Assignment statements have a definite form which they must follow

**identifier :=** **value**

Constants are like variables except that their values cannot be changed. They are used to assign a value to a space in RAM which will **never (never, never!)** change during the execution of the programme. i.e., **const PI := 3.1459**. (PI becomes 3.1459)

Note that ** constants are always declared and assigned in the same statement. ** It's like telling the computer that "you can shut the box because we're never, ever, ever going to put anything else in it". This saves the computer time and let's your program run faster.

**Task:** Complete the following exercises. Identify which assignment statements are correct (OK). Correct those which are incorrect. Assume that all variables have been declared correctly.


 * **Assignment Statement** || **Correction** ||
 * money:= $45.00 ||  ||
 * r * 6:= 2 * x + 8 ||  ||
 * <span style="font-family: arial,helvetica,sans-serif; font-size: 120%;">4/3 +5*t :=pay ||  ||
 * <span style="font-family: arial,helvetica,sans-serif; font-size: 120%;">put:= 4 + 6 * 3/2 ||  ||
 * <span style="font-family: arial,helvetica,sans-serif; font-size: 120%;">gross pay := rate * hours ||  ||
 * <span style="font-family: arial,helvetica,sans-serif; font-size: 120%;">net_pay := grosspay - deductions ||  ||
 * <span style="font-family: arial,helvetica,sans-serif; font-size: 120%;">variable := VARIABLE * 2 ||  ||
 * <span style="font-family: arial,helvetica,sans-serif; font-size: 120%;">7th_number := 12 ||  ||
 * <span style="font-family: arial,helvetica,sans-serif; font-size: 120%;">number6 := number +12 ||  ||
 * <span style="font-family: arial,helvetica,sans-serif; font-size: 120%;">counter := counter +1 ||  ||
 * <span style="font-family: arial,helvetica,sans-serif; font-size: 120%;">pi * r * *2=area ||  ||
 * <span style="font-family: arial,helvetica,sans-serif; font-size: 120%;">area_circle := pi (radius **2)** ||  ||
 * <span style="font-family: arial,helvetica,sans-serif; font-size: 120%;">student% := total_marks/4 ||  ||
 * <span style="font-family: arial,helvetica,sans-serif; font-size: 120%;">%area_rectangle := length * width ||  ||

<span style="font-family: Arial,Helvetica,sans-serif; font-size: 120%;">**Remember**: Only **__one__** variable may be __**on the left**__ side of an assignment statement. You are assigning <span style="font-family: Arial,Helvetica,sans-serif; font-size: 120%;">the result of the calculation to that variable's memory location.

<span style="font-family: Arial,Helvetica,sans-serif; font-size: 120%;">The percent sign % tells the computer to ignore everything to the right of it -- this is used for internal documentation (instructions you want to leave for people who have to read your program... like me)**