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)

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Conditional Operator (?:) Conditional operator (?:) takes three arguments (ternary) Syntax for using the conditional operator:
Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.
(c) 2003 E.S.Boese1 Conditionals Chapter 10 - Student.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
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 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.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Chapter 1 Quiz Questions (CGS-3464) Mahendra Kumar
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way 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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
Previously Repetition Structures While, Do-While, For.
Switch Statement Is a selection control structure for multi-way branching. SYNTAX switch ( IntegralExpression ) { case Constant1 : Statement(s); // optional.
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.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
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.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
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.
Chapter Making Decisions 4. Relational Operators 4.1.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
1 CS161 Introduction to Computer Science Topic #8.
Control statements Mostafa Abdallah
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Introduction to Control Statements IT108 George Mason University.
Chapter 3 Selection Statements
Chapter 3 Selections Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Chapter 3 Control Statements
Selections Java.
Chapter 4: Making Decisions.
A mechanism for deciding whether an action should be taken
Decisions Given hours worked and pay rate, calculate total pay
Programming Fundamentals
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Control Structures – Selection
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1.
Other Conditional Methods
Counting Loops.
Flow Control Statements
Control Structures Part 3
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
Lecture Notes – Week 2 Lecture-2
Java Programming Review 1
Engineering Problem Solving with C++ An Object Based Approach
Fundamental Programming
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Presentation transcript:

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) { case 0: // single case goes here break; case 1: // married joint goes here case 2: // married single goes here case 3: // head-of-household goes here default: // invalid status goes here }

§3.15 The switch Statement The flowchart version of the switch statement:

The rules for using switch: The expression must be a char, byte, short, or int (no long, float, double, or String), and it must go in parentheses The values in the case clauses must be of the same type as the switch expression, and must be constants Java and C++ check the case values in order. Once it finds a match, it keeps executing code until it sees either break or the end of the switch statement

The rules for using switch: Be aware of the need for break! switch (ch) // assume ch is 'a' { case 'a': System.out.print('a'); case 'b': System.out.print('b'); case 'c': System.out.print('c'); } This outputs "abc". Without a break to end each case, execution "falls through" to the next case.

The rules for using switch: Be aware of the need for break! switch (ch) // assume ch is 'a' { case 'a': System.out.print('a'); break; case 'b': System.out.print('b'); break; case 'c': System.out.print('c'); break; } This outputs "a"

A Switch Statement switch (ch) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': cout << ch << " is a vowel" << endl; break; default: cout << ch << " is not a vowel" << endl; }

int Left; int Right; char Operator; cout << "Enter simple expression: "; cin >> Left >> Operator >> Right; cout << Left << " " << Operator << " " << Right << " = "; switch (Operator) { case '+' : cout << Left + Right << endl; break; case '-' : cout << Left - Right << endl; break; case '*' : cout << Left * Right << endl; break; case '/' : cout << Left / Right << endl; break; default: cout << "Illegal operation" << endl; }