Part II The Switch Statement

Slides:



Advertisements
Similar presentations
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
2Object-Oriented Program Development Using C++ 3 Relational Expressions Decision-making: comparison of two numerical values Relational expression –Also.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
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)
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
1 Basic control structures Overview l Relational and Logical Operations l Selection structures »if statement »switch statement l Preview:
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
C++ for Engineers and Scientists Third Edition
Visual C++ Programming: Concepts and Projects
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
UNIT II Decision Making And Branching Decision Making And Looping
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Chapter 4: Control Structures SELECTION STATEMENTS.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
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.
Switch Selection Structure (L14) * General Form of the switch Statement * Details of switch Statement * Flowchart of switch Statement * cin.get * Character.
Quiz 1 Exam 1 Next Monday. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
Chapter 4: Control Structures I
Java Programming Fifth Edition
CNG 140 C Programming (Lecture set 3)
Chapter 4: Control Structures 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.
Decisions Chapter 4.
Selection—Making Decisions
Introduction to programming in java
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Chapter 5: Control Structures II
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
JavaScript: Control Statements.
Chapter 5: Control Structures II
Chapter 4 Control Statements: Part I
Chapter 4: Control Structures I
Java I.
CHAPTER 6 GENERAL-PURPOSE METHODS
Chapter 4: Control Structures I (Selection)
Expressions and Assignment
The System.exit() Method
Control Structure Chapter 3.
Chapter8: Statement-Level Control Structures April 9, 2019
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
PROGRAM FLOWCHART Selection Statements.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Controlling Program Flow
Control Structure.
CSS 161: Fundamentals of Computing
Methods Coding in Java Copyright © Curt Hill.
Presentation transcript:

Part II The Switch Statement

The switch Statement Provides alternative to if-else chain For cases that compare value of integer expression to specific value Reserved words: switch case default break

The switch Statement (continued) Syntax: switch (expression) { // start of compound statement case value-1: <------------terminated with a colon statement1; statement2; break; case value-2: <-------------terminated with a colon statementm; statementn; default: <-------------------terminated with a colon statementaa; statementbb; } // end of switch and compound statement

The switch Statement (continued) Once entry point has been located : All further case evaluations are ignored Execution continues through end of compound statement Unless break statement is encountered break statement Causes immediate exit from switch statement

import javax.swing.*; public class SelectDiskMaker { public static void main(String[] args) String s1, outMessage; int code; s1 =JOptionPane.showInputDialog("Enter a number:"); code = Integer.parseInt(s1); switch (code)

import javax.swing.*; public class SelectDiskMaker { public static void main(String[] args) String s1, outMessage; int code; s1 =JOptionPane.showInputDialog("Enter a number:"); code = Integer.parseInt(s1); switch (code) { case 1: outMessage = "3M Corporation"; break; case 2: outMessage = "Maxell Corporation"; case 3: outMessage = "Sony Corporation"; case 4: outMessage = "Verbatim Corporation"; default: outMessage = "An invalid code was entered"; } // end of switch

JOptionPane.showMessageDialog(null, outMessage, "Program 4.6", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } // end of main() } // end of class

Program Design and Development: Introduction to UML Explicit design Should always be undertaken before coding begins Referred to as program modeling Unified Modeling Language (UML) Program modeling language with its own set of rules and notations Not part of Java language

Program Design and Development: Introduction to UML (continued) Must understand and specify: What objects in system are What can happen to objects When it can happen Each item addressed by number of individual and separate views and diagrams

Class and Object Diagrams Class diagrams Used to describe classes and their relationships Object diagrams Describe specific objects and relationships Attribute Characteristic that each object in class must have Attribute type Primitive Or class type

Class and Object Diagrams (continued) Visibility Defines where attribute can be seen Whether attribute can be used in other classes or is restricted to class defining it Operations Transformations that can be applied to attributes Ultimately coded as Java methods

Class and Object Diagrams (continued) Figure 5.9: A class and object representation

Relationships (continued) Figure 5.17: A multilevel aggregation

Relationships (continued) Figure 5.18: A generalization relationship

Common Programming Errors Assuming if-else statement selecting incorrect choice when problem is values being tested Using nested if statements without including braces to clearly indicate desired structure Inadvertently using assignment operator = in place of relational operator == when comparing Boolean data Forgetting to use break statement to close off case within switch statement

Summary Relational expressions if-else statements Referred to as conditions Used to compare operands if-else statements Used to select between two alternative statements Based on value of relational or logical expression Can contain other if-else statements

Summary (continued) Compound statement switch statement Number of individual statements enclosed within brace pair { and } switch statement Multiway selection statement

End of Chapter 5 Be ready to review chapter questions for Thursday Submit topic for presentation work on today’s lab