introduction to engineering matlab - 13 agenda conditional statements if – end if – else –...

23
Introduction to Engineering MATLAB - 13 Agenda Conditional statements If – end If – else – end if – elseif – else - end

Upload: leo-walton

Post on 17-Jan-2016

233 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end

Introduction to EngineeringMATLAB - 13

Agenda Conditional statements

If – end If – else – end if – elseif – else - end

Page 2: Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end

CONDITIONAL STATEMENTS

Conditional statements enable MATLAB to make decisions.

The process is similar to the way we (humans) make decisions.

A condition stated. If the condition is met, one set of actions is

taken. If the condition is not met, either nothing is done, or a

second set of actions is taken.

Example:

If I win the Lottery,

I will quit college, buy a new car, and go fishing.

If I do not win the Lottery,

I will study harder so that I can get a better job.

Page 3: Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end

Conditional Statements

NEED AN EXAMPLE OF HOW “IF…ELSE” STATEMENTS RUN IN A PRORGAM; E.G. MACHINERY IN A FACTORY

Page 4: Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end

THE FORM OF A CONDITIONAL STATEMENT

if variable Relational operator variable or a number

Relational operator Meaning

< Less than.

<= Less than or equal to.

> Greater than.

>= Greater then or equal to.

== Equal to.

~= Not equal to.

Page 5: Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end

EXAMPLES OF CONDITIONAL STATEMENTS

If a < b

If c >= 5

If a == b

If a ~= 0

variable

Relational operator

Variable previouslydefined or a number

Page 6: Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end

ADDING LOGICAL OPERATORS TO

CONDITIONAL STATEMENTS

Logical Operator Name Meaning

& AND True if both conditionalstatements are true.

| OR True if either or bothconditional statementsare true.

~ NOT True if the conditionalstatement is false

Page 7: Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end

EXAMPLES OF LOGICAL OPERATORS IN

CONDITIONAL STATEMENTS

if (d < h) & (x >= 7) True if d is smaller than h and x is equalor larger than 7.

if (x ~= 13) | (y < 0) True if x is not equal to 13 and/or if y issmaller than 0.

If ~(x == y) True if x is not equal to y.

Page 8: Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end

THE THREE FORMS OF THE if STATEMENT

if conditional statement

command group 1

else

command group 2

end

If conditional statement

commands

end

if conditional statement 1

command group 1

elseif conditional statement 2

command group 2

else

command group 3

end

Page 9: Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end

Example: if-end STATEMENT In a soft drink factory,

every can is weighed, and if it weighs 11.750 oz. or more, it is put in a package

If weight ≥ 11.75 oz.

Put can in package

end

weight ≥ 11.75 oz.

True

False

if

end

Put can in package

Page 10: Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end

THE if – end STATEMENT

If conditional statement

commands

end

CONDITIONALSTATEMENT

COMMANDS

True

False

if

end

Flowchart of the if – end statement:

Page 11: Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end

% A script file that demonstrates the use of the if-end statement.

% The user is asked to enter three grades.

% The program calculates the average of the grades.

% If the average is less than 60, a massage:

% The student did not pass the course. is printed.

score = input('Enter (as a vector) the scores of the three tests ');

ave_grade = (score(1) + score(2) + score(3))/3;

disp('The average grade is:')

disp(ave_grade)

if ave_grade < 60

disp('The student did not pass the course.')

end

EXAMPLE OF USING THE if – end STATEMENT

Page 12: Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end

EXAMPLE OF USING THE if – end STATEMENT

Executing the script file of the previous slide in the Command

Window:

>> Lecture8Example1

Enter (as a vector) the scores of the three tests [78 61 85]

The average grade is:

74.6667

>> Lecture8Example1

Enter (as a vector) the scores of the three tests [60 38 55]

The average grade is:

51

The student did not pass the course.

Page 13: Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end

THE if – else - end STATEMENT

Flowchart of theif – else – end statement:

CONDITIONALSTATEMENT

True

False

if

end

COMMANDGROUP 2

COMMANDGROUP 1

if conditional statement

command group 1

else

command group 2

end

Page 14: Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end

EXAMPLE OF USING THE if – else - end STATEMENT

% A script file that demonstrates the use of the if-else-end statement.

% The user is asked to enter three grades. The program calculates

% the average of the grades. If the average is less than 60, a

% massage: The student did not pass the course. is printed.

% Otherwise, a massage: The student passed the course. is printed.

score = input('Enter (as a vector) the scores of the three tests ');

ave_grade = (score(1) + score(2) + score(3))/3;

disp('The average grade is:')

disp(ave_grade)

if ave_grade < 60

disp('The student did not pass the course.')

else

disp('The student passed the course.')

end

Page 15: Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end

EXAMPLE OF USING THE if - else – end STATEMENT

Executing the script file of the previous slide in the Command

Window:

>> Lecture8Example2

Enter (as a vector) the scores of the three tests [65 80 83]

The average grade is:

76

The student passed the course.

>> Lecture8Example2

Enter (as a vector) the scores of the three tests [60 40 55]

The average grade is:

51.6667

The student did not pass the course.

Page 16: Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end

THE if – elseif – else – end STATEMENT

if conditional statement 1

command group 1

elseif conditional statement 2

command group 2

else

command group 3

end

Page 17: Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end

CONDITIONALSTATEMENT 1

True

False

if

end

COMMANDGROUP 2

COMMANDGROUP 1

CONDITIONALSTATEMENT 2

COMMANDGROUP 3

elseif

True

False

FLOWCHART OF THE

if – elseif – else – end STATEMENT

Page 18: Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end

In-Class Exercise

Create a real-life situation that uses “if-else or if-else-if statements

Create a flow-chart showing the following: The logic for the sorter and your team’s

conveyor system The logic for your team’s counter

Page 19: Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end

EXAMPLE OF USING THEif – elseif – else – end STATEMENT

% A script file that demonstrates the use of the if-elseif-else-end

% statement.

% The program calculates the tip in a restaurant according to the

% amount of the bill.

% If the bill is less than $10 the tip is $1.80.

% Between $10 and $60 the tip is 18% of the bill.

% Above $60 the tip is 20% of the bill.

format bank

clear tip

(The file continues on the next slide)

Page 20: Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end

bill = input('Enter the amount of the bill (in dollars): ');

if bill <=0

disp('Error. The amount can not be zero or negative')

elseif (bill > 0) & (bill <= 10)

tip = 1.8;

disp('The tip is (in dollars):')

disp(tip)

elseif (bill > 10) & (bill <= 60)

tip = bill*0.18;

disp('The tip is (in dollars):')

disp(tip)

else

tip = bill*0.2;

disp('The tip is (in dollars):')

disp(tip)

end

(Continuation from the previous slide)

Page 21: Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end

EXECUTING THE SCRIPT FILE OF THERESTAURAT TIP CALCULATION

>> Lecture8Example3

Enter the amount of the bill (in dollars): 15

The tip is (in dollars):

2.70>> Lecture8Example3

Enter the amount of the bill (in dollars): -21

Error. The amount can not be zero or negative

>> Lecture8Example3

Enter the amount of the bill (in dollars): 6

The tip is (in dollars):

1.80

>> Lecture8Example3

Enter the amount of the bill (in dollars): 100

The tip is (in dollars):

20.00

Page 22: Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end

COMMENTS ABOUT if STATEMENTS

For every if command a computer program must have an

end command.

A program can have many if ….. end statements following

each other.

A computer program can perform the same task using

different combinations of if - end, if – else – end, and if

– elseif – else – end statements.

Page 23: Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end

Assignment #9

Problem 25 page 61 in the textbook. Submit a printout of the script file and a

printout of the command window when the script file was used for the different values of x.

Problem 14 page 217 in the textbook. Submit a printout of the function file and a

printout of the command window when the function was used for the different values of W.