1. switch statement 2. “Nested” statements Conditionals, part 2 1.

Slides:



Advertisements
Similar presentations
Fundamental of C programming
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Enumerated data type & typedef. Enumerated Data Type An enumeration consists of a set of named integer constants. An enumeration type declaration gives.
ENUMERATED, typedef. ENUMERATED DATA TYPES An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name.
Line Efficiency     Percentage Month Today’s Date
1 CSC103: Introduction to Computer and Programming Lecture No 11.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Engineering Computation with MATLAB Second Edition by David M. Smith.
Compound Statements If you want to do more than one statement if an IF- else case, you can form a block of statements, or compound statement, by enclosing.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
“Operators” (i.e. “symbols”) 1.Overview: Specific Symbols that Represent Specific Actions 2.Arithmetic 3.Relational 4.Boolean 5.Output values 1.
Compound Statements If you want to do more than one statement if an if- else case, you can form a block of statements, or compound statement, by enclosing.
Non Leap YearLeap Year DateDay NumberMod 7Day NumberMod 7 13-Jan Feb Mar Apr May Jun Jul
Conditionals 1.The if Statement 1. switch statement Allows for evaluation of multiple cases of the same variable The switch statement is looking for the.
Matlab tutorial course Lesson 4: Writing your own functions: programming constructs
Java Programming Fifth Edition Chapter 5 Making Decisions.
1 CS161 Introduction to Computer Science Topic #8.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Decision Statements, Short- Circuit Evaluation, Errors.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
13 Arrays CE : Fundamental Programming Techniques June 161.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Java Programming Fifth Edition
Jan 2016 Solar Lunar Data.

Chapter 4: Making Decisions.
JavaScript: Control Statements.
Q1 Jan Feb Mar ENTER TEXT HERE Notes

Project timeline # 3 Step # 3 is about x, y and z # 2
Average Monthly Temperature and Rainfall

2017 Jan Sun Mon Tue Wed Thu Fri Sat

Gantt Chart Enter Year Here Activities Jan Feb Mar Apr May Jun Jul Aug
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Free PPT Diagrams : ALLPPT.com

Logical Operations In Matlab.

Wireless Local Number Portability Timeline - Phase 2
Step 3 Step 2 Step 1 Put your text here Put your text here
Calendar Year 2009 Insure Oklahoma Total & Projected Enrollment
MONTH CYCLE BEGINS CYCLE ENDS DUE TO FINANCE JUL /2/2015
Jan Sun Mon Tue Wed Thu Fri Sat
Electricity Cost and Use – FY 2016 and FY 2017
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Chapter 3: Selection Structures: Making Decisions
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Free PPT Diagrams : ALLPPT.com

Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Project timeline # 3 Step # 3 is about x, y and z # 2
TIMELINE NAME OF PROJECT Today 2016 Jan Feb Mar Apr May Jun
Wireless Local Number Portability Timeline - Phase 2

Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Pilot of revised survey
Presentation transcript:

1. switch statement 2. “Nested” statements Conditionals, part 2 1

2. switch statement Allows for evaluation of multiple cases of the same parameter The switch statement is looking for the parameter to have an exact match to one of the cases. (No a<x && x<=b ) One case specification may have multiple values enclosed in braces( {…}). The default case catches any values of the parameter other than the specified cases. The default case should trap bad parameter values. 2

General Template switch parameter case specification1. case specification n otherwise end 3 There is no limit to the number of cases.

switch case: 1 st Example Let us review one of the previous example… switch month case {1,3,5,7,8,10,12} %31 days months days = 31; case 2 days = 29; %leap year to be coded.. case {4,5,9,11} %30 days months days = 30; otherwise disp(‘Invalid Entry.’) end 4

switch case: 1 st Example Let us review one of the previous example… switch month case {1,3,5,7,8,10,12} %31 days months days = 31; case 2 days = 29; %leap year to be coded.. case {4,5,9,11} %30 days months days = 30; otherwise disp(‘Invalid Entry.’) end 5 MUCH BETTER THAN if month==1 || month== 3 || month== 5 || … month== 7 || month== 8 || month== 10 || month== 12 Big advantage: reduces long OR statements of equality

switch case: 2 nd Example %ask user what he’d like to do menu_choice = input(‘Menu… (blablabla) 1 to 4: ’); %direct code to proper action switch menu_choice case 1 disp(‘You have selected 1.’) case {2,3,4} disp(‘You have selected a number from 2-4’) otherwise disp(‘Invalid Entry’) end 6

if versus. switch As general ideas: 7 ifswitch Combination of or statements that check equality Example: if x==1 || x==2 || x==3 || x==4 Ewwww…√ preferred Inequalities (, >)YesIMPOSSIBLE Conditions that check multiple variables Such as: if x==4 && y==7 √ preferredok Menusok√ preferred Conditions with &&YesIMPOSSIBLE

3. Nested statements “Nesting” means to encase one type of statement inside another. 8 For example: Nested IF statements if length > 0 diameter = input('Diameter, please: '); if diameter > 0 volume = pi * (diameter / 2)^2 * length; end

3. Nested statements “Nesting” means to encase one type of statement inside another. 9 For example: Nested IF statements if length > 0 diameter = input('Diameter, please: '); if diameter > 0 volume = pi * (diameter / 2)^2 * length; end Note that each if statement has its own end marker.

3. Nested statements “Nesting” means to encase one type of statement inside another. 10 For example: Nested IF statements if length > 0 diameter = input('Diameter, please: '); if diameter > 0 volume = pi * (diameter / 2)^2 * length; end Note that each if statement has its own end marker.

3. Nested statements “Nesting” means to encase one type of statement inside another. 11 For example: Nested IF statements if length > 0 diameter = input('Diameter, please: '); if diameter > 0 volume = pi * (diameter / 2)^2 * length; end Note that each if statement has its own end marker.

Nesting, cont. The if, elseif, else, and end keywords each mark the beginning and end of the code under its control. The elseif and else clauses of an if statement are optional. But you must include an end for every if statement – it marks the end of the code being executed conditionally. It can be helpful to comment lines with end keywords so that it is clear which statement is being terminated: end % of switch

Nesting, cont. Nesting can also mean using dissimilar statements – like nesting a switch within an if statement: if date > %greater than January 1st 2010 switch month case {‘Dec’, ‘Jan’, ‘Feb’} disp(‘Winter’); case {‘Mar’, ‘Apr’, ‘May’} disp(‘Spring’); case {‘Jun’, ‘Jul’, ‘Aug’} disp(‘Summer’); case {‘Sep’, ‘Oct’, ‘Nov’} disp(‘Autumn’); end % of switch else disp(‘Date is too early’) end % of if date

Nesting, cont. In fact, you can “nest” any MATLAB code you want inside of if statements, switch statements, or even… Loops!

Wrapping Up switch statements only check EQUALITY So you'll never ever see any relational operators (> =..) If you do: you ARE definitely doing something wrong. TRUST US. They tremendously reduce if statements that check == with || statements. The are very easy to use with numbers AND strings We have not yet presented strings and if statements, since it is harder. Nested if/switch are entirely feasible. Make sure the end keywords are correctly placed. Ctrl+A+I will remind you what MATLAB sees. 15