PHY 107 – Programming For Science. Today’s Goal  Know how to write decisions besides if-else select  Why we use select or if - else and how to choose.

Slides:



Advertisements
Similar presentations
CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Advertisements

1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
CSC Programming for Science Lecture 11: Switch Statements & ?: Operator.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
CSC 107 – Programming For Science. Announcements  Tutors available MTWR in WTC206/WTC208  Special lab (with Macs) & not in the Tutoring Center  Can.
CSC 107 – Programming For Science. Spacing in a Program  C++ ignores spaces in a program  This also means where newlines placed ignored  #define &
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
UniMAP Sem II-09/10EKT120: Computer Programming1 Week 3 – Selection Structures.
1 CSC103: Introduction to Computer and Programming Lecture No 11.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
CSC 107 – Programming For Science. The Week’s Goal.
 The if statement and the switch statement are types of conditional/decision controls that allow your program.  Java also provides three different looping.
CSC 107 – Programming For Science. Announcement Today’s Goal  Know how to write selections besides if-else  Why we use select or if - else and how.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
Copyright 2003 Scott/Jones Publishing Making Decisions.
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.
Week 8: Decisions Bryan Burlingame 21 October 2015.
CSC 107 – Programming For Science. Announcements.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Chapter Making Decisions 4. Relational Operators 4.1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
CSC Programming for Science Lecture 10: Boolean Expressions & More If ­ Else Statements.
1 CS161 Introduction to Computer Science Topic #8.
CSC 107 – Programming For Science. Today’s Goal  Know how to use and write for loops  Explain why to use for, while, & do-while loops  Convert between.
CSC 107 – Programming For Science. Today’s Goal  Know how to write selections besides if-else  When each of the options makes sense  When each selection.
Week 4 Program Control Structure
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
PHY 107 – Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;
CPS120: Introduction to Computer Science Decision Making in Programs.
CSC 107 – Programming For Science. Final Exams Dec. 16, 8AM – 10AM in OM221  Exam for CSC107: Dec. 16, 8AM – 10AM in OM221  Will be done using paper.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
Week 3 - Friday.  What did we talk about last time?  Preprocessor directives  Other C features  sizeof, const  ASCII table  printf() format strings.
CONDITIONALS CITS1001. Scope of this lecture if statements switch statements Source ppts: Objects First with Java - A Practical Introduction using BlueJ,
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
 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.
Week 3 - Friday CS222.
Chapter 4: Making Decisions.
Flow of Control.
Week 3 – Selection Structures
Chapter 4: Making Decisions.
Conditionals & Boolean Expressions
Chapter 4: Making Decisions.
Selection CSCE 121 J. Michael Moore.
Flow Control Statements
Week 3 – Program Control Structure
CSS 161: Fundamentals of Computing
Presentation transcript:

PHY 107 – Programming For Science

Today’s Goal  Know how to write decisions besides if-else select  Why we use select or if - else and how to choose  Understand times when select statements illegal  How to convert between the two types of statements

Press “*” For Horrible Music  Writing software for voic system  Which of 12 buttons hit determine action to take  For much of time, many keys have same effect ifelse ifelse  Long, ugly if - else if - else statement results  Now imagine choosing from 256 options  Required for web browsers & other systems  Takes forever to write & code impossible to read

Press “*” For Horrible Music  Writing software for voic system  Which of 12 buttons hit determine action to take  For much of time, many keys have same effect ifelse ifelse  Long, ugly if - else if - else statement results  Now imagine choosing from 256 options  Required for web browsers & other systems  Takes forever to write & code impossible to read

Long if-else statement if (keyHit == 1) { printf(“Please say message to record”); } else if (keyHit == 2) { printf(“Skipping to next message”); } else if (keyHit == 3) { printf(“Playing first new message”); } else if (keyHit == 4) { printf(“Message is saved for 30 days.”); } else if (keyHit == 6) { printf(“Message deleted”); } else if (keyHit == 7) { printf(“dniwer egasseM”); } else if ((keyHit == 5) || (keyHit == 8)) { printf(“I’m sorry Dave, I can’t do that.”); } else { printf(“Fooled you! Can’t talk to a person.”); }

Long if-else statement if (keyHit == 1) { printf(“Please say message to record”); } else if (keyHit == 2) { printf(“Skipping to next message”); } else if (keyHit == 3) { printf(“Playing first new message”); } else if (keyHit == 4) { printf(“Message is saved for 30 days.”); } else if (keyHit == 6) { printf(“Message deleted”); } else if (keyHit == 7) { printf(“dniwer egasseM”); } else if ((keyHit == 5) || (keyHit == 8)) { printf(“I’m sorry Dave, I can’t do that.”); } else { printf(“Fooled you! Can’t talk to a person.”); }

Better idea: if (keyHit == 1) { printf(“Please say message to record”); } else if (keyHit == 2) { printf(“Skipping to next message”); } else if (keyHit == 3) { printf(“Playing first new message”); } else if (keyHit == 4) { printf(“Message is saved for 30 days.”); } else if (keyHit == 6) { printf(“Message deleted”); } else if (keyHit == 7) { printf(“dniwer egasseM”); } else if ((keyHit == 5) || (keyHit == 8)) { printf(“I’m sorry Dave, I can’t do that.”); } else { printf(“Fooled you! Can’t talk to a person.”); }

switch Better idea: switch Statement switch (keyHit) { case 1: printf(“Please say message to record”; break; case 2: printf(“Skipping to next message”; break; case 3: printf(“Playing first new message”; break; case 4: printf(“Message will be saved for 30 days.”; break; case 6: printf(“Message deleted”; break; case 7: printf(“gnidniwer egasseM”; break; case 5: case 8: printf(“I’m sorry Dave, I can’t do that.”; break; default: printf(“The hit squad will find you soon.”; }

switch Better idea: switch Statement switch (keyHit) { case 1: printf(“Please say message to record”); break; case 2: printf(“Skipping to next message”); break; case 3: printf(“Playing first new message”); break; case 4: printf(“Message will be saved for 30 days.”); break; case 6: printf(“Message deleted”); break; case 7: printf(“dniwer egasseM”); break; case 5: case 8: printf(“I’m sorry Dave, I can’t do that.”); break; default: printf(“Fooled you! Can’t talk to a person.”); }

switch Better idea: switch Statement switch (keyHit) { case 1: printf(“Please say message to record”); break; case 2: printf(“Skipping to next message”); break; case 3: printf(“Playing first new message”); break; case 4: printf(“Message will be saved for 30 days.”); break; case 6: printf(“Message deleted”); break; case 7: printf(“dniwer egasseM”); break; case 5: case 8: printf(“I’m sorry Dave, I can’t do that.”); break; default: printf(“Fooled you! Can’t talk to a person.”); }

switch Outline of switch statement switch { case break case case break default switch (expression) { case label 1 : statement; statement;... break;... case label n : case label n+1 : statement;... break;... default: statement;...}

switch Outline of switch statement switch { case break case case break default switch (expression) { case label 1 : statement; statement;... break;... case label n : case label n+1 : statement;... break;... default: statement;...} Anything of ordinal (whole number) type: intdouble charfloat longMonkey intdouble charfloat longMonkey

switch Outline of switch statement switch { case break case case break default switch (expression) { case label 1 : statement; statement;... break;... case label n : case label n+1 : statement;... break;... default: statement;...} Anything of ordinal (whole number) type: intdouble charfloat longMonkey intdouble charfloat longMonkey

switch Outline of switch statement switch { case break case case break default switch (expression) { case label 1 : statement; statement;... break;... case label n : case label n+1 : statement;... break;... default: statement;...} Anything of ordinal (whole number) type: intdouble charfloat longMonkey intdouble charfloat longMonkey

switch Outline of switch statement switch { case break case case break default switch (expression) { case label 1 : statement; statement;... break;... case label n : case label n+1 : statement;... break;... default: statement;...} Labels must be constants or literal

switch Outline of switch statement switch { case break case case break default switch (expression) { case label 1 : statement; statement;... break;... case label n : case label n+1 : statement;... break;... default: statement;...} This is legal; execution only stops at break or ending brace

switch Outline of switch statement switch { case break case switch (expression) { case label 1 : statement; statement;... break;... case label n : case break default statement; statement; case label n+1 : statement;... break;... default: statement;...}

switch Outline of switch statement switch { case break case case break default switch (expression) { case label 1 : statement; statement;... break;... case label n : case label n+1 : statement;... break;... default: statement;...} Optional, like else must be at end & matches anything left

switch Outline of switch statement switch { case break case case break default switch (expression) { case label 1 : statement; statement;... break;... case label n : case label n+1 : statement;... break;... default: statement;...} Same as equality test in if : if (expression == label 1 ) {

switch Outline of switch statement switch { case break case case break default switch (expression) { case label 1 : statement; statement;... break;... case label n : case label n+1 : statement;... break;... default: statement;...} Works like OR (||) in if-else if : else if (expression == label n || expression == label n+1 ) {

switch Outline of switch statement switch { case break case switch (expression) { case label 1 : statement; statement;... break;... case label n : case break default statement; statement; case label n+1 : statement;... break;... default: statement;...} Fall-thru unique to switch ; Cannot overlap in if-else if

switch switch Statement  Depends on a value to determine what is run  Easier to read than if - else if  Expression can be anything you want  bool, char, short, int, or long result required  Where execution starts marked by the labels  Literals or constants only usable for the labels  Labels in any order: starts at first equal to value  Doesn’t stop at next label (unlike if - else )  Continues executing to closing brace or break  No reevaluation of expression in switch statement

switch switch Statement  Depends on a value to determine what is run  Easier to read than if - else if  Expression can be anything you want  bool, char, short, int, or long result required  Where execution starts marked by the labels  Literals or constants only usable for the labels  Labels in any order: starts at first equal to value  Doesn’t stop at next label  Continues executing to closing brace or break  Do not reevaluate expression  Do not reevaluate expression in switch

switch Executing switch Statement 1. Evaluates expression 2. Finds first matching case or default  If no default no match, will skip past switch 3. Execution starts at 1 st matching label  Execution will continue until break found  Continues into next case if break is not hit 4. Restarts running after switch once break hit  Legal to reach end of switch without a break  Continues running code after switch

switch Tracing switch Statement float temp = -40; // Note: -40 Celsius == -40 Fahrenheit float convert; char c = ‘c’; switch (toupper(c)) { case ‘K’: temp = temp – ; case ‘C’: convert = ((temp / 5) * 9) + 32; break; case ‘F’: convert = ((temp - 32) / 9) * 5; break; default: printf(“Amoronsayswhat?\n”); } printf(“Converted temp: %lf\n”, convert);

switch Tracing switch Statement float temp = -40; // Note: -40 Celsius == -40 Fahrenheit float convert; char c = ‘F’; switch (toupper(c)) { case ‘K’: temp = temp – ; case ‘C’: convert = ((temp / 5) * 9) + 32; break; case ‘F’: convert = ((temp - 32) / 9) * 5; break; default: printf(“Amoronsayswhat?\n”); } printf(“Converted temp: %lf\n”, convert);

Steel Cage Match if - else switch  Actions cannot overlap  Expression per if, else if  Any boolean expressions  else gets remaining cases only when expression(s) are true  Use for actions needed only when expression(s) are true  Overlap by omitting break;  Evaluates single expression  Only 1 equality test  default gets all other cases when expression has specific value actions should overlap  Use for actions needed when expression has specific value –or – actions should overlap

Your Turn  Get in groups & work on following activity

For Next Lecture  Week #4 weekly assignment available on D2L  If problem takes more than 10 minutes, TALK TO ME!  Programming Project #1 available on D2L also  One of term’s 3 large assignments – due Oct. 12  Midterm #1 in class on Friday  Wednesday’s lecture give you chance to review