[MUSIC] We've seen that MATLAB's
pretty picky about its rules. Every programming language is. The reason is that strict rules
make it easier to write accurate, unambiguous programs. We’ve violated MATLAB's
rule several times and we've seen these red error
messages that they cause. There's a computer science term for this type of rule, the type of rule
we've tripped over, it's called syntax. Syntax is a language’s set of rules for
the form of the statements, including, for example, the rule that says
you can't use a hash symbol in a variable name. Here's another example, one equals x. Of course, MATLAB doesn't like that. As you
can see it says that the expression to the left of the equals sign is not
a valid target for an assignment. The simple form of this syntax rule, or syntactic rule, that we're violating
is this: In an assignment statement there must be a variable name
on the left of the equal sign. We knew that, and
we know that one is not a variable name. We'll run into lots more syntax
areas as we learn more rules. But there's another type of area that
is much worse, the semantics error. As we said, the syntax of a
command is its form. The semantics of a command is its meaning. An example of semantics is this: Suppose we want to swap
the values that are in x and y. First note that the current value of x
is 42 and the current value of y is 87. I've seen many new programmers
try to do it like this, and by the way, I'm certain I did
it too when I was starting out. So they type x equals y, and y equals x. You see what went wrong here? Let's look up here and see that something
did go wrong. They're both equal to 87. Instead of having their value swapped, they both now have the value that
y had before, so what happened? Well, the first statement, this one here, copied the 87 that was in y into x, and
at this point they both have the value 87. So, x's value has just been lost. The 42 is gone. Then this next statement just
copies whatever is in x, which happens to be 87 now, back into y. So, y is 87; it was 87;
that really didn't do anything. Well let's do it right. First let's set things up
the way they were to begin with: x was equal to 42 and y was equal to 87. And we can see that we've done
that over here in the Workspace. Now here's how you swap them: temp equals x. We've copied x into temp.
temp stands for “temporary variable”. We've actually saved it there, so that when we
copy y into x we haven't lost x's value. Now we simply copy temp into y.
And there we go. So, when we made the error,
MATLAB didn't catch it and it didn't print an error message, And there wasn't any red color. That's
because our syntax is perfect. The problem is that our code didn't
mean what we thought it did. This is a semantic error,
also known as an error of semantics. The semantics of our first try was that y's value was simply
copied into x. That's all. It was a semantic error because we thought that x's value would
also be copied into y. The semantic error is an error in
which the meaning of our code is not what we intended. And the reason that it's
worse than a syntactic error is that MATLAB has no way of
knowing that it's an error. Neither would the other
languages we talked about. We could have made this particular error
in any one of thousands of applications because swapping variables is
a generic problem that occurs a lot. But an even more common type of semantic
error is one that is application specific. Look for example, at the formula we
used for calculating absolute zero in degrees Fahrenheit. Right up here. It's correct. But suppose we’d typed this. [MUSIC] Everything’s the same, except that
we used a minus instead of a plus. [MUSIC] [APPLAUSE]