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.

Slides:



Advertisements
Similar presentations
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
Advertisements

Conditional Operator (?:) Conditional operator (?:) takes three arguments (ternary) Syntax for using the conditional operator:
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
CSC Programming for Science Lecture 11: Switch Statements & ?: Operator.
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
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.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
1 CSC103: Introduction to Computer and Programming Lecture No 11.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
Semester Review. As we have discussed, Friday we will have in class time for you to work on a program, this program will come with instructions and you.
CONTROLLING PROGRAM FLOW
CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.
CPS120: Introduction to Computer Science Decision Making in Programs.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
1 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statement } while.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
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.
Chapter 05 (Part III) Control Statements: Part II.
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.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
Week 8: Decisions Bryan Burlingame 21 October 2015.
CSC 107 – Programming For Science. Announcements.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 4 – Logical Operators & Branching.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
1 CS161 Introduction to Computer Science Topic #8.
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
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.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
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.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
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.
Week 3 - Friday.  What did we talk about last time?  Preprocessor directives  Other C features  sizeof, const  ASCII table  printf() format strings.
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.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
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.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Chapter 3 Selection Statements
REPETITION CONTROL STRUCTURE
Chapter 4: Making Decisions.
Programming Fundamentals
Chapter 4: Making Decisions.
Selection CSCE 121 J. Michael Moore.
Chapter 7 Conditional Statements
Flow Control Statements
Control Structures Part 3
The switch Statement When we want to compare a variable against several values to see which one it has, we can use the switch statement: switch (status)
Presentation transcript:

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 to choose  Understand times when selection 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) { cout << “Please say message to record”; } else if (keyHit == 2) { cout << “Skipping to next message”; } else if (keyHit == 3) { cout << “Playing first new message”; } else if (keyHit == 4) { cout << “Message is saved for 30 days.”; } else if (keyHit == 6) { cout << “Message deleted”; } else if (keyHit == 7) { cout << “dniwer egasseM “; } else if ((keyHit == 5) || (keyHit == 8)) { cout << “I’m sorry Dave, I can’t do that.”; } else { cout << “The hit squad will find you soon.”; }

Long if-else statement if (keyHit == 1) { cout << “Please say message to record”; } else if (keyHit == 2) { cout << “Skipping to next message”; } else if (keyHit == 3) { cout << “Playing first new message”; } else if (keyHit == 4) { cout << “Message is saved for 30 days.”; } else if (keyHit == 6) { cout << “Message deleted”; } else if (keyHit == 7) { cout << “dniwer egasseM “; } else if ((keyHit == 5) || (keyHit == 8)) { cout << “I’m sorry Dave, I can’t do that.”; } else { cout << “The hit squad will find you soon.”; }

Better idea: if (keyHit == 1) { cout << “Please say message to record”; } else if (keyHit == 2) { cout << “Skipping to next message”; } else if (keyHit == 3) { cout << “Playing first new message”; } else if (keyHit == 4) { cout << “Message is saved for 30 days.”; } else if (keyHit == 6) { cout << “Message deleted”; } else if (keyHit == 7) { cout << “dniwer egasseM “; } else if ((keyHit == 5) || (keyHit == 8)) { cout << “I’m sorry Dave, I can’t do that.”; } else { cout << “The hit squad will find you soon.”; }

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

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

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

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;...} 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 )  Execution continues until closing brace or break  No reevaluation of expression in switch statement

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; float convert; char c; cin >> 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: cout << “Amoronsayswhat?” << endl; } cout <<“Converted temp: ” << convert << endl;

switch Tracing switch Statement float temp = -40; float convert; char c; cin >> 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: cout << “Amoronsayswhat?” << endl; } cout <<“Converted temp: ” << convert << endl;

switch Tracing switch Statement float temp = -40; float convert; char c; cin >> 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: cout << “Amoronsayswhat?” << endl; } cout <<“Converted temp: ” << convert << endl;

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 of 3 & work on following activity

For Next Lecture  Read sections 8.1 – 8.3 for Monday  What if we want to do something repeatedly?  How should we tell the machine to stop?  What is difference between while and do-while ?  Week #4 weekly assignment available on Angel  If problem takes more than 10 minutes, TALK TO ME!  Programming Project #1 available on Angel also  One of term’s 3 large assignments – due Oct. 1