Docsity

Programming - Unit 2: Basic elements of a program (slides), Diapositivas de Programación Informática

Vista previa parcial del texto

¡Descarga Programming - Unit 2: Basic elements of a program (slides) y más Diapositivas en PDF de Programación Informática solo en Docsity!

U N I T 2.

B A S I C E L E M E N T S

O F A P R O G R A M

P R O G R A M M I N G Y E A R 2 0 2 0 - 2 0 2 1

CONTENTS

1. Main components of a program

2. Variables

3. Data types

4. Expressions and instructions

5. Operators

6. Input/output

MATLAB LANGUAGE M

  • Matlab is an interpreted program
  • Scripts
    • A script is saved as a file with a .m extension (e.g. “myFirstScript.m”).
    • You can run a script either:
      • by typing its name at the command line or
      • by clicking on the Run icon of the Editor.

6

MATLAB programs

2. V A R I A B L E S

VARIABLES

  • In other programming languages ….
    • You need to declare the data type (double, logical)
      • In MatLab, data type automatically assigned when you assign a value
    • Variables that never change are constants
      • MatLab has no built-in support for constants

ASSIGNING A VALUE TO A VARIABLE

  • the value is stored in the memory cell allocated to the variable
    • a =7;
  • If there was a previous value, it is overwritten
  • To initialize is to assign an initial value to a variable
  • assignment operator =
    • A variable can be assigned an individual value (variable, literal) or the result of evaluating an expression - delta=0.001; - a=3; - x = y; - result=a+b;
    • In pseudocode the assignment is represented as 
      • The variable on the left is assigned the value on the right

CHOOSING GOOD NAMES FOR YOUR VARIABLES

  • Self-explanatory names
  • Lowercase (or camelCase)
  • Not too long

COMMENTS

  • programmer-readable explanation or annotation in the source code
    • meant for the programmer and not for the computer
    • Compiler / interpreter ignores comments
  • Code comments are essential to good programming style
    • Make code readable and understandable
      • other programmers may reuse our code and need to understand it
      • You may need to modify your code, comments will help
    • It good practice to introduce a comment at the beginning of each file

describing what it does

TYPES OF DATA TYPES

  • A type can be:
    • Basic
      • basic building block, single value
    • Structured or composite
      • Many values aggregated together
        • Arrays, Structs, Arrays of Structs (Units 3 and 5):
  • Primitive data types
    • Data types that are built in in the programming language
    • Can be simple or structured

15

NUMERICAL DATA TYPES IN MATLAB

  • Floating point numbers
    • double (double precision 64 bits)
      • By default, MATLAB stores all numeric values as double-precision floating point double
      • According to the IEEE® Standard 754 (64 bits)
      • The range for double is: -1.79769e+308 to -2.22507e-308 and 2.22507e-308 to 1.79769e+
      • Numbers larger than realmax or smaller than -realmax are assigned the values of positive and negative infinity, respectively:
    • single (single-precision floating point 32 bits)
      • The range for single is: -3.40282e+38 to -1.17549e-38 and 1.17549e-38 to 3.40282e+ 16

NUMERICAL DATA TYPES IN MATLAB

  • Integers (8, 16, 32, 64 bits)
    • Signed int8, int16, int32, int
    • Unsigned uint8, uint16, uint32, uint
  • Converting between data types
    • Functions with the same name as the data type: single( ), double( ), int8 ( )
    • Use function whos to see variables and associated data types

18

CHARACTERS AND STRINGS

  • Char
    • ‘a’ ‘hello World’
  • String
    • “hello World”
    • char and string are stored and managed differently by MatLab

4. E X P R E S S I O N S A N D

I N S T R U C T I O N S

EXPRESSION

  • Combination of data by means of one or several operators
    • Data can be literal values, variables, constants, and other expressions
  • Data in an expression are called operands

operand operand

operator

a + b

a == b a <= b a > 7 a + (b*c)

  • The composition of expression is guided by rules
    • An operator can only operate on operands of a given data type
  • Types of operators: arithmetic, relational, logical