lecture2a - Introduction to Computer Programming with...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Computer Programming with MATLAB CEE/MAE M20 Lecture 2a Logical expressions and conditionals CEE/MAE M20 At the heart of almost any computational task are logic and conditionals 2 Is logic expression true? Input No Yes Do something else Do something CEE/MAE M20 For such purposes, every programming language has a form of if-else 3 if (expression is true) do something else do something else end Is logic expression true? Input No Yes Do something else Do something Pseudocode CEE/MAE M20 For such purposes, every programming language has a form of if-else 3 if (expression is true) do something else do something else end Is logic expression true? Input No Yes Do something else Do something So we need to discuss logical expressions conditional statements Pseudocode CEE/MAE M20 Logical expressions 4 CEE/MAE M20 Logical expressions The logical data type is a special type of data that can take only two values: true or false . 4
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CEE/MAE M20 Logical expressions The logical data type is a special type of data that can take only two values: true or false . In MATLAB, for example: 4 >> a = true a = 1 >> Matlab stores this as a 1 (or 0 for false) CEE/MAE M20 How much memory does a logical variable need? In MATLAB, we can find this out with the whos command: 5 >> whos Name Size Bytes Class Attributes a 1x1 1 logical >> CEE/MAE M20 How much memory does a logical variable need? In MATLAB, we can find this out with the whos command: 5 >> whos Name Size Bytes Class Attributes a 1x1 1 logical >> In MATLAB, all variables are stored as arrays. A single scalar is just a 1 x 1 array. CEE/MAE M20 How much memory does a logical variable need? In MATLAB, we can find this out with the whos command: 5 >> whos Name Size Bytes Class Attributes a 1x1 1 logical >> 1 byte of memory (8 bits) In MATLAB, all variables are stored as arrays. A single scalar is just a 1 x 1 array. CEE/MAE M20 6 CEE/MAE M20 In contrast, MATLAB stores all numbers (integers or floating point) as double-precision floating 6
Image of page 2
CEE/MAE M20 In contrast, MATLAB stores all numbers (integers or floating point) as double-precision floating For example, 6 >> b = 1; >> whos Name Size Bytes Class Attributes a 1x1 1 logical b 1x1 8 double >> CEE/MAE M20 In contrast, MATLAB stores all numbers (integers or floating point) as double-precision floating For example, 6 >> b = 1; >> whos Name Size Bytes Class Attributes a 1x1 1 logical b 1x1 8 double >> We set this to 1 instead of ‘true’. This creates a real number, not a logical. CEE/MAE M20 In contrast, MATLAB stores all numbers (integers or floating point) as double-precision floating For example, 6 >> b = 1; >> whos Name Size Bytes Class Attributes a 1x1 1 logical b 1x1 8 double >> 8 bytes of memory (64 bits) We set this to 1 instead of ‘true’. This creates a real number, not a logical.
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern