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

Slides:



Advertisements
Similar presentations
Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Advertisements

PSEUDOCODE & FLOW CHART
Session Objectives# 24 COULD code the solution for an algorithm
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009.
1 Chapter 2 Problem Solving Techniques INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD.
Flow Charting. Goals Create Algorithms using Flow Charting procedures. Distinguish between Flow Charting and Pseudocode. Top-Down Design Bottom-up Design.
General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 06/29/2009.
ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
TMF1013 : Introduction To Computing Lecture 1 : Fundamental of Computer ComputerFoudamentals.
CHAPTER 4: CONDITIONAL STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.
The Hashemite University Computer Engineering Department
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
Algorithms and Flowcharts
An Introduction to Programming with C++ Sixth Edition Chapter 8 More on the Repetition Structure.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
All materials copyright UMBC unless otherwise noted CMSC201 Computer Science I for Majors Lecture 02 – Algorithmic Thinking.
Component 1.6.
Starter What does the following code do?
Development Environment
ALGORITHMS AND FLOWCHARTS
EGR 115 Introduction to Computing for Engineers
Introduction to Computing Science and Programming I
Topic: Introduction to Computing Science and Programming + Algorithm
REPETITION CONTROL STRUCTURE
1-1 Logic and Syntax A computer program is a solution to a problem.
Introduction to Programming for Mechanical Engineers (ME 319)
Python: Control Structures
System Design.
Chapter 4 MATLAB Programming
Programming Mehdi Bukhari.
CS1001 Programming Fundamentals 3(3-0) Lecture 2
CS1371 Introduction to Computing for Engineers
The Selection Structure
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 4: Decision Structures and Boolean Logic
Scripts & Functions Scripts and functions are contained in .m-files
Lesson 9: "if-else-if" and Conditional Logic
ALGORITHMS AND FLOWCHARTS
Conditional Statements
Programming Right from the Start with Visual Basic .NET 1/e
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
ALGORITHMS AND FLOWCHARTS
Chapter 4: Decision Structures and Boolean Logic
SME1013 PROGRAMMING FOR ENGINEERS
Design and Implementation
Introduction to Algorithms and Programming
CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter 2- Visual Basic Schneider
SME1013 PROGRAMMING FOR ENGINEERS
Algorithm and Ambiguity
ICT Gaming Lesson 2.
Programming Concepts and Database
CHAPTER 4: Conditional Structures
Tonga Institute of Higher Education IT 141: Information Systems
Experiment No. (1) - an introduction to MATLAB
Tonga Institute of Higher Education IT 141: Information Systems
Basic Concepts of Algorithm
Chapter 4: Decision Structures and Boolean Logic
Presentation transcript:

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

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

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

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.

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

The Debug Menu

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

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

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

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

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

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.

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

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

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

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

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

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

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

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

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”

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

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