The switch statement Week 5. The switch statement Java Method Coding CONCEPTS COVERED THIS WEEK.

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

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.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Lecture 4: Booleans and if-else Statements Yoni Fridman 7/3/01 7/3/01.
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.
Multi-alternative Selection Both the if clause and the else clause of an if...else statement can contain any kind of statement, including another selection.
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
C++ for Engineers and Scientists Third Edition
Decision Making George Mason University. Today’s topics 2 Review of Chapter 2: Decision Making Go over exercises Decision making in Python.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
Nested conditional statements. A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement.
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
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Flow of Control Part 1: Selection
Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.
Branches and Program Design
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
1 2. Program Construction in Java. 2.4 Selection (decisions)
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CS 106 Introduction to Computer Science I 09 / 26 / 2007 Instructor: Michael Eckmann.
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.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
Control Flow Statements
CSI 3125, Preliminaries, page 1 Control Statements.
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I.
Week 3 - Friday.  What did we talk about last time?  Preprocessor directives  Other C features  sizeof, const  ASCII table  printf() format strings.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
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.
 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.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Lecture 3 Selection Statements
Java Programming Fifth Edition
Upsorn Praphamontripong CS 1110 Introduction to Programming Fall 2016
JavaScript: Control Statements.
Control Structures.
More Selections BIS1523 – Lecture 9.
IF if (condition) { Process… }
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
محاضرة 1: مقدمة للمسـاق و مراجعـة للأساسيـات
Selection CSCE 121 J. Michael Moore.
CSC530 Data Structure - Decision
Professor Jodi Neely-Ritz CGS3460
Chapter#3 Structured Program Development in C++
Statement-Level Control Structures
CS 1111 Introduction to Programming Spring 2019
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Conditionals.
Program Flow.
Presentation transcript:

The switch statement Week 5

The switch statement Java Method Coding CONCEPTS COVERED THIS WEEK

Java Method Coding NESTED SELECTION: if … else Note that else links with the preceding nearest if statement, from the deepest level of nesting outwards, e.g. if ( condition1 ) if ( condition2 ) statement1; else statement2; else statement3; Q1. If condition1 is true, and condition2 is false, which statement will be executed? Q2. If condition1 is false, and condition2 is true, which statement will be executed?

Nested if…else Statements // reset product's price based on the new discount code if (discountCode == 'A') { price = cost * 2; // 100% profit } else { if (discountCode == 'B') { price = cost * 1.8; // 80% profit } else { if (discountCode == 'C') { price = cost * 1.6; // 60% profit } else { if (discountCode == 'D') { price = cost * 1.4; // 40% profit } else { if (discountCode == 'E') { price = cost * 1.2; // 20% profit } else { price = cost; // no profit }

Java Method Coding SWITCH // reset the product's price based on the new discount code switch (discountCode) { case 'A' : price = cost * 2; break; // 100% profit case 'B' : price = cost * 1.8; break; // 80% profit case 'C' : price = cost * 1.6; break; // 60% profit case 'D' : price = cost * 1.4; break; // 40% profit case 'E' : price = cost * 1.2; break; // 20% profit default : price = cost; // sold at cost, no profit } default acts like a final else clause, i.e., none of the above

Java Method Coding SWITCH NOTE case ‘A’: price = cost * 2; case ‘B’: price = cost * 1.8; break; This will execute both case ‘arms’, and therefore mistakenly set price to 80% profit when the discount code is ‘A’ as well as when its ‘B’! REMEMBER Don’t forget to terminate each case with a break !

Java Method Coding SWITCH switch (studentGrade) // student degree grade between 1-16 { case 16: case 15: case 14:degreeClass = "1st";break; case 13: case 12: case 11:degreeClass = "2:1";break; case 10: case 9: case 8:degreeClass = "2:2";break; case 7: case 6: case 5:degreeClass = "3rd";break; case 4: degreeClass = "Pass"; break; case 3: case 2: case 1:degreeClass = "Fail";break; } //end switch Sometimes the result of multiple case arms can result in the same action – The above is a better way to code such situations – much easier to read/understand!

Java Method Coding BENEFITS OF SWITCH  CASE labels are easier to use than conditional expressions.  The alternative nature of the action is usually clearer to see.  Alternative options in cases are mutually exclusive. This means that the order of the alternatives doesn’t matter.  Nested if statements will rarely be as easy to read.  CASE statements make the programmer’s intention clear. It isn’t just that it takes less lines of code but that a long sequence of if statements could lead to an unnecessarily complicated segment of code.

Further Reading Appendix D2.2 pages