Switch-Case Statement. What is a switch-case?  The switch-case statement is really nothing more than a glorified.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Selection The Switch Statement 2/16/11. grade = 'P'; switch (grade){ case 'A': cout
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Multi-alternative Selection Both the if clause and the else clause of an if...else statement can contain any kind of statement, including another selection.
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
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.
Fundamental Programming Fundamental Programming More on Selection.
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.
IF-ELSE IF-ELSE STATEMENT SWITCH-CASE STATEMENT Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
CONTROL STRUCTURES (MULTI-WAY SELECTION). MULTI-WAY SELECTION  EXTENDED IF-ELSE  Used to select exactly one task out of multiple tasks (or possibly.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
COSC175-Selection1 Decisions Given hours worked and pay rate, calculate total pay What if you work overtime? How do you indicate if your work overtime?
CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.
Previously Repetition Structures While, Do-While, For.
1 Chapter 9 Additional Control Structures Dale/Weems.
Switch Statement Is a selection control structure for multi-way branching. SYNTAX switch ( IntegralExpression ) { case Constant1 : Statement(s); // optional.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Review the following: if-else One branch if Conditional operators Logical operators Switch statement Conditional expression operator Nested ifs if –else.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Choices and Decisions if statement if-else statement Relational.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Selection Structures: if and switch Statements. 2 Selection Statements –In this chapter we study statements that allow alternatives to straight sequential.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Current Assignments Homework 2 is available and is due tomorrow (June 19th). Boolean expressions, if statements and the while loop. Project 1 due in 5.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
LESSON 4 Decision Control Structure. Decision Control Structures 1. if statement 2. if-else statement 3. If-else-if statement 4. nested if statement 5.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
Program Flow Control Addis Ababa Institute of Technology Yared Semu April 2012.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Selection (also known as Branching) Jumail Bin Taliba by
Chapter 4: Control Structures I
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 4: Making Decisions.
A mechanism for deciding whether an action should be taken
Decisions Given hours worked and pay rate, calculate total pay
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Decisions Given hours worked and pay rate, calculate total pay
Chapter 4: Control Structures I
Selection CSCE 121 J. Michael Moore.
Control Structures: Selection Statement
Visual Basic – Decision Statements
Chapter 4: Control Structures I (Selection)
Control Structures Selection or Decision Branching.
Looping III (do … while statement)
Conditionals.
Decisions, decisions, decisions
Control Structures: Selection Statement
Presentation transcript:

Switch-Case Statement

What is a switch-case?  The switch-case statement is really nothing more than a glorified nested if-else package.  C++ has taken on the headache of setting up the overhead of creating the structure of nested if- else for you.

switch-case Syntax switch (control_var) { case constant1: statement1 break; case constant2: statement2 break;... case constantN: statementN break; default: default_statement }

switch-case Syntax switch (control_var) { case constant1: statement1 break; case constant2: statement2 break;... case constantN: statementN break; default: default_statement }

switch-case Syntax switch (control_var) { case constant1: statement1 break; case constant2: statement2 break;... case constantN: statementN break; default: default_statement }

switch-case Syntax switch (control_var) { case constant1: statement1 break; case constant2: statement2 break;... case constantN: statementN break; default: default_statement }

switch-case Syntax switch (control_var) { case constant1: statement1 break; case constant2: statement2 break;... case constantN: statementN break; default: default_statement }

switch-case Syntax switch (control_var) { case constant1: statement1 break; case constant2: statement2 break;... case constantN: statementN break; default: default_statement }

switch-case Syntax switch (control_var) { case constant1: statement1 break; case constant2: statement2 break;... case constantN: statementN break; default: default_statement }

short choice; cout > choice; switch (choice) { case 1: cout << “Oink”; break; case 2: cout << “Bark”; cout << “Ruffruff”; break; case 3: cout << “Moooo” << endl; break; }

short choice; cout > choice; switch (choice) { case 1: cout << “Oink”; break; case 2: cout << “Bark”; cout << “Ruffruff”; break; case 3: cout << “Moooo” << endl; break; } user inputs: 2

short choice; cout > choice; switch (choice) { case 1: cout << “Oink”; break; case 2: cout << “Bark”; cout << “Ruffruff”; break; case 3: cout << “Moooo” << endl; break; }

short choice; cout > choice; switch (choice) { case 1: cout << “Oink”; break; case 2: cout << “Bark”; cout << “Ruffruff”; break; case 3: cout << “Moooo” << endl; break; }

short choice; cout > choice; switch (choice) { case 1: cout << “Oink”; break; case 2: cout << “Bark”; cout << “Ruffruff”; break; case 3: cout << “Moooo” << endl; break; }

short choice; cout > choice; switch (choice) { case 1: cout << “Oink”; break; case 2: cout << “Bark”; cout << “Ruffruff”; break; case 3: cout << “Moooo” << endl; break; }

short choice; cout > choice; switch (choice) { case 1: cout << “Oink”; break; case 2: cout << “Bark”; cout << “Ruffruff”; break; case 3: cout << “Moooo” << endl; break; }

short choice; cout > choice; switch (choice) { case 1: cout << “Oink”; break; case 2: cout << “Bark”; cout << “Ruffruff”; break; case 3: cout << “Moooo” << endl; break; }

short choice; cout > choice; switch (choice) { case 1: cout << “Oink”; break; case 2: cout << “Bark”; cout << “Ruffruff”; break; case 3: cout << “Moooo” << endl; break; }

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit );

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit );

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit );

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit ); user inputs: c

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit );

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit );

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit );

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit );

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit );

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit );

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit );

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit );

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit );

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit );

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit );

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit );

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit ); user inputs: 4

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit );

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit );

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit );

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit );

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit );

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit );

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit );

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit );

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit );

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit );

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit );

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit );

bool quit = false; char choice; do { cout > choice; switch (choice) { case ‘1’: case ‘p’: cout << “Oink”; break; case ‘2’: case ‘d’: cout << “Bark”; break; case ‘3’: case ‘c’: cout << “Moooo” << endl; break; case ‘4’: case ‘q’: quit = true; break; default: cout << “ERROR: Invalid input...” << endl; } } while ( !quit );

long student_number; short student_number_index; cout > student_number; student_number_index = student_number / 10000; switch (student_number_index) { case 12: // code to handle transfers break; case 13: // code to handle freshmen break; case 14: // code to handle sophomores break; case 35: // code to handle grads default: // Error condition }

End of Session