Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
True or false A variable of type char can hold the value 301. ( F )
Computer Science 1620 Loops.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
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.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 1Winter Quarter Switch Case Structures.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Switch Statements. Switch Statement Often you want to do a series of tests –if i==0 … else if i==1 …. else if i==2 … else if i==3 …. C++ provides the.
Switch Statements Comparing Exact Values. 2 The Switch Statement The switch statement provides another way to decide which statement to execute next The.
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
CP104 Introduction to Programming Selection Structures_3 Lecture 11 __ 1 The Switch Statement The switch statement provides another means to select one.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
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.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
1 CSC103: Introduction to Computer and Programming Lecture No 11.
Selection Structures (if & switch statements) (CS1123)
Chapter 4 Selection Structures: Making Decisions.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
Fundamental Programming: Fundamental Programming Introduction to C++
© 2000 Scott S Albert Selection Structures Structured Programming 256 Chapter 4.
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.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
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.
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.
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Objective: Students will be able to: Declare and use variables Input integers.
Control Structures RepetitionorIterationorLooping Part I.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
Problems Reads a letter grade from user and displays the corresponding range for that letter grade (A  , B  80-89, C  70-79, D  60-69, otherwise.
1 CS161 Introduction to Computer Science Topic #8.
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.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
LESSON 2 Basic of C++.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Switch Statements Comparing Exact Values
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Chapter 3 Selection Statements
Chapter 3 Control Statements
LESSON 2 Basic of C++.
Chapter 4 C Program Control Part I
LESSON 4 Decision Control Structure
Variables A piece of memory set aside to store data
Programming Fundamentals
Chapter 4: Making Decisions.
Additional Control Structures
Chapter 4: Control Structures I (Selection)
Control Structures Part 3
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)
CprE 185: Intro to Problem Solving (using C)
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Switch Case Structures
Presentation transcript:

Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply values that can be expressed as an integer, such as the value of a char). The basic format for using switch case is outlined below. The value of the variable given into switch is compared to the value following each of the cases, and when one value matches the value of the variable, the computer continues executing the program from that point.

syntax switch ( ) { case value: // Code to execute if == // value break; case value: // Code to execute if == // value break;... default: // Code to execute if does not equal the value following any of the cases break; }

The condition of a switch statement is a value. The case says that if it has the value of whatever is after that case then do whatever follows the colon. The break is used to break out of the case statements. Break is a keyword that breaks out of the code block, usually surrounded by braces, which it is in. In this case, break prevents the program from falling through and executing the code in all the other case statements. An important thing to note about the switch statement is that the case values may only be constant integral expressions.

Sadly, it isn't legal to use case like this: int a = 10; int b = 10; int c = 20; switch ( a ) { case b: // here should be an actual value, such as 10, not a varible // Code break; case c: // Code break; default: // Code break; }

The default case is optional, but it is wise to include it as it handles any unexpected cases. Switch statements serves as a simple way to write long if statements when the requirements are met. Often it can be used to process input from a user.

#include using namespace std; int main() { int input; float num1, num2; cout<<“Enter two numbers\n”; cin>>num1>>num2; cout<<“Enter 1. To add\n 2. Subtract\n3. multiply\n"; cin>> input; switch ( input ) { case 1: // if user enter 1 then do the following result = num1+nume2; // break; case 2: // if user enter 2 then do the following result = num1- num2; break; case 3: // if user enter 3 then do the following result = num1 * num2; break; default: // if user enter any number other than 1, 2,and 3, do the following cout<<"Error, bad input, quitting\n"; break; } // end of switch return 0; } // end of main

Project to do Write a program that ask user to enter a grade (0- 100) and assign letter grade (use switch structure) A 90 – 100 B 80 – 89 C 70 – 79 D F 0 – 59 Print out the letter grade