Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.

Slides:



Advertisements
Similar presentations
CSCI 51 Introduction to Programming Dr. Joshua Stough February 10, 2009.
Advertisements

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.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
INF 523Q Chapter 3: Program Statements (Examples).
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.
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.
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.
Copyright © 2014 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design 8 th Edition John.
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,
Last time We covered: –primitive data types –declaration, initialization, assignment of variables –expressions and operator precedence –data conversions.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Copyright © 2012 Pearson Education, Inc. Lab 8: IF statement …. Conditionals and Loops.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
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:
Layouts, Types, & Switches Copyright © 2012 Pearson Education, Inc.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Control statements Mostafa Abdallah
Loops Copyright © 2012 Pearson Education, Inc.. Conditionals and Loops (Chapter 5) So far, we’ve looked at: –making decisions with if –how to compare.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
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.
Interactive Programs Programs that get input from the user 1 In PowerPoint, click on the speaker icon then click the "Play" button to hear the narration.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
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
COS 312 DAY 5 Tony Gauvin. Ch 1 -2 Agenda Questions? Assignment 2 Posted – Due Feb 22 prior to class – Any issues? Assignment 3 will be posted soon Capstones.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I
Chapter 6 More Conditionals and Loops
Chapter 6 More Conditionals and Loops
Programming Fundamentals
More Conditionals and Loops
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
Introduction to Classes and Methods
Chapter 6 More Conditionals and Loops
Chapter 6 More Conditionals and Loops
Outline Boolean Expressions The if Statement Comparing Data
The ‘while’ Statement September 27, 2006
CprE 185: Intro to Problem Solving (using C)
PROGRAM FLOWCHART Selection Statements.
Midterm Review October 23, 2006 ComS 207: Programming I (in Java)
Comparing Data & the ‘switch’ Statement
Outline Software Development Activities
Comparing Data & the ‘switch’ Statement
Outline Boolean Expressions The if Statement Comparing Data
CprE 185: Intro to Problem Solving (using C)
Presentation transcript:

Chapter 6

else-if & switch Copyright © 2012 Pearson Education, Inc.

Example: else-if Copyright © 2012 Pearson Education, Inc. int grade = 85; char letter = ‘’; if (grade > 89) letter = ‘A’; else if (grade > 79) letter = ‘B’; else if (grade > 69) letter = ‘C’; else if (grade > 59) letter = ‘D’; else letter = ‘F’; int grade = 85; char letter = ‘’; if (grade > 89) letter = ‘A’; else if (grade > 79) letter = ‘B’; else if (grade > 69) letter = ‘C’; else if (grade > 59) letter = ‘D’; else letter = ‘F’;

The switch Statement 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 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 Copyright © 2012 Pearson Education, Inc.

The switch Statement 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 value2, control jumps to here Copyright © 2012 Pearson Education, Inc. A break statement can be used in a switch to jump to the end of the switch statement

The switch Statement switch (option) { case 'A': aCount++; break; case 'B': bCount++; break; case 'C': cCount++; break; } An example of a switch statement: Copyright © 2012 Pearson Education, Inc.

Example: else-if vs switch Copyright © 2012 Pearson Education, Inc. int grade = 85; char letter = ‘’; if (grade > 89) letter = ‘A’; else if (grade > 79) letter = ‘B’; else if (grade > 69) letter = ‘C’; else if (grade > 59) letter = ‘D’; else letter = ‘F’; int grade = 85; char letter = ‘’; if (grade > 89) letter = ‘A’; else if (grade > 79) letter = ‘B’; else if (grade > 69) letter = ‘C’; else if (grade > 59) letter = ‘D’; else letter = ‘F’; int grade = 85; char letter = ‘’; switch (grade) { case 100: case 99: case 98: … case 90: letter = ‘A’; break; case 89: … default: letter = ‘F’; } int grade = 85; char letter = ‘’; switch (grade) { case 100: case 99: case 98: … case 90: letter = ‘A’; break; case 89: … default: letter = ‘F’; } If no other case value matches, the optional default case will be executed (if present)

The switch Statement The type of a switch expression must be integers, characters, or enumerated types Why? == Copyright © 2012 Pearson Education, Inc. As of Java 7, a switch can also be used with strings You cannot use a switch with floating point values The implicit boolean condition in a switch statement is equality You cannot perform relational checks with a switch statement (i.e., > or <)

Copyright © 2012 Pearson Education, Inc. //******************************************************************** // GradeReport.java Author: Lewis/Loftus // // Demonstrates the use of a switch statement. //******************************************************************** import java.util.Scanner; public class GradeReport { // // Reads a grade from the user and prints comments accordingly. // public static void main (String[] args) { int grade, category; Scanner scan = new Scanner (System.in); System.out.print ("Enter a numeric grade (0 to 100): "); grade = scan.nextInt(); category = grade / 10; System.out.print ("That grade is "); continue

Copyright © 2012 Pearson Education, Inc. continue switch (category) { case 10: System.out.println ("a perfect score. Well done."); break; case 9: System.out.println ("well above average. Excellent."); break; case 8: System.out.println ("above average. Nice job."); break; case 7: System.out.println ("average."); break; case 6: System.out.println ("below average. You should see the"); System.out.println ("instructor to clarify the material " + "presented in class."); break; default: System.out.println ("not passing."); } Sample Run Enter a numeric grade (0 to 100): 91 That grade is well above average. Excellent.