Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 10, 2009.
COMP 14 Introduction to Programming Miguel A. Otaduy May 19, 2004.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
(c) 2003 E.S.Boese1 Conditionals Chapter 10 - Student.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
ECE122 L8: More Conditional Statements February 7, 2007 ECE 122 Engineering Problem Solving with Java Lecture 8 More Conditional Statements.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 1, 2005.
Logical Operators and Conditional statements
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Switch Statements Comparing Exact Values. 2 The Switch Statement The switch statement provides another way to decide which statement to execute next The.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
CP104 Introduction to Programming Selection Structures_3 Lecture 11 __ 1 The Switch Statement The switch statement provides another means to select one.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
Chapter 5: Conditionals and loops. 2 Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing.
1 Data Comparisons and Switch Data Comparisons Switch Reading for this class: L&L 5.3,
© 2004 Pearson Addison-Wesley. All rights reserved February 17, 2006 The ‘while’ Statement ComS 207: Programming I (in Java) Iowa State University, SPRING.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Previously Repetition Structures While, Do-While, For.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
1 Chapter 3 Selections. 2 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
SE-1020 Dr. Mark L. Hornick 1 The switch statement int gradeLevel = kbd.nextInt(); // can be byte or short also, but not long switch( gradeLevel ) { //
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Introduction to Java Java Translation Program Structure
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/29 The switch Statement The switch statement provides another way.
Topics Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement The for loop Nested Loops.
Chapter 5 Conditionals and Loops 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights.
Control Flow. Data Conversion Promotion happens automatically when operators in expressions convert their operands For example, if sum is a float and.
Decisions. Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional.
Control statements Mostafa Abdallah
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
Loops Copyright © 2012 Pearson Education, Inc.. Conditionals and Loops (Chapter 5) So far, we’ve looked at: –making decisions with if –how to compare.
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.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
1 More about Flow of Control. Topics Debugging Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 The if-else Statement An else clause can be added to an if statement to make an if-else statement.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Switch Statements Comparing Exact Values
LESSON 4 Decision Control Structure
Programming Fundamentals
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
Control Structures.
Chapter 6 More Conditionals and Loops
Selection CSCE 121 J. Michael Moore.
The ‘while’ Statement September 27, 2006
Conditionals.
CprE 185: Intro to Problem Solving (using C)
Midterm Review October 23, 2006 ComS 207: Programming I (in Java)
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Chap 7. Advanced Control Statements in Java
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)
Presentation transcript:

Switch Statements Comparing Exact Values

The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression, then attempts to match the result to one of several possible cases The match must be an exact match. 2 switch ( expression ){ case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case... }

The Switch Statement Each case contains a value and a list of statements The flow of control transfers to statement associated with the first case value that matches 3 switch ( expression ){ case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case... }

Switch Syntax The general syntax of a switch statement is: switch ( expression ){ case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case... } switch and case are reserved words If expression matches value3, control jumps to here

The Switch Statement The break statement can be used as the last statement in each case's statement list A break statement causes control to transfer to the end of the switch statement If a break statement is not used, the flow of control will continue into the next case switch ( expression ){ case value1 : statement-list1 break; case value2 : statement-list2 break; case value3 : statement-list3 break; case... }

Switch Example Example of the switch statement: If '1' is true, then aCount is incremented by 1, then breaks to the bottom and outside of the switch switch (test){ case 1: aCount++; break; case 2: bCount++; break; case 3: cCount++; break; } int test = 0; int aCount = 1; int bCount = 2; int cCount = 3; test = aCount;

Switch: No breaks!!! Another Example: If there is no break statement, then the switch will move down through every case and do all the statements until done with the switch. switch (option){ case 'A': aCount++; case 'B': bCount++; case 'C': cCount++; } switch (option){ case 'A': aCount++; break; case 'B': bCount++; break; case 'C': cCount++; break; }

Switch - Default A switch statement can have an optional default case The default case has no associated value and simply uses the reserved word default If the default case is present, control will transfer to it if no other case value matches If there is no default case, and no other value matches, control falls through to the statement after the switch

The switch Statement Switch with default case: switch (option){ case 'A': aCount++; break; case 'B': bCount++; break; case 'C': cCount++; break; default: otherCount++; break; }

To Switch or Not to Switch The expression of a switch statement must result in an integral type, meaning an integer ( byte, short, int, long ) or a char It cannot be a boolean value or a floating point value ( float or double ) The implicit boolean condition in a switch statement is equality: A == B You cannot perform relational checks with a switch statement

…that is the question! To switch or not to switch….