Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,

Similar presentations


Presentation on theme: "The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,"— Presentation transcript:

1 The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging, Relational and Logical Operators, Flow Charts, Conditional Statements

2 Lecture #1 Review Course introduction –course policies –assessment and deadlines MATLAB as a calculator MATLAB variables Script files

3 Lecture #2 Outline Debugging Relational and logical operators Flow Charts Conditional statements

4 Debugging Sometimes our program has errors or does not do exactly what we want. –the program has BUGS! If the program is small it may be easy to see what is wrong. If the program is large or complex then it may be hard to find the error. We can then DEBUG the program.

5 Debugging Can set a break in a script file –MATLAB runs script up to the break Then you control the program (step-by- step.

6 The Debug Menu

7 Relational Operators Relational statements test relationships between variables. A == B tests whether A equals B A ~= B tests whether A does not equal B A < B tests whether A is less than B A > B tests whether A is greater than B A <= B tests whether A is less than or equal to B A >= B tests whether A is greater than or equal to B

8 >> a=3; >> b=4; >> a==b ans = 0 >> a~=b ans = 1 >> a<b ans = 1 Using Relational Operators >> a>b ans = 0 >> a<=b ans = 1 >> a>=b ans = 0 false true

9 Logical Operators Logical operators are used to test conditions expressed as relationships between variables. “not” ( ~ in MATLAB), “and” ( & in MATLAB) and “or” ( | in MATLAB) are the most common logical operators. ~ ptrue if p is not true p & qtrue if both p and q are true p | qtrue if either p or q are true

10 Using Logical Operators >> a=3; >> b=4; >> ~(a==b) ans = 1 >> c=5; >> (a<b) & (b<c) ans = 1 >> (a>b) | (a>c) ans = 0 >> (a>b) | (b<c) ans = 1

11 Conditional Statements Dissecting the conditional: “If it is sunny outside then I will have a picnic.” “If the All Blacks win then I will be happy.” –Using MATLAB “If a < b then disp(a) ” condition dependent condition dependent condition dependent

12 Pseudocode and Flowcharts Pseudocode –Text description of program steps. May contain fragments of code. Doesn’t contain the nitty-gritty details. –Can use this to program any language. Flowcharts –Geometric symbols to describe program steps. –Captures the “flow” of the program. –Also useful for any programming language.

13 Flowchart Elements read radius area =  radius 2 print radius, area is radius < 0 ? start main stop main Beginning of algorithm End of algorithm No Yes Input Computation Output Comparisons From Etter Figure 3.1 page 87

14 if … end Syntax if condition some commands end end lets MATLAB know when the conditional statement is finished dependent

15 if … end Example File: conditional.m a=2; b=5; if a<b disp(a) end Matlab command prompt >> conditional 2 >>

16 Flowchart for if statement Simple is a < b ? No Yes display a begin end a=2 b=5

17 if … else … end Syntax if condition some commands else some other commands end This section is OPTIONAL end lets MATLAB know when the conditional statement is finished

18 if … else … end Example File: conditional.m a=5; b=4; if a<=b disp(a) else disp(b) end Matlab command prompt >> conditional 4 >>

19 Flowchart for if.. else statement is a <= b ? No Yes display a begin end display b a=5 b=4

20 if (a<b) & (b<c) disp(‘b is between a and c’) end Building Conditions Suppose we are interested in 3 variables ( a, b, c ). a < b < c ? In MATLAB we construct two conditions and test whether both are true. Note use of “&” to test whether both conditions are true

21 Boolean Variables Boolean variables used to store “true” and “false” values. They can be very useful for relational statements and conditional statements. MATLAB uses 1 to represent true and 0 to represent false. –However, MATLAB considers any NONZERO number to be “true”

22 Boolean Variables Create boolean variables just like other variables: Use boolean variables to store answer to some “question” that controls a conditional statement. isFinished = 1 % true isFound = 0 % false Variable name Variable value

23 Naming Boolean Variables Should be able to tell the type of a variable from its name. Don’t use boolean status but isSuccessful. Try to write statements which read well. if isSuccessfulif ~isFailuredo somethingend


Download ppt "The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,"

Similar presentations


Ads by Google