chapter-6 logical functions.ppt

52
Chapter-5-6 Logical functions, Selection structures and Repetition structures Faculty of Chemical & Natural Resources Engineering

Upload: mythili

Post on 14-Jul-2016

22 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter-6 Logical functions.ppt

Chapter-5-6Logical functions, Selection structures and Repetition

structures

Faculty of Chemical & Natural Resources Engineering

Page 2: Chapter-6 Logical functions.ppt

Content

Flow diagram while loop and for loop. Addition, subtraction and other mathematical

functions. Break and continue statement. Branches if else, switch/case. Debugging tools

Page 3: Chapter-6 Logical functions.ppt

Execution Control

This chapter discusses techniques for changing the flow of control of a program, which may be necessary for two reasons.

You may want to execute some parts of the code under certain circumstances only.

You may want to repeat a section of code a certain number of times.

Page 4: Chapter-6 Logical functions.ppt

Logical functions A sequence is a list of commands that are

executed one after another. A selection structure allows the programmer

to execute one command if some criterion is true and second if the criterion is false.

A repetition structure, or loop, causes a group of statements to be executed multiple times.

Page 5: Chapter-6 Logical functions.ppt

Structures

Sequence Selection Repetition

Sequence Selection Repetition (Loop)

Page 6: Chapter-6 Logical functions.ppt

The if/else structure

The simple if triggers the execution of a block of code if a condition is true.

If it is false that block of code is skipped, and the program continues without doing anything.

What if instead you want to execute an alternate set of code if the condition is false?

Page 7: Chapter-6 Logical functions.ppt

Block of code to excute if the

comparison is true

ComparisonTrue False

Block of code to excute if the

comparison is false

Flow chart of an if/else structure

Page 8: Chapter-6 Logical functions.ppt

Use an if structure to calculate a natural log Check to see if the input is positive

If it is, calculate the natural logIf it isn’t, send an error message to the screen

Page 9: Chapter-6 Logical functions.ppt

M-file Program

Page 10: Chapter-6 Logical functions.ppt

Interactions in the Command Window

Page 11: Chapter-6 Logical functions.ppt

The if/else/elseif structure

Use the elseif for multiple selection criteria For example

Write a program to determine if an applicant is eligible to drive

Page 12: Chapter-6 Logical functions.ppt

Start

if age<16 TrueSorry – You’ll have to wait

age<18You may have a youth license

age<70

True

You may have a standard license

True

Drivers over 70 require a special license

End

elseif

elseif

else

Page 13: Chapter-6 Logical functions.ppt
Page 14: Chapter-6 Logical functions.ppt

Always test your programs – making sure that you’ve covered all the possible calculational paths

Page 15: Chapter-6 Logical functions.ppt

As a general rule…

If structures work well for scalars For vectors or arrays use a find function or.. Combine if structures with a repetition

structure Repetition structures are introduced in the next

chapter

Page 16: Chapter-6 Logical functions.ppt

switch/case

This structure is an alternative to the if/else/elseif structure

The code is generally easier to read This structure allows you to choose between multiple

outcomes, based on some criterion, which must be exactly true

Page 17: Chapter-6 Logical functions.ppt

When to use switch/case

The criterion can be either a scalar (a number) or a string.

In practice, it is used more with strings than with numbers.

Page 18: Chapter-6 Logical functions.ppt

switch variablecase option1

code to be executed if variable is exactly equal to option 1

case option2code to be executed if variable is exactly

equal to option 2…

case option_ncode to be executed if variable is exactly equal to option n

otherwisecode to be executed if variable is not equal to any of the options

end

The structure of switch/case

Page 19: Chapter-6 Logical functions.ppt

Suppose you want to determine what the airfare is to one of three cities

Page 20: Chapter-6 Logical functions.ppt
Page 21: Chapter-6 Logical functions.ppt

Remember…You tell the input command to expect a string by adding ‘s’ in the second field.

Page 22: Chapter-6 Logical functions.ppt

Menu

The menu function is often used in conjunction with a switch/case structure.

This function causes a menu box to appear on the screen with a series of buttons defined by the programmer.

input = menu(‘Message to the user’,’text for button 1’,’text for button 2’, etc)

Page 23: Chapter-6 Logical functions.ppt

Because the input is controlled by a menu box, the user can’t accidentally enter a bad choice

This means you don’t need the otherwise portion of the switch/case structure

Page 24: Chapter-6 Logical functions.ppt

Note that the otherwise portion of the switch/case structure wasn’t used

Page 25: Chapter-6 Logical functions.ppt

When you run this code a menu box appears

Instead of entering your choice from the command window, you select one of the buttons from the menu

Page 26: Chapter-6 Logical functions.ppt

If I select Honolulu…

Page 27: Chapter-6 Logical functions.ppt

Summary

Sections of computer code can be categorized assequencesselection structuresrepetition structures

Page 28: Chapter-6 Logical functions.ppt

Summary – Sequence

Sequences are lists of instructions that are executed in order

Page 29: Chapter-6 Logical functions.ppt

Summary – Selection Structure

Selection structures allow the programmer to define criteria (conditional statements) which the program uses to choose execution paths

Page 30: Chapter-6 Logical functions.ppt

Summary – Repetition Structures

Repetition structures define loops where a sequence of instructions is repeated until some criterion is met (also defined by conditional statements).

Page 31: Chapter-6 Logical functions.ppt

Summary – Relational Operators

MATLAB uses the standard mathematical relational operators<<=>>===~=

Recall that = is the assignment operator, and can not be used for comparisons

Page 32: Chapter-6 Logical functions.ppt

Summary – Logical Operators

MATLAB uses the standard logical operators && and || or~ notxor exclusive or

Page 33: Chapter-6 Logical functions.ppt

Summary – if family

The family of if structures allows the programmer to identify alternate computing paths dependent upon the results of conditional statements. ifelseelseif

Page 34: Chapter-6 Logical functions.ppt

Summary switch/case

Similar to the if/elseif/else structure Commonly used with menu

Page 35: Chapter-6 Logical functions.ppt

X=5 Y=1 X<Y another example X=[1, 2, 3, 4,5] Y=[-2,0,2,4,6] Use same sign

Page 36: Chapter-6 Logical functions.ppt

Selection structuresIf comparison statementsEndif G<50disp(‘G is a small value equal to :’)disp(G);end

Page 37: Chapter-6 Logical functions.ppt

if /elseif/ else The elseif function allows you to check

multiple criteria while keeping the code easy to read.

eg

Page 38: Chapter-6 Logical functions.ppt

Example for “If”, command% example for if statementday = 1if day== 7 % saturday state = 'weekend'elseif day ==1 % sunday state = 'weekend'else state= 'weekday'end

Page 39: Chapter-6 Logical functions.ppt

Switch and Case The above structure is often used when a series of programming

path options exists for a given variable, depending on its value. The above structure can be used either a scalar ( a number) or a

string.switch variablecase option 1 code to be executed if variable equal to option 1case option 2 code executed if variable equal to option 2OtherwiseIf none of the optionend

Page 40: Chapter-6 Logical functions.ppt

For loop

% To understand For Loop% For loop will do iterations only the fixed number of timesclcclear SumNumbers =0 ;for I = 1:10 SumNumbers = SumNumbers+ Iend

Page 41: Chapter-6 Logical functions.ppt

Exercise

Convert the values from degree to radian from 10 degree to 360 degree with an increment of 10.

Develop the table for the results. Formula for Radian = (Value in Degree X Pi)/180

Page 42: Chapter-6 Logical functions.ppt

Example for loop

for k= 1:5a(k)=k^2end

Page 43: Chapter-6 Logical functions.ppt

while loop% To understand while loop% while loop is an iteration loop% It will keep on going round and round until the condition is satisfied.% This loop is used, when we dont know the exact number of iterations.

clcclearNumber =50 while Number > 20 Number = Number -5 end

Page 44: Chapter-6 Logical functions.ppt

Some Useful MATLAB commands what List all m-files in current directory dir List all files in current directory ls Same as dir type test Display test.m in command window delete test Delete test.m cd a: Change directory to a: chdir a: Same as cd pwd Show current directory which test Display directory path to ‘closest’

test.m

Page 45: Chapter-6 Logical functions.ppt

MATLAB Logical Operators

MATLAB supports three logical operators.

not ~ % highest precedenceand & % equal precedence with oror | % equal precedence with and

Page 46: Chapter-6 Logical functions.ppt

break and continue

break causes the loop to terminate prematurely

continue causes MATLAB to skip a pass through the loop, but continue on until the criteria for ending is met

both are used in conjunction with an if statement

Page 47: Chapter-6 Logical functions.ppt

This program prompts the user 10 times to enter a value, and computes the natural log.

If a negative value is entered, the break command causes MATLAB to exit the loop

Page 48: Chapter-6 Logical functions.ppt

Notice that n had a value of 3 when the program terminated – if it had run to completion it would have had a value of 10

Page 49: Chapter-6 Logical functions.ppt

The continue command is similar to break, however instead of terminating the loop, the program just skips to the next pass.

Page 50: Chapter-6 Logical functions.ppt

Matlab Selection Structures An if - elseif - else structure in MATLAB. Note that elseif is one word.

if expression1 % is true% execute these commands

elseif expression2 % is true% execute these commands

else % the default% execute these commands

end

Page 51: Chapter-6 Logical functions.ppt

MATLAB Repetition Structures

A for loop in MATLAB for x = array for X = 1:100 disp X

end

A while loop in MATLAB while expression while x <= 10

% execute these commands end

Page 52: Chapter-6 Logical functions.ppt