Variable Assignment

To "assign" a variable means to symbolically associate a specific piece of information with a name. Any operations that are applied to this "name" (or variable) must hold true for any possible values. The assignment operator is the equals sign which SHOULD NEVER be used for equality, which is the double equals sign.

Variable Assignment

The '=' symbol is the assignment operator. Warning, while the assignment operator looks like the traditional mathematical equals sign, this is NOT the case. The equals operator is '=='

Design Pattern

          variable_name = expression;

          // Expression could be a number:  variable_name = 5;
          //         or a math expression:  variable_name = 10 + 5 / 3 - 7;
          //         or a funciton call  :  variable_name = sin( 5 );

To evaluate an assignment statement:

  1. Evaluate the "right side" of the expression (to the right of the equal sign).
  2. Once everything is figured out, place the computed value into the variables bucket.

More info

We've already seen many examples of assignment. Assignment means: "storing a value (of a particular type) under a variable name". Think of each assignment as copying the value of the righthand side of the expression into a "bucket" associated with the left hand side name!

Read this as, the variable called "name" is "assigned" the value computed by the expression to the right of the assignment operator ('=');

  jims_age = 51;   % first time we say that we have declared the variable
  jims_age = 21;   % second time.  we are changing the value that is associated with the variable

  daves_age = jims_age;   % evaluate jims_age, get 21, then assign 21 to daves_age

  jims_age = 19;  % what is daves_age?

Now that you have seen some variables being assigned, tell me what the following code means?


  lkjasdlfjlskdfjlksjdflkj = jlkajdsf + lkjsdflkjsdf;  % what does this mean?

The answer to above questions: the assignment means that lkjasdlfjlskdfjlksjdflkj is a variable (a really badly named one), but a variable none-the-less. jlkajdsf and lkjsdflkjsdf must also be variables. The sum of the two numbers held in jlkajdsf and lkjsdflkjsdf is stored in the variable lkjasdlfjlskdfjlksjdflkj.

Examples of builtin Data and Variables (and Constants)

        realmin  % the smallest possible positive number greater than 0
        pi  % the value of pi
        eps  % the smallest "GAP" between two numbers that the computer
             % can recognize (anything smaller and the computer
             % thinks the numbers are the same).
        inf  % Infinity (biggest number)
        NaN  % Not a Number (something wrong has happened;  0 divided by 0)

For more info, use the "help" command: (e.g., help realmin);

Examples of using Data and Variable

  wage_per_hour         = 25.40;   % dollars per hour
  wage_per_hour         = 12.00;   % dollars per hour changed (for rest of      program has new value)
  hours_worked_per_day  = 8.0;     % eight hour work day
  hours_worked_per_week = hours_worked_per_week * 5;  % five days per week
  weekly_wage           = hours_worked_per_week * wage_per_hour;
  yearly_wage           = ???;  % assume 52 work weeks in a year

  fprintf("I made %f dollars this year\n", yearly_wage);



The assignment pattern creates a new variable, if this is the first time we have seen the "name", or, updates the variable to a new value!

Read the following code in English as: First, compute the value of the thing to the right of the assignment operator (the =). then store the computed value under the given name, destroying anything that was there before.

Or more concisely: assign the variable "name" the value computed by "right_hand_expression"

  name = right_hand_expression;

  % this DEFINES or updates a variable.
  % The right_hand_expression could be many things:.
  % Another Variable .
  % A Math Expression.
  % A Function Call.

Back to Topics List