CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
CMPS 1371 Introduction to Computing for Engineers
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS
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.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Conditional Statements If these were easy then everyone would them!
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
General Computer Science for Engineers CISC 106 Lecture 08 Dr. John Cavazos Computer and Information Sciences 2/27/2009.
Making Decisions In Python
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.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 6.
Lecture Set 5 Control Structures Part A - Decisions Structures.
COMP 116: Introduction to Scientific Programming Lecture 28: Midterm #2 Review.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
VBScript Language. What is VBScript Based on the Visual Basic family of languages Supports object oriented features Interpreted Loosely Typed Implicitly.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Matlab for Engineers Logical Functions and Control Structures Chapter 8.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Introduction to Problem Solving and Control Statements.
CSE123 Lecture 3 Files and File ManagementScripts.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
James Tam Making Decisions In Python In this section of notes you will learn how to have your programs choose between alternative courses of action.
Digital Image Processing Introduction to M-function Programming.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
31/01/ Selection If selection construct.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Decision Making and Branching
CSE202: Lecture 5The Ohio State University1 Selection Structures.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Chapter 4: Decisions and Conditions
Chapter 4: Decisions and Conditions
Introduction to Programming for Mechanical Engineers (ME 319)
Repetition Structures Chapter 9
Chapter 4: Making Decisions.
Chapter 4 MATLAB Programming
Selection Statements by Ahmet Sacan
Chapter 4: Making Decisions.
Microsoft Visual Basic 2005 BASICS
Conditional Statements
More Selections BIS1523 – Lecture 9.
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Selection Statements Chapter 4 Attaway MATLAB 4E.
Introduction to Problem Solving and Control Statements
Selection Statements.
Chapter 5: Control Structure
CS2011 Introduction to Programming I Selections (I)
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Selection Statements Chapter 3.
Selection Statements Chapter 4 Attaway MATLAB 5E.
Presentation transcript:

CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS

ARRAYS The important use of arrays as indices has to do with picking out the elements of a vector (or array) that have a certain property or characteristic. To do this requires to things: definition of the characteristic "Finding" the indices of the vector with that property

Boolean test on Vectors Consider the vector A = [ ] Suppose want non-negative elements. Use a logic comparison to locate: Need notion of true/false – called Boolean Only two possible values (true or false) In Matlab false is 0, true is 1

Relational Operators < Less than <= Less than or equal to > Greater than >= Greater than or equal to == Equal to ~= Not equal to

Comparisons Comparisons are either true or false Most computer programs use the number 1 for true and 0 for false The results of a comparison are used in selection structures and repetition structures to make choices

MATLAB compares corresponding elements and determines if the result is true or false for each

Comparisons In order for MATLAB to decide a comparison is true for an entire matrix, it must be true for every element in the matrix

Logical Operators & and ~ not | or xor exclusive or

Simple “if”statement if comparison statements end For example…. if G<50 count = count +1; disp(G); end

IF statements Each branch can include multiple statements Statements can also include other if statements (can nest if statements inside if statements ) if A>10 % computations; end if condition statements true false if A>10 % computations; else % computations; end if condition statements (1) true false statements (2)

The if/else structure The simple if triggers the execution of a block of code if a condition is true If it is false that block of code is skipped, and the program continues without doing anything What if instead you want to execute an alternate set of code if the condition is false?

Flow chart of an if/else structure Block of code to execute if the comparison is true Comparison TrueFalse Block of code to execute if the comparison is false

Calculation Example Use an if structure to calculate a natural log Check to see if the input is positive If it is, calculate the natural log If it isn’t, send an error message to the screen

M-file Program

Interactions in the Command Window

The if/else/elseif structure Use the elseif for multiple selection criteria For example if (grade >= 90) Letter = ‘A’ Disp(‘You did great’) % note disp function Elseif (grade >= 80) % didn’t need <90 Letter = ‘B’ Elseif (grade >= 70) Letter = ‘C’ Elseif (grade >= 60) Letter = ‘D’ Else Letter = ‘F’ End

Examples Write a program to determine if an applicant is eligible to drive Start if age<16 True Sorry – You’ll have to wait age<18 You may have a youth license age<70 True You may have a standard license True Drivers over 70 require a special license elseif else

M-file Program

Always test your programs – making sure that you’ve covered all the possible calculational paths

switch/case This structure is an alternative to the if/else/elseif structure The code is generally easier to read This structure allows you to choose between multiple outcomes, based on some criterion, which must be exactly true The criterion can be either a scalar (a number) or a string. In practice, it is used more with strings than with numbers.

The structure of switch/case switch variable case option1 code to be executed if variable is exactly equal to option 1 case option2 code to be executed if variable is exactly equal to option 2 … case option_n code to be executed if variable is exactly equal to option n otherwise code to be executed if variable is not equal to any of the options end

Suppose you want to determine what the airfare is to one of three cities Airfare Example

Menu The menu function is often used in conjunction with a switch/case structure. This function causes a menu box to appear on the screen with a series of buttons defined by the programmer. Because the input is controlled by a menu box, the user can’t accidentally enter a bad choice This means you don’t need the otherwise portion of the switch/case structure

Menu Note that the otherwise portion of the switch/case structure wasn’t used

When you run this code a menu box appears Instead of entering your choice from the command window, you select one of the buttons from the menu