Fall 2004ENGR 111A - 10.11 MatLab – Palm Chapter 4, Part 2 The if and switch structure Class 10.1 Sections: 4.4 and 4.6.

Slides:



Advertisements
Similar presentations
MIS 3200 – Unit 4 Complex Conditional Statements – else if – switch.
Advertisements

Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Programming Environment S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan-Dearborn Introduction to Matlab: Control Flow.
MatLab – Palm Chapter 4, Part 3 For and While Loops
MATLAB Loops and Branching.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
Class 9.1 Chapter 4 Sections: 4.1, 4.2, 4.3
ENGR 111A - Spring MatLab – Palm Chapter 4, Part 4 Review and Debugging Class 12.1: Palm Chapters &
Chapter 4: Control Structures: Selection
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.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
ALGORITHMS AND FLOWCHARTS
How to think through your program [ principles of good program design ] Rachel Denison MATLAB for Cognitive Neuroscience ICN, 13 December 2007.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Conditional Control Flow By Muhammad Ahsan Qadar SACS Programming Fundamentals Workshop 1.
Lecture Set 5 Control Structures Part A - Decisions Structures.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Conditional Execution
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
ENGR-25_Programming-3.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (4): Control Flow (Chapter 2)
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical operators: and, or, and not ❏ To understand how a C program.
Algorithm Design.
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.,
ENG 1181 College of Engineering Engineering Education Innovation Center P. 1 MAT - Conditional Statements Topics Covered: 1. if based conditional statements.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
1 CS1371 Introduction to Computing for Engineers Control Statements 9/4/2003.
Structured Programming II: If Statements By the end of this class you should be able to: implement branching in a program describe and use an “if” statement.
Week 4 Program Control Structure
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
Algorithms and Pseudocode
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
1 Structured Programming EEN170 Programming in MATLAB.
Control Structure  What is control Structure?  Types of Controls  Use the control structure in VBScript.  Example Summery.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
 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.
Chapter 3 Selection Statements
Control Statements in Matlab
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Oracle11g: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
Selection—Making Decisions
Chapter 4 MATLAB Programming
ALGORITHMS AND FLOWCHARTS
More Selections BIS1523 – Lecture 9.
MSIS 655 Advanced Business Applications Programming
ALGORITHMS AND FLOWCHARTS
Introduction to Programming
Class 9.1 Chapter 4 Sections: 4.1, 4.2, 4.3
MatLab – Palm Chapter 4, Part 2 The if and switch structure
MatLab – Palm Chapter 4, Part 2 The if and switch structure
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Selection Statements Chapter 3.
Introduction to Programming
Structural Program Development: If, If-Else
Presentation transcript:

Fall 2004ENGR 111A MatLab – Palm Chapter 4, Part 2 The if and switch structure Class 10.1 Sections: 4.4 and 4.6

Fall 2004ENGR 111A RAT 10.1 Take out a piece of paper, write your name, team #, today’s date and RAT As an INDIVIDUAL, you have 2-minutes to determine the value of y in the MatLab code shown below. x = 10; if x >= 12 y = sqrt(x); else y = x^2; end Pass your answer to the center aisle Answer: y = 100

Fall 2004ENGR 111A Learning Objectives Students should be able to: Use conditional statements to develop logical program flow. if, elseif, and else commands switch structure Develop flow charts in standard notation.

Fall 2004ENGR 111A Conditional Statements The MatLab conditional statements enable us to write programs that make decisions Understanding the IF-THEN-ELSE logic is fundamental to all software development. Make sure that you understand: if statement on p. 201 else statement on p. 202/203 elseif statement on p. 205

Fall 2004ENGR 111A LOGICAL CONTROL PROGRAMMING CONSTUCTS A conditional (Boolean) statement is an expression which tests the validity of a specified condition e.g., z = I ==J z = I > J These are used in selection structures (conditional statements) to control the flow of a program.

Fall 2004ENGR 111A LOGICAL CONTROL PROGRAMMING CONSTRUCTS Syntax of the if statement: if logical expression statements end Proper indentation is MANDATORY or you will receive NO CREDIT! Note you can right click and choose smart indent in the m-file editor. See page 187 for a flow chart

Fall 2004ENGR 111A Flowchart representation of the if statement. Figure 4.1–2 LOGICAL CONTROL PROGRAMMING CONSTRUCTS Conditional statement Sequential Statement(s)

Fall 2004ENGR 111A LOGICAL CONTROL PROGRAMMING CONSTRUCTS MATLAB starts at the beginning of the if sequence It proceeds one condition to the next When it finds a true statement, the appropriate section of the code is executed THE SEQUENCE IS THEN TERMINATED!!! The last section of code is closed using the keyword end

Fall 2004ENGR 111A EXAMPLE x = some given value if x >= 0 y = sqrt (x) end

Fall 2004ENGR 111A EXAMPLE x = 10; y = 20; if x >= 0 & y >= 0 z = sqrt(x) + sqrt(y); w = log(x) – 3*log(y); end

Fall 2004ENGR 111A LOGICAL PROGRAMMING CONTRUCTS Nested “if” statements: if logical expression 1 statement group 1 if logical expression 2 statement group 2 end Note the indentions – an absolute must Nested Statement

Fall 2004ENGR 111A Typical flow Chart for nested if…end Logic

Fall 2004ENGR 111A LOGICAL PROGRAMMING CONTRUCTS THE else STATEMENT: If two mutually exclusive actions can occur as a result of a decision, use the else statement. if logical expression statement group 1 else statement group 2 end See page 204 for a flow chart of a typical if-else structure.

Fall 2004ENGR 111A Flowchart of the else structure. Figure 4.4–2 LOGICAL PROGRAMMING CONTRUCTS Write these words

Fall 2004ENGR 111A In-class Exercise (5 minutes) Suppose y = x 1/2 for x >= 0 and y = e x – 1 for x < 0 Write a program (.m script file) to calculate y assuming that x already has a scalar value. Test your program for x = 3 and x = -2.

Fall 2004ENGR 111A SOLUTION (Script File) % Solution to In-Class Exercise if x >= 0 y = sqrt (x); else y = exp (x) -1; end Did you indent properly?!

Fall 2004ENGR 111A LOGICAL PROGRAMMING CONSTRUCTS The elseif statement: When three actions can occur as a result of a decision, the else and elseif statements are used along with the if statement. Remember: ONLY ONE ACTION WILL ACTUALY OCCUR!!!

Fall 2004ENGR 111A LOGICAL PROGRAMMING CONSTRUCTS if logical expression 1 statement group1 elseif logical expression 2 statement group 2 else statement group 3 end

Fall 2004ENGR 111A if part elseif check else is here Note: else is NOT a conditional statement

Fall 2004ENGR 111A EXAMPLE Given: y = ln x for x > 10 y = x 1/2 for x >= 0 and x <= 10 y = e x – 1 for x < 0 Compute y if x has been assigned a scalar value.

Fall 2004ENGR 111A SOLUTION (Script file) % Solution to example if x > 10 y = log (x) elseif x >= 0 y = sqrt (x) else y = exp (x) -1 end Does the order that I check things matter? YES!

Fall 2004ENGR 111A LOGICAL PROGRAMMING CONSTRUCTS As a TEAM, take three minutes to complete the following exercise. Write the syntax for the if-elseif-else-end construct if there are more than three alternatives.

Fall 2004ENGR 111A SOLUTION if logical expression1 Statements group1 elseif logical expression2 Statements group2 elseif logical expression3 Statements group3 elseif logical expression4 Statements group4 … else Statement if all other cases are false end

Fall 2004ENGR 111A In-class Assignment As an INDIVIDUAL, you have 10 minutes. Write an.m script file that converts a numerical test score to a letter grade. (90 –100) – A (80 – 89) – B (70 – 79) – C (60 – 69) – D Less than 60 – F Test your program for the grades of 95 and 72.

Fall 2004ENGR 111A SOLUTION (Script file) % Program grades.m grade = 72; if grade >=90 letter = 'A' elseif grade >= 80 letter = 'B' elseif grade >= 70 letter = 'C' elseif grade >= 60 letter = 'D' else letter = 'F' end

Fall 2004ENGR 111A The switch Structure THE “switch” STATEMENT: Provides a convenient way to execute conditional code when there are many cases to choose from. This construct can replace series of if- else-end statements

Fall 2004ENGR 111A LOGICAL PROGRAMMING CONSTRUCTS SYNTAX: switch expression (scalar or string) case value1 statement group 1 case value2 statement group 2 … otherwise statement group n end

Fall 2004ENGR 111A EXAMPLE for “switch” Suppose you are given a variable angle in degrees to represent the following directions: Northeast, Southeast, Southwest, and Northwest. Use the switch statement to display the desired direction given the angle.

Fall 2004ENGR 111A SOLUTION switch angle case 45 disp('Northeast') case 135 disp('Southeast') case 225 disp('Southwest') case 315 disp('Northwest') otherwise disp('Direction Unknown') end Decision variable name Value to test Default case

Fall 2004ENGR 111A EXAMPLE #2 for “switch” You input a numerical value of a quantity in one set of units (e.g., centimeters) and you desire an output in another set of units (e.g., inches, feet, or meters, etc…). Write a program using the switch-case construction that transforms a length in centimeters, inches, feet, meters, etc… to length in centimeters

Fall 2004ENGR 111A SOLUTION switch units case {'inch','in'} % ‘units’ contains type of y = x*2.54; % input, output is in cm case {'feet','ft'} y = x*2.54*12; case {'meter','m'} y = x*100; case {'centimeter','cm'} y = x; case {'millimeter','mm'} y = x/10; otherwise disp(['Unknown Units: ' units]) y = NaN; end

Fall 2004ENGR 111A Assignment 10.1 Individual assignment. Due: Nov. 9, 2004 Palm’s MatLab: Chaper 4; #16, 19a, and 35. Read Section 4.5 in the Palm MATLAB book