Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.

Similar presentations


Presentation on theme: "CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS."— Presentation transcript:

1 CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS

2 ARRAYS The important use of arrays as indices has to do with picking out the elements of a vector (or array) that have a certain property or characteristic. To do this requires to things: definition of the characteristic "Finding" the indices of the vector with that property

3 Boolean test on Vectors Consider the vector A = [ 5 3 1 7 -3 -1 9 27 6 1] Suppose want non-negative elements. Use a logic comparison to locate: Need notion of true/false – called Boolean Only two possible values (true or false) In Matlab false is 0, true is 1

4 Relational Operators < Less than <= Less than or equal to > Greater than >= Greater than or equal to == Equal to ~= Not equal to

5 Comparisons Comparisons are either true or false Most computer programs use the number 1 for true and 0 for false The results of a comparison are used in selection structures and repetition structures to make choices

6 MATLAB compares corresponding elements and determines if the result is true or false for each

7 Comparisons In order for MATLAB to decide a comparison is true for an entire matrix, it must be true for every element in the matrix

8 Logical Operators & and ~ not | or xor exclusive or

9 Simple “if”statement if comparison statements end For example…. if G<50 count = count +1; disp(G); end

10 IF statements Each branch can include multiple statements Statements can also include other if statements (can nest if statements inside if statements ) if A>10 % computations; end if condition statements true false if A>10 % computations; else % computations; end if condition statements (1) true false statements (2)

11 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?

12 Flow chart of an if/else structure Block of code to execute if the comparison is true Comparison TrueFalse Block of code to execute if the comparison is false

13 Calculation Example Use an if structure to calculate a natural log Check to see if the input is positive If it is, calculate the natural log If it isn’t, send an error message to the screen

14 M-file Program

15 Interactions in the Command Window

16 The if/else/elseif structure Use the elseif for multiple selection criteria For example if (grade >= 90) Letter = ‘A’ Disp(‘You did great’) % note disp function Elseif (grade >= 80) % didn’t need <90 Letter = ‘B’ Elseif (grade >= 70) Letter = ‘C’ Elseif (grade >= 60) Letter = ‘D’ Else Letter = ‘F’ End

17 Examples Write a program to determine if an applicant is eligible to drive Start if age<16 True Sorry – You’ll have to wait age<18 You may have a youth license age<70 True You may have a standard license True Drivers over 70 require a special license elseif else

18 M-file Program

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

20 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 The criterion can be either a scalar (a number) or a string. In practice, it is used more with strings than with numbers.

21 The structure of switch/case switch variable case option1 code to be executed if variable is exactly equal to option 1 case option2 code to be executed if variable is exactly equal to option 2 … case option_n code to be executed if variable is exactly equal to option n otherwise code to be executed if variable is not equal to any of the options end

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

23

24 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. 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

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

26 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


Download ppt "CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS."

Similar presentations


Ads by Google