Switch Selection Structure (L14) * General Form of the switch Statement * Details of switch Statement * Flowchart of switch Statement * cin.get * Character.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Conditional Operator (?:) Conditional operator (?:) takes three arguments (ternary) Syntax for using the conditional operator:
1 10/16/06CS150 Introduction to Computer Science 1 switch Selection Structure.
Do/while Structure (L15) * do/while structure * break Statement * continue Statement * Loop Programming Techniques - Interactive input within a loop -
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
C++ for Engineers and Scientists Third Edition
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
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.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
EC-111 Algorithms & Computing Lecture #4 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Chapter 05 (Part III) Control Statements: Part II.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
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,
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
1 CS161 Introduction to Computer Science Topic #8.
Review 1 Computers and Programming I Dr. Ming Zhang Tel: (757) Fax: (757) Office: Gosnold 217b Subject Overview.
CPS120: Introduction to Computer Science Decision Making in Programs.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
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.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall Chapter 5 - Control Statements: Part 2 Outline 5.3 for Repetition.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
Lecture 3 Selection Statements
Chapter 3 Selection Statements
Java Programming Fifth Edition
Chapter 4 – C Program Control
CNG 140 C Programming (Lecture set 3)
Chapter 3 Control Statements
Chapter 4 C Program Control Part 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.
Flow of Control.
Topic 3, Selection Statements
Chapter 2.1 Control Structures (Selection)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 4: Making Decisions.
Part II The Switch Statement
Introduction to C++ Programming
Chapter 7 Additional Control Structures
Selection Control Structure: Switch Case Statement
Chapter 4: Control Structures I (Selection)
switch Selection Structure
2.6 The if/else Selection Structure
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Structured Program Development in C++
Control Statements Paritosh Srivastava.
Chapter 4 - Program Control
The switch Statement Topics Multiple Selection switch Statement
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Presentation transcript:

switch Selection Structure (L14) * General Form of the switch Statement * Details of switch Statement * Flowchart of switch Statement * cin.get * Character : Char or Integer ? * EOF * Case Study * Exercise/Home Work Dr. Ming Zhang

General Form of a switch Statement switch (expression) { case value_1: // terminated with a colon statement_1; break; // exit from switch; optional case value_2: statement_m; break; default: // last else; optional statement_aa;......} Dr. Ming Zhang

Details of switch Statement (1) * switch The keyword switch identifies the start of the switch. * expression -The expression in parentheses is evaluated and the result of the expression compared to various alternative values contained within the compound statement. -The expression must evaluate to an integer result or a compilation error results. Dr. Ming Zhang

Details of switch Statement (2) * case -The keyword case is used to identify or label individual values that are compared to the value of the switch expression. -The switch expression’s value is compared to each of these case value in order in which these values are listed until a match is found. -When the mach occurs, execution begins with the statement immediately following the match. Dr. Ming Zhang

Details of switch Statement (3) * default -If the value of the expression does not match any case values, however, no statement is executed unless the keyword default is encountered. - The word default is option and operates the same as the last else in an if-else chain. -If the value of the expression does not match any of the case values, program execution begins with the statement following the word default. Dr. Ming Zhang

Details of switch Statement (4) * break -Once an entry point has been located by the switch statement, all further case evaluations are ignored and execution continues through the end of the compound statement unless a break statement is encountered. - This is the reason for the break statement, which identifies the end of a particular case and causes an immediate exit from the switch statement. - If the break statements are omitted, all cases following the matching case value, including the default case, are executed. Dr. Ming Zhang

Flowchart of switch Statement case a case a action(s) break case b case b action(s) break case z case z action(s) break default action(s) Dr. Ming Zhang

Example of switch Statement int num1 = 10, num2 = 10; cin >> opselect ; switch (opselect) { case 1: cout << “The sum:”<< (num1+num2); break; case 2: cout<<“The product”<<(num1*num2); break; case 3: cout<<“The division”<<(num1/num2); break; } Dr. Ming Zhang

cin.get * grade = cin.get The cin.get( ) function reads one character from the keyboard and stores that character in integer variable grade. * Details will be introduced in Chapter 6, “Classes”. Dr. Ming Zhang

Char or Integer * char or integer Characters normally are stored in variables of type char; however, an important feature of C++ is that characters can be stored in any integer data type because they are represented as 1-byte integers in the computer. * cout (‘a’); output: a has the value 97 * ‘a’: integer value of “a”. Dr. Ming Zhang

Assignment Statements * Example of Assignment Statements a=4; b=2; a = b = c = 0; a=4 or 0 ??????? b=2 or 0 ??????? * The assignment operator associated from right to left. The variable b is then assigned the value of the assignment c = 0 (which is also 0). Same reason, a = 0. Dr. Ming Zhang

EOF * EOF (end-of-file) EOF is the symbol whose acronym stands for end-of-file. * EOF normal has the value -1. However, we do not type the value -1 nor do we type the letters EOF. Rather, you type a system- dependent keystroke combination to mean “end-of-file”. * UNIX: -> EOF MS-DOS: -> EOF Dr. Ming Zhang

Case Study - Figure 2.22 (D & D) while ( ( grade = cin.get( ) ) != EOF) { switch (grade) { case ‘A’: case ‘a’: ++aCount; break; } Dr. Ming Zhang

Question 1 - Exercise/Home Work Rewrite the following if-else chain using a switch statement: if (res_type == 1) data( ); else if (res_type == 2) capacity( ); else if (res_type == 3) volume( ); else if (res_type == 4) area( ); else if (res_type == 5) files( ); else retrieve( ); Dr. Ming Zhang

Question 2 - Exercise/Home Work Write a program to use a switch statement to select the arithmetic operation (addition, multiplication, or division) to be performed on two numbers depending on the value of the variable opselect. A menu for your operation message should be created. Dr. Ming Zhang