Selection Structures: Switch CSC 1401: Introduction to Programming with Java Week 4 – Lecture 1 Wanda M. Kunkle.

Slides:



Advertisements
Similar presentations
The if-else Statements
Advertisements

Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Conditionals with Strings The comparison operators we’ve seen so far (==, !=, >=, > etc.) all apply to numbers ( ints floats doubles etc.) and return either.
Methods in Java CSC 1401: Introduction to Programming with Java Week 7 – Lecture 2 Wanda M. Kunkle.
Converting Between Numbers and Characters CSC 1401: Introduction to Programming with Java Week 4 – Lecture 3 Wanda M. Kunkle.
1 CS 192 Lecture 8 Winter 2003 December 17-18, 2003 Dr. Shafay Shamail.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Boolean Expressions Conditional Statements & Expressions CSC 1401: Introduction to Programming with Java Lecture 4 – Part 2 Wanda M. Kunkle.
1 More About Methods in Java CSC 1401: Introduction to Programming with Java Week 7 – Lecture 3 Wanda M. Kunkle.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.
Repetition Structures: For Loop Constants CSC 1401: Introduction to Programming with Java Week 5 Wanda M. Kunkle.
Repetition Structures: Do-while Loop Random Number Generation CSC 1401: Introduction to Programming with Java Week 6 Wanda M. Kunkle.
Conditionals with Strings The comparison operators we’ve seen so far (==, !=, >=, > etc.) all apply to numbers ( ints floats doubles etc.) and return either.
Chapter 4 Making Decisions
File Input and Output CSC 1401: Introduction to Programming with Java Week 4 – Lecture 2 Wanda M. Kunkle.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 1Winter Quarter Switch Case Structures.
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.
UNIT II Decision Making And Branching Decision Making And Looping
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
J-1 University of Washington Computer Programming I Switch Statement © 2000 UW CSE.
CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
1 CSC103: Introduction to Computer and Programming Lecture No 11.
CPS120: Introduction to Computer Science Decision Making in Programs.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Selection. Flow Chart If selection If/else selection Compound statement Switch.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
Chapter 05 (Part III) Control Statements: Part II.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
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.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
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.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Control Flow 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.
Switch Selection Structure (L14) * General Form of the switch Statement * Details of switch Statement * Flowchart of switch Statement * cin.get * Character.
COMP Loop Statements Yi Hong May 21, 2015.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Java Programming Fifth Edition
Decisions Chapter 4.
Chapter 4: Making Decisions.
Flow of Control.
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Selection CSCE 121 J. Michael Moore.
Control Structures: Selection Statement
Chapter#3 Structured Program Development in C++
Lecture Notes – Week 2 Lecture-2
Control Structures: Selection Statement
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Presentation transcript:

Selection Structures: Switch CSC 1401: Introduction to Programming with Java Week 4 – Lecture 1 Wanda M. Kunkle

Selection Structures A selection structure is used to choose among alternative courses of action. A selection structure is used to choose among alternative courses of action. So far we have looked at the following types: So far we have looked at the following types: –if –if-else –if-else-if The remaining type is: The remaining type is: –switch

Selection Structures The switch selection structure can be used when the course of action to be taken in a program depends upon an integer ( int ) or character ( char ) expression. The switch selection structure can be used when the course of action to be taken in a program depends upon an integer ( int ) or character ( char ) expression.

Selection Structures switch switch –Usage: Used in situations where there are multiple alternatives (generally more than two choices) Used in situations where there are multiple alternatives (generally more than two choices) The statements executed depend on which case label the controlling expression matches The statements executed depend on which case label the controlling expression matches –Format: switch (Expression) {// Braces must enclose the case Item list case Item1: Statement(s) break Statement case Item2: Statement(s) break Statement case Item3:... Optional default: Statement(s) } // end switch

Selection Structures switch switch –Example: char letter = 'i'; switch (letter) {// Classify a letter as a vowel or a consonant case ‘a’: out.writeln("Vowel"); break; case ‘e’: out.writeln("Vowel"); break; case ‘i’: out.writeln("Vowel"); break; case ‘o’: out.writeln("Vowel"); break; case ‘u’: out.writeln("Vowel"); break; default: out.writeln("Consonant"); } // end switch

Selection Structures Let’s look at several different Java programs that classify letters (characters) as vowels or consonants: Let’s look at several different Java programs that classify letters (characters) as vowels or consonants: –Uses if-else-if VowelOrConsonant1.java VowelOrConsonant1.java VowelOrConsonant1.java –Use switch VowelOrConsonant2.java VowelOrConsonant2.java VowelOrConsonant2.java VowelOrConsonant3.java VowelOrConsonant3.java VowelOrConsonant3.java VowelOrConsonant4.java VowelOrConsonant4.java VowelOrConsonant4.java

Selection Structures Now let’s look at a Java program that associates integers with days of the week: Now let’s look at a Java program that associates integers with days of the week: –Uses switch DaysOfWeek.java DaysOfWeek.java DaysOfWeek.java

Pitfalls of the “switch” Statement Let’s look at the sample programs to consider possible pitfalls … Let’s look at the sample programs to consider possible pitfalls …

Topic for Wednesday’s Class File Input and Output (Chapter 6) File Input and Output (Chapter 6)