Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 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

3 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

4 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

5 Flowchart Engineering Computation: An Introduction Using MATLAB and Excel

6 Example Engineering Computation: An Introduction Using MATLAB and Excel >> Grades Enter numerical grade 75 grade = P >> Grades Enter numerical grade 55 >>

7 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

8 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

9 Flowchart Engineering Computation: An Introduction Using MATLAB and Excel Note that one of the output paths must be followed

10 Example Engineering Computation: An Introduction Using MATLAB and Excel >> Grades Enter numerical grade 75 grade = P >> Grades Enter numerical grade 55 grade = F >>

11 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

12 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

13 Flowchart Engineering Computation: An Introduction Using MATLAB and Excel

14 Example Engineering Computation: An Introduction Using MATLAB and Excel >> Grades Enter numerical grade 85 grade = B >> Grades Enter numerical grade 68 grade = D >> Grades Enter numerical grade 55 grade = F

15 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


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

Similar presentations


Ads by Google