1 10/16/06CS150 Introduction to Computer Science 1 switch Selection Structure.

Slides:



Advertisements
Similar presentations
Strings.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.11 Assignment Operators 2.12 Increment and Decrement Operators.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
LOOPING do { } while ( ) for (I = 0; I < 5; I++) { } while ( ) { }
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
CS150 Introduction to Computer Science 1
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Control Statements (II) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
 2003 Prentice Hall, Inc. All rights reserved. 1 Algorithms Computing problems –Solved by executing a series of actions in a specific order Algorithm.
Control Structures in C++ while, do/while, for switch, break, continue.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 4.
1 Lecture 4 for loops and switch statements Essentials of Counter-Controlled Repetition Counter-controlled repetition requires  Name of control.
 2003 Prentice Hall, Inc. All rights reserved. 1 Control Structures Outline Assignment Operators Increment and Decrement Operators Essentials of Counter-Controlled.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
EC-111 Algorithms & Computing Lecture #4 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Switch Statement in C++. Syntax switch (selector) { case L1: statements1; break; case L2: statements2; break; … default: statements_n; } Semantics: This.
Multi Way Selection You can choose statement(s) to run from many sets of choices. There are two cases for this: (a)Multi way selection by nested IF structure.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Previously Repetition Structures While, Do-While, For.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Assignment Operators = +=-= *= /=%= Statement Equivalent Statement a = a + 2 ;a += 2 ; a = a - 3 ;a -= 3 ; a = a * 2 ;a *= 2 ; a = a / 4 ; a /= 4 ; a =
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
Chapter 05 (Part III) Control Statements: Part II.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
111/18/2015CS150 Introduction to Computer Science 1 Announcements  I have not graded your exam yet.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
CMSC 104, Version 9/011 The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading Section 4.7,
1 CS161 Introduction to Computer Science Topic #8.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Switch Selection Structure (L14) * General Form of the switch Statement * Details of switch Statement * Flowchart of switch Statement * cin.get * Character.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Part 1 Outline Counter-Controlled Repetition: Example Sentinel-Controlled Repetition:
Switch Statements Comparing Exact Values
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall Chapter 5 - Control Statements: Part 2 Outline 5.3 for Repetition.
Chapter 2.1 Control Structures (Selection)
Programming Fundamentals
مبانی برنامه‌سازی با C++ جلسه دوم
- Additional C Statements
Counting Loops.
The switch Statement Topics Multiple Selection switch Statement
Program Control Topics While loop For loop Switch statement
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
switch Selection Structure
2.6 The if/else Selection Structure
do/while Selection Structure
Dale Roberts, Lecturer IUPUI
Reading from and Writing to Files
Chapter 4 - Program Control
C++ Programming Lecture 20 Strings
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Reading from and Writing to Files
Presentation transcript:

1 10/16/06CS150 Introduction to Computer Science 1 switch Selection Structure

2 10/16/06CS150 Introduction to Computer Science 1 Switch Statements (4.13)  Another form of selection statement  Similar to an if statement o but not exactly!  Useful for lots of alternatives if( x == 1 ) { } else if ( x == 2 ) { } else if ( x == 3 ) { } else { }

3 10/16/06CS150 Introduction to Computer Science 1 Example switch (watts) { case 25: life = 2500; break; case 40: case 60: life = 1000; break; case 75: case 100: life = 750; break; default: life = 0; } This switch sets the average life span of a light bulb based on its wattage How is this different from an if statement?

4 10/16/06CS150 Introduction to Computer Science 1 Syntax switch ( integer_expression ) // selector { case constant_integer_expression_1: statements1; break; case constant_integer_expression_2: statements2; break; … case constant_integer_expression_n: statementsn; break; default: statements; }

5 10/16/06CS150 Introduction to Computer Science 1 Important!  Selector must be o A variable of any of the integer data types (including char) (what are the integer data types?) o An expression whose value is any of the integer data types  Each possible value is a separate case  break stops statements for case, otherwise continue with statements for next case

6 10/16/06CS150 Introduction to Computer Science 1 Example – Musical Notes char musical_note; cin >> muscial_note; switch (musical_note) { case ‘c’: cout << “do” << endl; break; case ‘d’: cout << “re” << endl; break; case ‘e’: cout << “mi” << endl; break; case ‘f’: cout << “fa” << endl; break; case ‘g’: cout << “so” << endl; break; case ‘a’: cout << “la” << endl; break; case ‘b’: cout << “ti” << endl; break; default: cout << “An invalid note was read.”; }

7 10/16/06CS150 Introduction to Computer Science 1 Example switch (color) { case ‘R’: case ‘r’: cout << “red” << endl; case ‘B’: case ‘b’: cout << “blue” << endl; case ‘Y’: case ‘y’: cout << “yellow” << endl; }  What happens when color is ‘r’? ‘B’? ‘Y’? ‘Z’?

8 10/16/06CS150 Introduction to Computer Science 1 Example switch (x > y) { case 1: cout << “x greater” << endl; break; case 0: cout << “y greater or equal” << endl; break; }  Write as if statement

9 10/16/06CS150 Introduction to Computer Science 1 Questions  Can you write any switch statement as an if? Why or why not?  Can you write any if statement as a switch? Why or why not?

10 10/16/06CS150 Introduction to Computer Science 1 Example int grade; // one grade int aCount = 0; // number of As int bCount = 0; // number of Bs int cCount = 0; // number of Cs int dCount = 0; // number of Ds int fCount = 0; // number of Fs cout << "Enter the letter grades." << endl << "Enter the EOF character to end input." << endl; // loop until user types end-of-file key sequence while ((grade = cin.get()) != EOF) {

11 10/16/06CS150 Introduction to Computer Science 1 Example switch ( grade ) { case 'A': case 'a': ++aCount; break; case 'B': case 'b': ++bCount; break; case 'C': case 'c': ++cCount; break; case 'D': case 'd': ++dCount; break;

12 10/16/06CS150 Introduction to Computer Science 1 Example case 'F': case 'f': ++fCount; break; case '\n': case '\t': case ' ': break; default: cout << "Incorrect letter grade entered." << " Enter a new grade." << endl; break; }

13 10/16/06CS150 Introduction to Computer Science 1 cin.get()  Used to read one character from the keyboard at a time  Also reads new lines, spaces, and tabs as a character o ‘\n’: new line o ‘\t’: tab o ‘ ‘: space

14 10/16/06CS150 Introduction to Computer Science 1 ASCII Values  All characters have integer values called ASCII values o ‘a’: 97 o ‘b’: 98 o ‘z’:122 o ‘A’: 65 o ‘B’: 66 o ‘Z’: 90

15 10/16/06CS150 Introduction to Computer Science 1 EOF  An integer constant defined in the iostream library  On Unix it is: o  On Windows it is: o

16 10/16/06CS150 Introduction to Computer Science 1 Change to switch if (speed > 35) { fee = 20.00; } else if (speed > 50) { fee = 40.00; } else if (speed > 75) { fee = 60.00; }

17 10/16/06CS150 Introduction to Computer Science 1 Examples  Write an if statement that prints out the level of schooling. (0, none; 1 through 6, elementary; 7 through 8, middle school; 9 through 12, high school; > 12, college) int levelOfSchooling; cin >> levelOfSchooling;  Write a switch statement to do the same