Muole03 Syntax and Semantics
Muole03 Syntax and Semantics
Commands
Learning Objectives:
• Enter MATLAB commands.
• Create, access, and manipulate variables.
• Create numeric and character variables.
Basic Syntax and Semantics
• Syntax
• refers to the structure of a program written in a programming language.
• Semantics
• describes the relationship between the sense of the program and the
computational model.
Note:
Syntactic errors are handled at the compile time. As against, semantic
errors are difficult to find and encounters at the runtime.
• In C++, Java, C, a variable number is declared as int
number.
• To initialize, we must use an integer value. int number
= 7;
• If we will initialize with “seven” i.e.; int number =
seven;
Explanation:
This declaration and initialization is syntactically
correct but semantically incorrect because “seven”
does not represent integer form.
In MATLAB, you will just assign a value to a given
variable to determine its data type.
Examples
on MatLab
Input
Data Types
Data Type Stores Memory Required
Fundamental Data Types
short an integer 2 bytes
range: -32,768 to 32, 768
int an integer 4 bytes
range: -2, 147, 483, 648 to 2, 147, 483, 647
float a real number with 7 digits of precision 4 bytes
range: -3.4 x 1038 to 3.4 x 1038
double a real number with 15 digits of precision 8 bytes
range: -1.7 x 10308 to 1.7 x 10308
bool A Boolean value (either True or False) 1 byte
char One character 1 byte
User-Defined Data Type
String zero or more characters 1 byte per character
Identifiers and Reserved Words
Identifiers Identifiers:
- price
• is the name of a programmer-defined entity.
- discount_rate
• In Java, any identifier must begin with a letter, - score1
which may be followed by any number of letters - Number
and/or digits. - x
- b
Reserved Words Reserved words:
• are identifiers that cannot be used in any way - auto
- break
other than the way they were intended to be
- case
used by the Java designers.
- char
- const
Available at: - static
https://www.mathworks.com/help/rtw/ug/reserv - if
ed-keywords.html - void
- double
Variables
• is a memory location whose value can change (vary) during
runtime, which is when a program is running.
Named Constant
• is a memory location whose value cannot be changed during
runtime.
Examples:
pi = 3.141593
inch_to_cm = 2.54
kilo_to_pound = 2.2
Variables
How to Name a Memory Location for a Variable and Name
Constant:
• The name must begin with a letter or an underscore.
• The name can contain only letters, numbers, and the underscore
character. No punctuation marks, spaces, or other special
characters are allowed in the name.
• The name cannot be a keyword or a programming language’s
reserved word.
• Names are case sensitive. (Letter a and A are different)
Examples:
Valid Names
grossPay, interest, TAX_RATE, PI,Sales_2015
Invalid
Names
Examples Reason
2011Sales The name must begin with a letter
end Balance The name cannot contain a space
first.name The name cannot contain a punctuation
int The name cannot be a keyword
RATE% The name cannot contain a special
character
Operators
• is a symbol that tells the compiler to
perform specific mathematical or logical
manipulations. There are three classes of
operators: arithmetic, logical and
relational.
A. Arithmetic Operators
Operator Action Examples Answers
+ Addition 7+5 12
- Subtraction 10-3 7
* Multiplication 5*7 35
/ Division 100/20 5
https://www.mathworks.com/help/matlab/matlab_prog/matlab-operators-and-special-characters.html
B. Relational and Logical Operators
Relational Operator
- refers to the relationship values can have with one
another.
Logical Operator
- refers to the ways these relationships can be connected
together using the rules of formal logic.
- Decisions – true or false
Relational Operators Action
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
== Equal
~= Not equal
Examples:
1. 23>5 =
2. 78<7 =
3. 4==40 =
4. 6>=3 =
5. 10<=5 =
6. 9~=5 =
7. 48<=78 =
Relational Operators Relational Operators
>
Action
Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
== Equal
!= Not equal
Examples:
1. 23>5 = TRUE
2. 78<7 = FALSE
3. 4==40 = FALSE
4. 6>=3 = TRUE
5. 10<=5 = FALSE
6. 9~=5 =TRUE
7. 48<=78 = TRUE
SEATWORK:
Suppose: num1=5, num2=7, sum=5;
1. sum+num1 >= num1+num2
2. num1 == sum
3. num2 < sum
4. num2 ~= sum
5. num2/sum == 1+sum/num1
SEATWORK:
Suppose: int num1=5, num2=7, sum=5;
1. sum+num1 >= num1+num2 || num1 == sum
2. num1 == sum && num2 < sum || num2 ~= sum
3. num2 < sum || 1==sum-num1
4. num2 != sum && num1*num2==num2*sum
5. num2/sum == 1+sum*num1 || sum>=num1
Escape
Sequence
The conversion character specifies the notation of the output. It consists
Conversion Character of a single character and appears last in the format specifier.