15
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display

Embed Size (px)

Citation preview

Page 1: Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display

Chapter 4MATLAB Programming

Logical Structures

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 2: Chapter 4 MATLAB 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

Page 3: Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display

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

Page 4: Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display

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

Page 5: Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display

Flowchart

Engineering Computation: An Introduction Using MATLAB and Excel

g >= 60?

No

Yes Grade = P

Input Numerical Grade g

Page 6: Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display

Example

Engineering Computation: An Introduction Using MATLAB and Excel

>> GradesEnter numerical grade 75grade =P

>> GradesEnter numerical grade 55>>

Page 7: Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display

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

Page 8: Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display

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

Page 9: Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display

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

Page 10: Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display

Example

Engineering Computation: An Introduction Using MATLAB and Excel

>> GradesEnter numerical grade 75grade =P

>> GradesEnter numerical grade 55grade =F>>

Page 11: Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display

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

Page 12: Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display

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

Page 13: Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display

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

Page 14: Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display

Example

Engineering Computation: An Introduction Using MATLAB and Excel

>> GradesEnter numerical grade 85grade =B

>> GradesEnter numerical grade 68grade =D

>> GradesEnter numerical grade 55grade =F

Page 15: Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display

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