Absolute Beginners C++



 

Lesson 5 - Assignment Statement

5.1 Assignment statement 

// ex5-1.cpp - assignment statement
#include <iostream>
using namespace std;
int main()
{
    cout << "Enter two numbers : ";
    double num1, num2;
    cin >> num1;
    cin >> num2;
    double bigger;
    if (num1 > num2)
        bigger = num1;
    else
        bigger = num2;
    cout << "Bigger is " << bigger << endl;
    return(0);
}
ex5-1
Enter two numbers : 35 46
Bigger is 46
Example ex5-1 introduces a new statement which allows us to directly change the contents of a variable.  It is called the 'assignment' statement and it has the syntax: 
variable = expression;
where 'variable' means the name of an existing variable, and 'expression' means some kind of value made up from numbers, variables or strings, combined with arithmetic operations such as add, multiply, etc.  In general the type of the expression has to match the type of the variable: if the variable is an integer, the expression needs to be an integer; if the variable is a double, the expression needs to be of double type (i.e. a fractional number); if the variable is a string, then the expression must evaluate to a sequence of characters.  In the example, the variable 'bigger' is assigned one of two values depending on the test performed on the input numbers num1 and num2.  If num1 is bigger than num2, then the variable bigger is set to num1, otherwise it is set to num2.  This logic ensures that the variable 'bigger' contains a value equal to the larger of the two input numbers. 

5.2 Assignment statement changes variable

// ex5-2.cpp - assignment statement changes variable
#include <iostream>
using namespace std;
int main()
{
    cout << "Enter a number : ";
    double num;
    cin >> num;
    double numcopy = num;

    cout << "Initial values:" << endl;
    cout << "num=" << num;
    cout << " numcopy=" << numcopy << endl;

    num = num+1;

    cout << "After assignment:" << endl;
    cout << "num=" << num;
    cout << " numcopy=" << numcopy << endl;
    return(0);
}
ex5-2
Enter a number : 10
Initial values:
num=10 numcopy=10
After assignment:
num=11 numcopy=10
Example ex5-2 demonstrates an important characteristic of the assignment statement: when the assignment statement is used to assign a value to a variable, the old contents of that variable are lost.  In this example, a number is input into the variable 'num', and a copy of that value is stored in the variable 'numcopy'.  The statement 'double numcopy = num' is a kind of joint declaration and assignment statement: it simultaneously declares a new variable 'numcopy' and initialises it to the same value as 'num'.  The program then outputs the values of num and numcopy to show they are the same.  The statement 'num = num + 1' looks mysterious to many people.  It is not a test on the value of num (this would actually look like 'num==num+1' and would always be false), but an assignment statement which says evaluate the value of num+1 and assign that value to num.  The consequence is that the value of num is increased by one.  This is shown by the second set of output statements.

5.3 Exercises

a. Write a program (assign.cpp) that given a number x, calculates x to the power 2, x to the power 3, and x to the power 4, using no more than three multiplications, as in: 
assign
Enter value x : 3
x to power 2 = 9
x to power 3 = 27
x to power 4 = 81
b. Write a program (stars.cpp) that prints 5 rows of asterisks, with each row containing twice as many asterisks as the previous row, but with your program only containing a single asterisk, as in: 
stars
*
**
****
********
****************
Hint: for strings, the '+' operator means concatenate; so "Hello "+"there" => "Hello there".

c. Adapt exercise 4.5b (small2.cpp) to use assignment rather than multiple print statements. 
 


© 1999 Mark Huckvale University College London