Presentation is loading. Please wait.

Presentation is loading. Please wait.

EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.

Similar presentations


Presentation on theme: "EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers."— Presentation transcript:

1 EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers

2 Lecture Outline Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers Branches Slide 2 of 16

3 Branching & Program Design Branches – Program Flow Control Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers Program Control Flow  Refers to the order in which each statement of a program is executed  Sequential Flow (prior Chapters) o Simple top to bottom no branches!!  Line #1, line #2, …, last line.  Logical Branching (Current Chapter) o Program flow based on Branching via Logical comparisons  E.g., if, switch, try, break, …  Looping (Next Chapter) o Conditional repetative execution of statements  For, while, … Slide 3 of 16

4 Branching & Program Design Branches – The if Statement Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers There are 3 forms of the if statement if (Logical Expression) statement(s) end if (Logical Expression) statement(s) else other statement(s) end Slide 4 of 16

5 Branching & Program Design Branches – The if Statement Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers There are 3 forms of the if statement if (Logical Expression1) statement(s) elseif (Logical Expression2) other statement(s) elseif (Logical Expression3) other statement(s). else other statement(s) end Slide 5 of 16......

6 Branching & Program Design Branches – A 1 st Example: The Guessing Game Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers An Example: The Guessing Game  Problem: o I am thinking of an integer between 1 and 10 o Can you guess what number I am thinking of? Lets go through the 5 Step Top-Down Design Procedure Step 1: Clearly state the problem  Use the computer to randomly generate an integer 1  number  10  Prompt the user for a guess at the number  Respond to the guess Slide 6 of 16

7 Branching & Program Design Branches – A 1 st Example: The Guessing Game Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers Step 2: Define inputs and outputs Step 3: Design the Algorithm (pseudocode)  Generate a random integer: 1  num  10  Prompt the user to guess the number  Test the guess and issue a response My Program guess Response Integer Text string Slide 7 of 16

8 Branching & Program Design Branches – A 1 st Example: The Guessing Game Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers Step 4: Convert the Algorithm into Code % Generate a random integer: 1 <= num <= 10 num = rand; % Generate a random number betw 0 & 1 num = ceil(10*num); % Convert to an integer betw 1 & 10 % Prompt the user to guess the number guess = input('Guess a Integer from 1 to 10: '); % Test the guess and issue a response if guess == num disp('Your Guess is Correct!!'); else disp('Your Guess is NOT Correct!!') end Step 5: Test the resulting program Could use randi(10) Guessing_Game_v1.m Slide 8 of 16

9 Branching & Program Design Branches – A 1 st Example: The Guessing Game Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers Let’s Modify the program to provide a “better” response % Test the guess and issue a response of Correct, % too high, or too low if guess < num disp('Your Guess is Too Low!!'); elseif guess > num disp('Your Guess is Too High!!'); else disp('Your Guess is Correct!!') end Notice that only one of the conditional blocks will ever get processed!! Could use another elseif => Less Efficient What if the user entered a non-integer? Guessing_Game_v2.m Slide 9 of 16

10 Branching & Program Design Branches – A 1 st Example: The Guessing Game Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers We could also use nested if structures: % Test the guess and issue a response of Correct, % too high, or too low if guess < num disp('Your Guess is Too Low!!'); else if guess > num disp('Your Guess is Too High!!'); else disp('Your Guess is Correct!!') end It is good programming practice to indent code Guessing_Game_v3.m Slide 10 of 16

11 Branching & Program Design Branches – A 2 nd Example: Converting to Letter Grades Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers An Example: Convert Numeric Grades to Letter Grades  Given a numeric grade determine the letter equivalent. Step 1: Clearly state the problem:  Given the relationship between numeric and letter grades as: o Numeric Grade ≥ 90 is an A o 80 ≤ Numeric Grade < 90 is an B o 70 ≤ Numeric Grade < 80 is an C o 60 ≤ Numeric Grade < 70 is an D o Numeric Grade < 60 is an F  Prompt the user for a numeric grade  Respond with the letter grade equivalent Slide 11 of 16

12 Branching & Program Design Branches – A 2 nd Example Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers Step 2: Define inputs and outputs Step 3: Design the Algorithm (pseudocode)  Prompt the user for the numeric grade  Determine and respond with the letter grade equivalent My Program grade Response float Text string Slide 12 of 16

13 Branching & Program Design Branches – A 2 nd Example Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers Step 4: Convert the Algorithm into Code % Prompt the user for the numeric grade numeric_grade = input('Enter the Numeric Grade (0-100): '); % Convert a numeric grade to a letter Grade if numeric_grade < 60 disp('Letter Grade = F'); elseif numeric_grade < 70 disp('Letter Grade = D'); elseif numeric_grade < 80 disp('Letter Grade = C'); elseif numeric_grade < 90 disp('Letter Grade = B'); else disp('Letter Grade = A') end What happens if the numeric grade entered is greater than 100? Let’s analyze the program’s behavior in the debugger. Numeric_Grade_Converter_v1.m Slide 13 of 16

14 Branching & Program Design Branches – A 2 nd Example Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers Step 5: Test the resulting program  What if the user provides an invalid input? o Approach #1: Trap all possible errors  Create a variable to denote if error and type of error » E.g., Error_Var = 0 (no error) or = 1 (wrong type) or …  If no error (Error_Var = 0) continue as usual  if error found, print message identifying the type of error » Some types can be “fixed,” then continue, others not, then stop. o Approach #2:  If an error is found terminate execution and print “Error Found – Terminating Execution” o Approach #3:  If an error is found prompt the user to re-enter the input Slide 14 of 16

15 Branching & Program Design Branches – A 2 nd Example Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers Trapping invalid input entries – Approach #1 % Prompt the user for the numeric grade numeric_grade = input('Enter the Numeric Grade (0-100): '); % Check the input for validity assuming the input is numeric if ~isscalar(numeric_grade) % Detect non-scalar entry disp('You have entered a non-scalar Grade'); valid_input = false; elseif numeric_grade < 0 % Detect out of bounds entry: < 0 disp('You have entered a negative Grade'); valid_input = false; elseif numeric_grade > 100 % Detect out of bounds entry: > 100 disp('You have entered a Grade of > 100'); valid_input = false; else valid_input = true; end Numeric_Grade_Converter_v2.m Slide 15 of 16

16 Next Lecture Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers More Branching More Debugging Slide 16 of 16


Download ppt "EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers."

Similar presentations


Ads by Google