14-440-127 Lecture 2 Notes

14-440-127 Lecture 2 Notes - 14:440:127 Introduction to...

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

View Full Document Right Arrow Icon
14:440:127– Introduction to Computers for Engineers Notes for Lecture 2 Rutgers University, Fall 2008 Instructor- Blase E. Ur 0.1 Conditional Statements 0.1.1 Recap of Relational Operators > is greater than < is less than >= is greater than or equal to <= is less than or equal to == (2 equals signs!) is equal to ~= is NOT equal to 0.1.2 Recap of Logical Operators means AND--- both true T T = T. T F = F. F T = F. F F = F. | means OR--- either true T | T = T. T | F = T. F | T = T. F | F = F. ~ means NOT--- switches True and False ~ T = F. ~ F = T. 0.2 Conditional Statements A very important part of computer programming is the ability to make decisions based on whether things are True or False. To do this, you’ll use ”If Statements,” which are a crucial piece in just about every programming language. 0.2.1 If Statement In Matlab, If Statements are implemented as follows: if(condition) statements end
Background image of page 1

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

View Full DocumentRight Arrow Icon
As an example, let’s consider: if(age>=21) disp(’You can enter the bar’) end In this example, if age is greater than or equal to 21, then Matlab will display ”You can enter the bar.” If age is under 21, then Matlab doesn’t do anything! In essence, Matlab will only do something (execute the indicated statements) IF the condition is true. 0.2.2 If-Else Statements Sometimes, you’ll want to take one action if the condition is true and a different action if the con- dition is false. Matlab lets you use If-Else Statements to say ”If the condition is true, let’s only do the first set of actions; however, if the condition is false, let’s only execute the second set of actions.” if(condition) statements else statements end As an example, let’s consider: if(age>=21) disp(’You can enter the bar’) else disp(’Sorry, you have to stand outside’); end In this example, if age is greater than or equal to 21, then Matlab will display ”You can enter the bar.” If age is under 21, then Matlab displays ”Sorry, you have to stand outside.” Notice that only one of the actions is taken, not both. 0.2.3 If-Elseif-. ..-Else Statements Sometimes, you’ll want your program to choose one from myriad courses of action. In these in- stances, use If-Elseif-Else statements. if(condition) statement1 elseif(condition2) statement2 ... else statement3 end
Background image of page 2
Matlab will first evaluate the truth of the If condition. If that condition is true, it executes state- ment1 and then skips down to the end. If that condition is not true, but condition2 is indeed true, Matlab executes statement2 and then skips down to the end. If Matlab goes through the if condition and all of the elseif conditions and hasn’t found anything that’s true, it will execute the statements under else (statement3). Note that you can have as many
Background image of page 3

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

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

This note was uploaded on 11/09/2011 for the course ENGINEERIN 127 taught by Professor Finch during the Fall '08 term at Rutgers.

Page1 / 8

14-440-127 Lecture 2 Notes - 14:440:127 Introduction to...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online