sheng/Assignment

From CDS 130

Jump to: navigation, search

Assignment

Contents

  1. Objectives
  2. Motivation
  3. Priming questions
  4. Notes
    1. Definition
    2. Assignment
    3. Assignment versus Equal
    4. Assignment versus Equal
    5. Whitespace around equal sign
    6. The end-of-line semicolon
    7. The end-of-line semicolon continued
    8. Assignment Example
    9. Assignment Example
    10. Basic Syntax Rules
    11. General Syntax Rules
    12. General Syntax Recommendations
    13. Syntax Rules Example
    14. The comment character
    15. The "clear" command
  5. Questions
    1. Syntax I
    2. Syntax II
    3. Valid Names

1. Objectives

  • To introduce the notion of assignment and how it corresponds to a bit pattern
  • To distinguish between assignment and equality
  • To introduce the clear command
  • To introduce the meaning of the semicolon at the end of a line
  • To introduce the comment character
  • To understand how to form valid variable names

2. Motivation

  • One of the most frequent operations in programming is assignment.

3. Priming questions

  • Your algebra instructor writes x = x + 1 on the board and asks you what you can conclude. What is your answer?

4. Notes

4.1. Definition

  • In programming, assignment is associating a value to a variable.
  • For example, in a program, you may assign the number 1.999999999999 to the variable named x. Later in the program, you may assign the number 3.14159 to the variable named x. At any point in the program, you may ask "what is the last value that was assigned to x?" or say "take the current value assigned to x and double it".

4.2. Assignment

In a spreadsheet we enter a number 1.0 into cell A1. We have assigned the value of 1.0 to A1.

In MATLAB the equal sign is used to indicate assignment. This command

A1 = 1.0

assigns the value of 1.0 to variable named A1. When you assign a decimal number to a variable, MATLAB writes the bit pattern associated with the decimal number into memory.


4.3. Assignment versus Equal

  • In MATLAB, the equal character is used for assignment.
  • In a MATLAB program the equal character does not mean equal in the sense that you are used to (the mathematical sense). That is, = \neq =
  • Some languages attempt to avoid this confusion by using a different symbol for assignment; for example, instead of A1 = 1.0, the expression A1 := 1.0 or A1 <- 1.0 is used.

4.4. Assignment versus Equal

The reason these languages use a different symbol for assignment is to avoid confusion. If you were thinking in the mathematical sense and saw this in a computer program

A1 = A1 + 1.0

you might re-write this equation as

A1 - A1 = 1.0

and then

0 = 1.0

which does not make sense.

4.5. Whitespace around equal sign

  • Whitespace is optional around the equal sign
A1=1
A1 = 1
A1      =1

all do the same thing.

  • Some programmers use the convention of connecting the equal sign to the variable, as in
A= 1

to emphasize that A is being assigned to the value of 1.

4.6. The end-of-line semicolon

  • If a line does not have a semi-colon at the end, MATLAB assumes that you want a value of a variable to be displayed.

If you enter

A1 = 20.0;

MATLAB assigns the value of 20.0 to a variable named A1.

If you had entered

A1 = 20.0

MATLAB will assign the value of 20.0 to a variable named A1 and then display the value of A1:

A1 = 20

4.7. The end-of-line semicolon continued

You can also display the value of a variable after it has been assigned. For example, you could enter

A1 = 20;
B1 = 20;
A1

and MATLAB will display

A1 =
    20

If you had ommited the semi-colon on the first line, you would see

A1 =
    20

A1 =
    20

4.8. Assignment Example

First work out on paper, then try in MATLAB:

  1. Assign a variable named A1 the value 20.0
  2. Assign a variable named B1 the value of A1 plus 13.0
  3. Re-assign the variable named B1 to the current value of B1 plus 12.0

What is B1 after the instructions are executed?

Could you do the above three instructions in Excel?

4.9. Assignment Example

Write a MATLAB program that does the following

  1. Assigns the decimal number 1.0 to a variable named A1
  2. Assigns the value of A1 plus 2.0 to a variable named A2
  3. Assigns the value of A2 plus 4.0 to a variable named A3
  4. Assigns the value of A3 plus 8.0 to a variable named A4
  5. Displays the value of A4

4.10. Basic Syntax Rules

Thus far, we have assigned values to variables named A1, B1, etc. following the way cells are named in a spreadsheet. MATLAB allows other variable names besides a capital letter followed by an integer. However, in the same way that the hospital won't let you name your baby "l33t", there are rules for variable names.

As a general rule:

  • Use only characters that are letters, numbers, or the underscore. For example, A1, Bob, bob, cat3, tag3, my_variable.
  • Don't start a variable name with a number. For example, 1A or 333BB.

4.11. General Syntax Rules

  • A variable may not start with a number (1A is not allowed).
  • Variable names should be shorter than 64 characters.
  • Variable names may not have a space (My Documents = 1 is not allowed).
  • Lower-case and upper-case variables are different (B1 is different from b1).
  • Variable names may not have a hyphen (A1-B1 is not allowed).
  • To determine if a variable name is valid, you can type isvarname VARNAME on the command line. If the result is 1, then you may useVARNAME as a variable name.

4.12. General Syntax Recommendations

  • Some variable names are not recommended: if, pi, for, end, matrix, array. To determine if a variable name is "not recommended", type which NAMEon the command line. If MATLAB says 'NAME' not found, then NAME is a safe name to use.

4.13. Syntax Rules Example

Which of the following are valid names? Which are not recommended?

  • 3BBBB
  • testing123
  • A1A
  • A1*B1
  • while
  • vector

4.14. The comment character

  • A comment character instructs the interpreter to ignore everything (the comments) that follows it.
  • Most programming languages have a comment character (some use multiple characters to represent a comment, e.g., //).
  • MATLAB's comment character is the percent sign: %.
  • Comments are added to make programs easier for a human to understand.

For example, the program

A1 = 20.0;
B1 = A1 + 13.0;
B1 = B1 + 12.0;
B1

could have been written as

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Start of program to demonstrate assignment
A1 = 20.0;      % Assign the value of 20 to a variable named A1
B1 = A1 + 13.0; % Assign the value of A1 plus 13.0 to a variable named B1
B1 = B1 + 12.0; % Assign the previous value of B1 plus 12.0 to the variable named B1
B1              % Display the value of B1
% End of program to demonstrate assignment
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

4.15. The "clear" command

When you start MATLAB, all of the variables that you have previously assigned values to no longer exist. Said another way, the values and the associated names are cleared from memory. Sometimes you will want to remove a variable (most often an array) from memory without exiting and restarting MATLAB. To see how the clear command works, note that if you enter

A1 = 10;
clear A1;
b = A1 + 1.0

you will see an error message. On the second line you have instructed MATLAB to remove the variable A1 from memory. On the last line, you have asked MATLAB to add 1.0 to the value of a variable that does not have a value associated with it. To clear all variables, type clear or clear all.

5. Questions

5.1. Syntax I

What will happen when the following commands are executed (take into account semi-colons)?

A = 1;
B = 2
C = A + B
A = 1;
B = A+1;
B = B*2
a = 2
b = 4
c = a + b

5.2. Syntax II

Each of the following has one error in it that will prevent the code from running if typed on the command line. What is the error?

clear;
a = b;
clear;
b = 1;
c = a+b;
clear;
A1 = 10;
1A = A1;
clear;
A = 1;
B = A+2;
C = B+2;
D = D+2;
clear;
A = 1;
B = 2;
A+B = 3;

5.3. Valid Names

Which of the following are valid names? Which are not recommended?

  • plot
  • matrix
  • _twelve
  • seven
  • My Variable
  • MyVariable
  • A1^3
  • case
Personal tools