Selection Control Structure: Switch Case Statement

Slides:



Advertisements
Similar presentations
If/else and switch. Assignments Due – Lab 3 No reading – study for your quiz!
Advertisements

Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
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.
Chapter 6 Control Structures.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Chapter 4 Making Decisions
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 4 Making.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
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
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
Computer Science Department Relational Operators And Decisions.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
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.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
CONTROL STRUCTURES (MULTI-WAY SELECTION). MULTI-WAY SELECTION  EXTENDED IF-ELSE  Used to select exactly one task out of multiple tasks (or possibly.
Chapter 4 Selection Structures: Making Decisions.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
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?
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
Chapter 05 (Part III) Control Statements: Part II.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Copyright 2003 Scott/Jones Publishing Making Decisions.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Chapter Making Decisions 4. Relational Operators 4.1.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
CHAPTER 5 MAKING DECISION Hidayah Elias BFC2042 – Computer Programming.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
1 CS161 Introduction to Computer Science Topic #8.
CPS120: Introduction to Computer Science Decision Making in Programs.
Switch Selection Structure (L14) * General Form of the switch Statement * Details of switch Statement * Flowchart of switch Statement * cin.get * Character.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Quiz 1 Exam 1 Next Monday. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You.
Chapter 3 Selection Statements
Review 1.
More on the Selection Structure
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.
Compound Condition Break , Continue Switch Statements in Java
Decisions Chapter 4.
Chapter 4: Making Decisions.
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
Introduction to Programming
Selection (if-then-else)
The switch statement: an alternative to writing a lot of conditionals
Chapter#3 Structured Program Development in C++
Topics 4.1 Relational Operators 4.2 The if Statement
Chapter 4: Control Structures I (Selection)
Structured Program Development in C++
Fundamental Programming
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)
Control Structures contd… Selection
Selection Control Structure
Programming Fundamental
Presentation transcript:

Selection Control Structure: Switch Case Statement

Introduction Switch case statement do not require the evaluation of a logical expression, compare with if else statement. Give the computer power to select from among many alternatives/options. The reverse words are: switch, case, break, default. Switch structure contain constant expression. Constant expression will be evaluated at first in the structure. It is used to select the action/option specified in the statement that follow the reserved word case. Normally, constant expression is the identifier. In such situation, it is also called as selector. Break is used to immediately exit from the switch structure.

General Form switch(condition){ case constant_expression: { statements; break; } default: single line statement; // no compound statement //if more than single line statement MUST have compound //statement

Example 1 – if…else statement using int int num; cout << “Please select your menu number”; cin >> num; //condition statement (variable) if (num == 1){ //number 1 & 2 known as constant expression cout << “You have select menu number 1”; } else if(num == 2){//number 1 & 2 known as constant expression cout << “You have select menu number 2”; else cout << “Invalid selection”;

Example 1 – Constant Expression using int int num; cout << “Please select your menu number”; cin >> num; //condition statement for switch (variable) switch(num){ case 1:{ //number 1 & 2 known as constant expression cout << “You have select menu number 1”; break; } case 2:{//number 1 & 2 known as constant expression cout << “You have select menu number 2”; default: cout << “Invalid selection”;

Example 2 – if…else statement using char char character; cout << “Please select your menu”; cin >> character; //condition statement (variable) if (character == ‘A’){ //A & B known as constant expression cout << “You have select menu A”; } else if(character == ‘B’){//A & B known as constant expression cout << “You have select menu B”; else cout << “Invalid selection”;

Example 2 – Constant Expression using char char character; cout << “Please select your menu”; cin >> character; //condition statement for switch (variable) switch(character){ case ‘A’:{ //A & B known as constant expression cout << “You have select menu A”; break; } case ‘B’:{//A & B known as constant expression cout << “You have select menu B”; default: cout << “Invalid selection”;

Example 3 – if…else statement using char char character; cout << “Please select your menu”; cin >> character; //condition statement for switch (variable) if(character == ‘A’ || character == ‘a’){ //A & B known as constant expression cout << “You have select menu A”; } else if(character == ‘B’ || character == ‘b’) {//A & B known as constant expression cout << “You have select menu B”; else cout << “Invalid selection”;

Example 3 – Constant Expression using char char character; cout << “Please select your menu”; cin >> character; //condition statement for switch (variable) switch(character){ case ‘A’: case ‘a’:{ //A & B known as constant expression cout << “You have select menu A”; break; } case ‘B’: case ‘b’:{//A & B known as constant expression cout << “You have select menu B”; default: cout << “Invalid selection”;

Advantages & Disadvantages Suitable for menu selection. Proper code arrangement. Do not require the evaluation of logical expression. Disadvantage: Not suitable for comparing string (strcmp). Not suitable for comparing range of numbers.