Download presentation
Presentation is loading. Please wait.
Published byThomasina Kelley Modified over 8 years ago
1
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 4 Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers
2
Lecture Outline Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers More Branching More Debugging Slide 2 of 16
3
Branching & Program Design Branching – The Switch Statement Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers The “switch” statement is another branching construct Typically used to test if an expression is equal to one of an enumerated set of choices. General form of the structure: Slide 3 of 16 switch variable case {value1, …, valueL} some code; case {valueM, …,valueN} some more code; otherwise even more code; end switch variable case {value1, …, valueL} some code; case {valueM, …,valueN} some more code; otherwise even more code; end variable = value1, value2, …, or valueN } Code Block #1 } Code Block #2
4
Branching & Program Design Branching – The Switch Statement Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers An Example: Output the number of days in a user selected month (NOT a leap year): o Month = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, or 12 o Months with 30 days = 9, 4, 6, or 11 o Months with 31 days = 1, 3, 5, 7, 8, 10, or 12 o Months with 28 days = 2 Slide 4 of 16 switch month case {2} disp('This month has 28 days'); case {9, 4, 6, 11} disp('This month has 30 days'); case {1, 3, 5, 7, 8, 10, 12} disp('This month has 31 days'); otherwise disp('Invalid Month'); end GOOD PRACTICE: include otherwise statement to trap illegal values of variable NOTE: Only one case will ever be executed
5
Branching & Program Design Branching – The Switch Statement Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers Comparison between if and switch: Switch better for enumerated variable If better for testing variables over continuous ranges Slide 5 of 16 switch month case {2} disp('This month has 28 days'); case {9, 4, 6, 11} disp('This month has 30 days'); case {1, 3, 5, 7, 8, 10, 12} disp('This month has 31 days'); otherwise disp('Invalid Month'); end Code continues on next line if month == 2 disp('This month has 28 days'); elseif (month == 9 | month == 4 |... month == 6 | month == 11) disp('This month has 30 days'); elseif (month == 1 | month == 3 |... month == 5 | month == 7 |... month == 8 | month == 10|... month == 12) disp('This month has 31 days'); else disp('Invalid Month'); end
6
Branching & Program Design Branching – The Switch Statement Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers In Class Programming Exercise: Prompt the user for an integer from 1 to 10 Respond by telling the user whether it is odd or even number. 1. If your last name begins with A to L (inclusive) Use the switch construct 2. If your last name begins with M to Z (inclusive) Use the if construct Slide 6 of 16
7
Branching & Program Design Branching – The Try/Catch Construct Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers Typically, when MATLAB encounters a run-time error in your code the program aborts The Try/Catch construct allows you to “trap” the error and continue with execution Slide 7 of 16 try some statements; catch other statements; end } tries to execute these statements } execute these if an error above
8
Branching & Program Design Branching – The Try/Catch Construct Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers An Example: Given a row vector a with 10-elements display a(index) Slide 8 of 16 a = [9 8 7 6 5 4 3 2 1 0]; % A row vector with 10-elements try% Request an index and display a(index) index = input('Which element would you like to display? '); disp(['a(',num2str(index),') = ', num2str(a(index))]); catch% Run this code if an error occurs above disp('Indexing type Run-Time Error: 1 <= index <= 10 '); disp([' You entered index = ', num2str(index)]) end Try_catch_example.m
9
Branching & Program Design More on Debugging Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers More on Debugging: Debug the program: o There are ~ 6 different errors Slide 9 of 16 more_debugging_w_errors.m
10
Syntax Error: Should be ‘ input ’ NOT ‘ Input ’ Syntax Error: Should be ‘ input ’ NOT ‘ Input ’ Branching & Program Design Branching – The Switch Statement Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers Slide 10 of 16 … % Enter the real coefficients of a quadratic polynomial disp('Enter the coeffs of the quadratic: a x^2 + b x + c') a = Input('Enter the coeff. of x^2 term: a = '); b = Input('Enter the coeff. of x^1 term: b = '); c = Input('Enter the coeff. of x^0 term: c = '); % Compute the Discriminant: Delta = b^2 - 4ac Delta = b^2 - 4*a*c … Syntax Error: Missing a semicolon ‘;’ Syntax Error: Missing a semicolon ‘;’ ; ;
11
Arithmetic Run-Time Error: Should be ‘/ (2*a) ’ NOT ‘ /0*a ’ Arithmetic Run-Time Error: Should be ‘/ (2*a) ’ NOT ‘ /0*a ’ Logic Error: Should be ‘ x2 ’ NOT ‘ x1 ’ Logic Error: Should be ‘ x2 ’ NOT ‘ x1 ’ Logic Error: Should be ‘ > ’ NOT ‘ < ’ Logic Error: Should be ‘ > ’ NOT ‘ < ’ Branching & Program Design Branching – The Switch Statement Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers Slide 11 of 16 … % Solve for the roots of the quadratic polynomial if Delta < 0 % Case of real and distinct roots x1 = (-b + sqrt(Delta)) / 0*a; x2 = (-b - sqrt(Delta)) / 0*a; fprintf(' Real and distinct roots: x1 = %4.2f x2 = %4.2f\n', x1, x2) elseif Delta == 0 % Case of real repeated roots x1 = -b / (2*a); x2 = x1; fprintf(' Real and repeated roots: x1 = %4.2f x2 = %4.2f\n', x1, x2) else % Case of complex roots x1 = (-b + sqrt(Delta)) / (2*a); x2 = (-b - sqrt(Delta)) / (2*a); fprintf(' Imag roots: x1 = %g + j%g and x2 = %g + j%g \n',... real(x1), imag(x1), real(x1), imag(x1)); end …
12
Assignment Run-Time Error: Should be ‘ num2str(x2) ’ NOT ‘ (x2) ’ Assignment Run-Time Error: Should be ‘ num2str(x2) ’ NOT ‘ (x2) ’ Branching & Program Design Branching – The Switch Statement Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers Slide 12 of 16 … % Plot the two roots plot(real(x1), imag(x1),'rx', real(x2), imag(x2),'bo', 'MarkerSize',16, 'LineWidth',5) title(['Roots of polynomial: f(x) = ', poly, ' = 0'], 'FontSize', 16) xlabel('Real Axis'); ylabel('Imag Axis') legend(['x_1=',num2str(x1)],['x_2=', (x2)]) grid
13
Next Lecture Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers An Application Example Slide 13 of 16
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.