General Computer Science for Engineers CISC 106 Lecture 10 Roger Craig Computer and Information Sciences 3/06/2009.

Slides:



Advertisements
Similar presentations
Control Structures.
Advertisements

Chapter 4: Control Structures I (Selection)
A number of MATLAB statements that allow us to control the order in which statements are executed in a program. There are two broad categories of control.
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009.
Week 10 Recap CSE 115 Spring For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each.
Monday, 12/9/02, Slide #1 CS 106 Intro to CS 1 Monday, 12/9/02  QUESTIONS??  On HW #5 (Due 5 pm today)  Today:  Recursive functions  Reading: Chapter.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
General Computer Science for Engineers CISC 106 Lecture 13 Roger Craig Computer and Information Sciences 3/13/2009.
Program Control Dilshad M. Shahid New York
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 9/18/2009.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
General Computer Science for Engineers CISC 106 Lecture 33 Dr. John Cavazos Computer and Information Sciences 05/11/2009.
General Computer Science for Engineers CISC 106 Lecture 05 Dr. John Cavazos Computer and Information Sciences 2/20/2009.
General Computer Science for Engineers CISC 106 Lecture 08 James Atlas Computer and Information Sciences 9/21/2009.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
4. Python - Basic Operators
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
Dale Roberts Program Control using Java - Boolean Expressions Dale Roberts, Lecturer Computer Science, IUPUI Department of.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Recursion) Outline 5.13Recursion 5.14Example.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (4): Control Flow (Chapter 2)
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Lesson - 7. Operators There are three types of operators: Arithmetic Operators Relational and Equality Operators Logical Operators.
Basic Control Structures
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Introduction to branching.
Week 8: Decisions Bryan Burlingame 21 October 2015.
General Computer Science for Engineers CISC 106 Lecture 2^4 Roger Craig Computer and Information Sciences 03/23/2009.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.
Conditional Logic in MATLAB By Bruce Raine. How to do a basic IF – END structure in MATLAB if MATLAB Commands end i.e. do the MATLAB commands if the conditional.
General Computer Science for Engineers CISC 106 Lecture 06 James Atlas Computer and Information Sciences 06/24/2009.
Sum of Arithmetic Sequences. Definitions Sequence Series.
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Beginning Programming for Engineers Matlab Conditional Computation.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
Control Flow (Python) Dr. José M. Reyes Álamo.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
CMSC201 Computer Science I for Majors Lecture 03 – Operators
UMBC CMSC 104 – Section 01, Fall 2016
Sequence, Selection, Iteration The IF Statement
Control Structures: Part 2
JavaScript: Control Statements.
A computer program is a sequence of computer commands.
Relational & Logical Operators
MSIS 655 Advanced Business Applications Programming
Relational and Logical Operators
Relational Operators Operator Meaning < Less than > Greater than
3 Control Statements:.
Introduction to Programming Using Python PART 2
Computer Science Core Concepts
Introduction to Programming – 4 Operators
Relational and Logical Operators
Relational and Logical Operators
Matlab Basics.
Relational and Logical Operators
Chap 7. Advanced Control Statements in Java
Relational and Logical Operators
REPETITION Why Repetition?
Presentation transcript:

General Computer Science for Engineers CISC 106 Lecture 10 Roger Craig Computer and Information Sciences 3/06/2009

Lecture Overview Lab (due Monday at 11:55pm) / Midterm Review (3/17) Sequence, selection, and repetition Recursion cont. Complex IF statement conditions

Sequence, selection, repetition Sequence (statements are ordered, 1, 2, 3…etc.) Selection (logical control, IF statement) Repetition (iteration and recursion) With these three control structures, you can program anything

Recursion cont. Simple functions f(x) = pi * x^2 f (b, h) = ½ * b * h Functions involving repetition f (x) = sum of the squares integers from 1 to x Can do the above with a FOR loop. Can we do it recursively?

Recursion cont. f (x) = if x == 1, return 1^2 if x > 1, return x^2 + f(x-1) Notice we subtract 1 from x when calling f(x-1). This allows us to get closer to our base case so that we can eventually finish. How to write that function in Matlab?

Fibonacci sequence (a famous recursive function) F0F0 F1F1 F2F2 F3F3 F4F4 F5F5 F6F6 F7F7 F8F8 F9F9 F 10 F

IF statements if statements anatomy of an if statement simple conditions complex conditions (using AND, OR and NOT)

IF statements ◦ If (control expression) Code to execute if ‘control expression’ is true else Code to execute if ‘control expression’ is false end If there is no ‘else’ part, it may be omitted. E.g. If (x > 2) fprintf(‘%f is greater than 2’, x); end

Relational Operators ◦ A < B A is less than B ◦ A > B A is greater than B ◦ A <= B A is less than or equal to B ◦ A >= B A is greater than or equal to B ◦ A == B A is equal to B ◦ A ~= B A not equal B

Logic Operators OperationOperatorExample NOT~~T = F OR| (pipe; shift backslash) F | T = T AND&F & T = F XORxorF & T = T

Control expressions ◦ Control expressions can be simple If (x > 2) fprintf(‘%f is greater than 2’, x); end ◦ Or more advanced: If ((x > 2) && (x < 4)) fprintf(‘%f is greater than 2, but less than 4’, x); end If ((x == 2) || (x == 4)) fprintf(‘%f is either 2 or 4’, x); end

Logic Operators ◦ NOT is unary operator (it only acts on one thing, and flips it) ◦ AND, OR, and XOR are binary operators. (need to have a pair of things to compare.) ◦ OR is inclusive OR (both A and B can be TRUE for A OR B to be TRUE) ◦ XOR is exclusive OR (only one of A and B can be TRUE for A OR B to be TRUE)

Order of operations 1.Arithmetic operators (+, -, *, / etc.) 2.Relational operators (==,, etc.) 3.NOT operator (~) 4.AND operators (&, &&) 5.OR and XOR operators (|, ||, xor) As with arithmetic expressions, parentheses can be used to change the default order of evaluation.