Date post: | 22-Dec-2015 |
Category: |
Documents |
Upload: | colleen-carpenter |
View: | 217 times |
Download: | 1 times |
Chapter 4MATLAB Programming
Logical Structures
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Logical if Statements
• We previously examined if statements in Excel:
IF(condition, value if true, value if false)• Since there are different values corresponding to
true and false conditions, this type of logic is termed if-else
• MATLAB has several types of if constructs, beginning with the most basic – a simple if statement
Engineering Computation: An Introduction Using MATLAB and Excel
if Statement
• The simple if statement specifies that a command or group of commands will be executed only if a condition exists
• If the condition does not exist, then the command(s) are simply bypassed, and the execution of the program continues
• All if statements must have an end statement as well
Engineering Computation: An Introduction Using MATLAB and Excel
Example
• Let’s consider the example of grades again• With a if statement, we can mark a passing grade:
• Note that nothing is printed if the grade is not passing
Engineering Computation: An Introduction Using MATLAB and Excel
g = input( 'Enter numerical grade ');if g >= 60 grade = 'P'end
Flowchart
Engineering Computation: An Introduction Using MATLAB and Excel
g >= 60?
No
Yes Grade = P
Input Numerical Grade g
Example
Engineering Computation: An Introduction Using MATLAB and Excel
>> GradesEnter numerical grade 75grade =P
>> GradesEnter numerical grade 55>>
if-else Statement
• This form is similar to the Excel IF statement in that two alternative paths are presented
• If the condition is true, then one set of commands is executed. If the condition is not true (else), then another set of commands is executed
• The same result can be achieved by “stacking” simple if statements, but this form is more compact
• Also, it guarantees that one of the alternative paths is followed
Engineering Computation: An Introduction Using MATLAB and Excel
Example
• Adding to the previous commands, we can designate a grade as either passing or failing
• Note that one of the two options must be chosen
Engineering Computation: An Introduction Using MATLAB and Excel
g = input( 'Enter numerical grade ');if g >= 60 grade = 'P'else grade = 'F'end
Flowchart
Engineering Computation: An Introduction Using MATLAB and Excel
Note that one of the output paths must be followed
g >= 60?
No
Yes Grade = P
Input Numerical Grade g
Grade = F
Example
Engineering Computation: An Introduction Using MATLAB and Excel
>> GradesEnter numerical grade 75grade =P
>> GradesEnter numerical grade 55grade =F>>
if-elseif Statement
• This structure allows for more than two paths to be considered
• More compact structure than nested if statements• As soon as a condition is satisfied, then the flow of
calculations proceeds to the end statement
Engineering Computation: An Introduction Using MATLAB and Excel
Example
• Grades example modified to report letter grades:
Engineering Computation: An Introduction Using MATLAB and Excel
g = input( 'Enter numerical grade ');if g >= 90 grade = 'A'elseif g >= 80 grade = 'B'elseif g >= 70 grade = 'C'elseif g >= 60 grade = 'D'else grade = 'F'end
Flowchart
Engineering Computation: An Introduction Using MATLAB and Excel
g >= 90?
No
Yes
Input Numerical Grade g
Grade = F
g >= 60?
g >= 70?
g >= 80?
NoNoNo
Grade = FGrade = DGrade = CGrade = B
Example
Engineering Computation: An Introduction Using MATLAB and Excel
>> GradesEnter numerical grade 85grade =B
>> GradesEnter numerical grade 68grade =D
>> GradesEnter numerical grade 55grade =F
Summary
• if statement: execute a command or set of commands if a condition is true, otherwise skip the commands
• if-else statement: execute one set of commands if a condition is true, execute a different set of commands if the condition is false
• if-elseif-else: allows the checking of multiple conditions
Engineering Computation: An Introduction Using MATLAB and Excel