# lecture2a - Introduction to Computer Programming with...

• Test Prep
• 11
• 100% (1) 1 out of 1 people found this document helpful

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

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

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

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 ﬂoating point) as double-precision ﬂoating 6
CEE/MAE M20 In contrast, MATLAB stores all numbers (integers or ﬂoating point) as double-precision ﬂoating 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 ﬂoating point) as double-precision ﬂoating 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 ﬂoating point) as double-precision ﬂoating 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.

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

This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern