6_conditionals.pdf - TA Mahalakshmi Srinivasan...

This preview shows 1 out of 2 pages.

TA: Mahalakshmi Srinivasan [email protected] HELP CONDITIONALS What is a conditional? A conditional statement is a block of code that only runs if a certain condition is met. For example, if you only want to multiply together all the numbers in a vector if they are all odd, you can write an if-statement that tells the computer to first evaluate a logical statement and if it’s true, the subsequent code block will run . This is the first time you get to control which of your code is actually run. In the past, all of your code ran from beginning to end. Now, we’ve introduced the concept of control flow so you get to decide which blocks of code run and which are skipped entirely. Pretty cool, huh? Conditional statements are useful when you want to separate out different cases. Maybe you only want to calculate how many nice dates you can afford IF you have enough money in your bank account to at least cover textbooks for that semester. Perhaps you have an encoder that treats uppercase letters one way and lowercase letters another. You might even have If-statements If-statements are used when you have a logical condition that is to be evaluated and the code block run only if it’s true. The general form of an if -statement is as follows: if <logical expression> <code block> end If the logical expression is evaluated to be true, the block of code will run. If the logical expression is evaluated to be false, MATLAB will ignore the block of code as if it never existed. What if you want MATLAB to do something in one case and something different for a second case and something completely different for a third case? You could use an “elseif” expression. It looks like this: if <logical expression> <code block 1> elseif <logical expression 2> <code block 2> elseif <logical expression 3> <code block 3> end
Image of page 1

Subscribe to view the full document.

Image of page 2
You've reached the end of this preview.
  • Fall '08
  • rwr
  • Switch statement, logical expression, code block, Mahalakshmi Srinivasan

{[ 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