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

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Selection (decision) control structure Learning objective
ITEC113 Algorithms and Programming Techniques
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Conditional Statements Introduction to Computing Science and Programming I.
Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
BACS 287 Programming Logic 3. BACS 287 Iteration Constructs Iteration constructs are used when you want to execute a segment of code several times. In.
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Chapter 4: Looping CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
Flow Charting. Goals Create Algorithms using Flow Charting procedures. Distinguish between Flow Charting and Pseudocode. Top-Down Design Bottom-up Design.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Flow of Control Part 1: Selection
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
ITEC113 Algorithms and Programming Techniques
Branches and Program Design
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Course A201: Introduction to Programming 09/16/2010.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 1 Monday 29 Sept 2014 EGR 115 Introduction to Computing for Engineers.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 1 Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 4 Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 3 Decision Trees Conditionals.
Controlling Program Flow with Decision Structures.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
1 CSE1301 Computer Programming: Lecture 16 Flow Diagrams and Debugging.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
A First Book of C++ Chapter 4 Selection.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
EGR 115 Introduction to Computing for Engineers
Week of 12/12/16 Test Review.
Python: Control Structures
CHAPTER 4 Selection CSEG1003 Introduction to Computing
Chapter 4 MATLAB Programming
Chapter 4: Making Decisions.
PH2150 Scientific Computing Skills
1) C program development 2) Selection structure
Selection Statements.
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Programming Concepts and Database
Chapter 3: Selection Structures: Making Decisions
Control Structures.
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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