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 ) { //

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Control Structures.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
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.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Animated Version.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
بسم الله الرحمن الرحيم CPCS203: Programming II. Objectives After you have read and studied this chapter, you should be able to Implement a selection control.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Selection Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Chapter Chapter 5 Selection Statements. Objectives Understand selection control statement –if statements –switch statements Write boolean expressions.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Primitive Type boolean.
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.
C Programming Lecture 11.
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.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
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.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Most of these slides are based on “Intro to.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Decisions. Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 5: Objectives After you have read and studied this chapter, you should be able to Implement a selection control using if statements Implement a.
1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Arrays and ArrayList Files Most of these slides.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.
Switch Selection Structure (L14) * General Form of the switch Statement * Details of switch Statement * Flowchart of switch Statement * cin.get * Character.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 5 Selection Statements Animated Version.
1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Arrays and ArrayList Files Most of these slides.
5 Copyright © 2004, Oracle. All rights reserved. Controlling Program Flow.
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.
1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Arrays and ArrayList Most of these slides are.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
Control Structures.
Chapter 5 Control Statements: switch Statement
Chapter 5: Control Statements
Intro to OOP with Java, C. Thomas Wu Selection Statements
Programming Fundamentals
Java Review Most of these slides are based on
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Looping and Repetition
IF if (condition) { Process… }
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
In this class, we will cover:
Java Review Most of these slides are based on
Lecture Notes – Week 2 Lecture-2
Conditionals.
Java Programming Review 1
In this class, we will cover:
Chapter 6 Selection Statements
Controlling Program Flow
Looping and Repetition
Presentation transcript:

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 ) { // examine value of gradeLevel case 1 : System.out.print("Go to the Gymnasium"); break; case 2 : System.out.print("Go to the Science Auditorium"); break; case 3 : System.out.print("Go to Harris Hall Rm A3"); break; case 4 : System.out.print("Go to Bolt Hall Rm 101"); break; } This statement is executed if the gradeLevel is equal to 1. This statement is executed if the gradeLevel is equal to 4.

SE-1010 Dr. Mark L. Hornick 2 The switch statement consists of a switch control expression and one or more case statements switch ( gradeLevel ) { case 1: System.out.print( "Go to the Gymnasium" ); break; case 2: System.out.print( "Go to the Science Auditorium" ); break; case 3: System.out.print( "Go to Harris Hall Rm A3" ); break; case 4: System.out.print( "Go to Bolt Hall Rm 101" ); break; } Case Body The Switch expression is an arithmetic expression that evaluates to an integer value Case Label Colon

SE-1010 Dr. Mark L. Hornick 3 The break statements cause the switch to terminate switch ( N ) { case 1: x = 10; break; case 2: x = 20; break; case 3: x = 30; break; } x = 10; false true N == 1 ? x = 20; x = 30; N == 2 ? N == 3 ? false true break;

SE-1010 Dr. Mark L. Hornick 4 A switch without break statements will not terminate after the case is handled switch ( N ) { case 1: x = 10; case 2: x = 20; case 3: x = 30; } x = 10; false true N == 1 ? x = 20; x = 30; N == 2 ? N == 3 ? false true The flow will first start from the matching case body and then proceed to the subsequent case bodies.

SE-1010 Dr. Mark L. Hornick 5 the default block handles cases not explicitly handled switch (ranking) { case 10: case 9: case 8: System.out.print("Master"); break; case 7: case 6: System.out.print("Journeyman"); break; case 5: case 4: System.out.print("Apprentice"); break; default: System.out.print("Input error: Invalid Data"); break; }

SE-1010 Dr. Mark L. Hornick 6 The switch expression can also be a char datatype switch ( ranking ) { case ‘a’: System.out.print( "Go to the Gymnasium" ); break; case ‘b’: System.out.print( "Go to the Science Auditorium" ); break; case ‘c’: System.out.print( "Go to Harris Hall Rm A3" ); break; case ‘d’: System.out.print( "Go to Bolt Hall Rm 101" ); break; } The variable ranking is a char datatype