ME 142 Engineering Computation I Condition Statements.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Selection (decision) control structure Learning objective
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
Conditional Statements Introduction to Computing Science and Programming I.
Slide 1 VB Program Flow Control. Slide 2 Making Decisions v Decision Statement: control the execution of parts of the program based on conditions. v The.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Chapter 4: Control Structures: Selection
C++ for Engineers and Scientists Third Edition
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
CIS162AD - C# Decision Statements 04_decisions.ppt.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
CHAPTER 4: CONDITIONAL STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture Set 5 Control Structures Part A - Decisions Structures.
Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
1 2. Program Construction in Java. 2.4 Selection (decisions)
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Basic Conditions. Challenge: ● Ask the user his/her name ● If it’s “Wally,” jeer him ● Pause video and try on your own.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith.
Controlling Program Flow with Decision Structures.
If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter 4: Decisions and Conditions
Introduction to Decision Structures and Boolean Variables
Chapter 4: Decisions and Conditions
Making Choices with if Statements
Sequence, Selection, Iteration The IF Statement
Decisions Chapter 4.
Chapter 4 MATLAB Programming
Topics The if Statement The if-else Statement Comparing Strings
Programming Fundamentals
Introduction to MATLAB
Microsoft Visual Basic 2005 BASICS
Topics The if Statement The if-else Statement Comparing Strings
IF if (condition) { Process… }
Logical Operations In Matlab.
Selection Statements.
Conditional Logic Presentation Name Course Name
By Hector M Lugo-Cordero September 3, 2008
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 3: Selection Structures: Making Decisions
Types, Truth, and Expressions
REPETITION Why Repetition?
Control Structures.
Presentation transcript:

ME 142 Engineering Computation I Condition Statements

Key Concepts Relational Operators Logical Operators If-Then-Else Statement Select Case Statement GoTo Statement

Relational & Logical Operators

Relational Operators  Relational or comparison operators are used in expressions to perform a test.  They are most commonly used in conjunction with an IF statement.

Relational Operators  Common relational operators include: <less than <=less than or equal to >greater than >=greater than or equal to <>not equal to =equal to

Logical Operators  Logical operators are used in complex expressions to perform a test. Used to check for the validity of multiple conditions being met.  Common logical operators include AND, OR, and NOT:

Logical Operators AND returns true if all parts of the expression are true, otherwise returns false OR returns true if any part of the expression is true, otherwise returns false NOT returns true if expression is false, returns false if expression is true

If-Then-Else Statement

 If statements are used to execute a statement or block of statements when a condition is true.  Logical and relational operators are used to form expressions which are evaluated as either true or false.

If-Then-Else Statement Syntax for the If statement is as follows: If condition Then [statements…] [ElseIf condition Then] [statements…] [Else] [statements…] End If

If-Then-Else Statement  When a condition is true, VBA executes the corresponding block of statements.  Execution of the program is then transferred to the end of the If structure.

If-Then-Else Statement  A one line version of the statement is also available for cases where a single statement is to be executed: If condition Then statement

 Develop a function to give discounts based on the quantity purchased: Less than 25: no discount Greater than/equal to 25:10% discount Greater than/equal to 50:20% discount Greater than/equal to 75:25% discount Example Problem

Function Discount(Qty) If Qty >= 25 And Qty < 50 Then Discount = 0.1 ElseIf Qty >= 50 And Qty < 75 Then Discount = 0.2 ElseIf Qty >= 75 Then Discount = 0.25 Else Discount = 0 End If QuantityDiscount Can you develop an alternative IF statement that will yield the same results? Example Problem Soln

Select Case

Select Case Statement  The Select Case structure is useful for decisions that involve three or more options.  It is best suited to situations where a block of statements is executed on the basis of the value of a single variable.

Select Case Statement Syntax for the Select Case statement is as follows: Select Case condition [Case expressionlist1] [statements…] [Case expressionlist2] [statements…] [Case Else] [statements…] End Select

Select Case Example Function Discount(Qty) Select Case Qty Case Is >= 75 Discount = 0.25 Case 50 To 74 Discount = 0.2 Case 25 To 49 Discount = 0.1 Case Else Discount = 0 End Select End Function

GoTo Statement

 A GoTo statement offers a straightforward means of changing the program flow. GoTo Label  This statement transfers program control to a new statement, identified by a label.  A label is a text string followed by a colon. ExampleLabel:

GoTo Statement  In general avoid the use of GoTo Use it only when you have no other way to perform an action. The only time you really need to use a GoTo statement if for error trapping.

Example Problem  Create a truth table for the following expression: (X Z) and X < 3  Use VBA to evaluate the expression above as either True or False, given X = 1, Y=2, and Z=3 (Hint: try using VBA’s Immediate Window along with the Print command).

Example Problem Soln  Create a truth table for the following expression: (X Z) and X < 3 X<=YY>ZX<3X Z(X Z) and X < 3 TTTTT TTFTF TFTTT TFFTF FTTTT FTFTF FFTFF FFFFF

Example Problem Soln  Use VBA to evaluate the expression above as either True or False, given X = 1, Y=2, and Z=3 (Hint: try using VBA’s Immediate Window along with the Print command).

Review Questions

I-Clicker Question Relational & Logical Operators Given, A=10 and B=8, what does the following expression return: NOT(A>B) A.True B.False C.None of the Above D.Cannot tell from information given

I-Clicker Question If-Then-Else Statement Given, perform=3 and sal=100, what does the following function return: A.0 B.3 C.7 D.100 E.None of the Above Function Bonus(perform, sal) If perform = 1 Then Bonus = sal * 0.1 ElseIf perform = 2 Then Bonus = sal * 0.09 ElseIf perform = 3 Then Bonus = sal * 0.07 Else Bonus = 0 End If End Function

Homework Help ‘n Hints