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.

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.
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.
General Computer Science for Engineers CISC 106 Lecture 33 Dr. John Cavazos Computer and Information Sciences 05/11/2009.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
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. Announcements  Textbook available from library’s closed reserve.
CSC 107 – Programming For Science. Spacing in a Program  C++ ignores spaces in a program  This also means where newlines placed ignored  #define &
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.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
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.
CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.
CSC 107 – Programming For Science. Announcements.
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.
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.
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.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
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.
CSC 107 – Programming For Science. Announcements.
© 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.
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. Science Means Solving Problems  Physics – How does an atom work?
 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.
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.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
CONDITIONALS CITS1001. Scope of this lecture if statements switch statements Source ppts: Objects First with Java - A Practical Introduction using BlueJ,
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.
Chapter 3 Selection Statements
Chapter 4: Making Decisions.
Programming Fundamentals
Chapter 4: Making Decisions.
Selection CSCE 121 J. Michael Moore.
Flow Control Statements
Control Structures Part 3
Understanding Conditions
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

Today’s Goal  Know how to write selections besides if-else  When each of the options makes sense  When each selection statements should NOT be used  How to covert between them

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  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  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 << “gnidniwer 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.”; }

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.”; }

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.”; }

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.”; }

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

Outline of switch statement 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

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

Outline of switch statement 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

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

Outline of switch statement 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

Execution of switch statement 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 ) {

Execution of switch statement 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 ) {

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

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 (not like if-else s)  Execution will only stop at closing brace or break

Executing switch Statement 1. Evaluates expression 2. Tries to find 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

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;

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;

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 2.1 – 2.9 for Tuesday  How should we write programs?  Is there some way to make debugging less painful?  Why should we not always rush to the computer?  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. 7