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

## Presentation on theme: "Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display."— Presentation transcript:

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

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

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

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