CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Advertisements

Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
INF 523Q Chapter 3: Program Statements (Examples).
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
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:
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.
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 Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 5 Loops.
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:
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
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:
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
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.
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 M.A. Papalaskari, Villanova University Conditional Statements Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 M.A. Papalaskari, Villanova University CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari.
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 M.A. Papalaskari, Villanova University Algorithms 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:
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
Loops Copyright © 2012 Pearson Education, Inc.. Conditionals and Loops (Chapter 5) So far, we’ve looked at: –making decisions with if –how to compare.
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:
1 More about Flow of Control. Topics Debugging Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement.
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.
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 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 Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC111 Quick Revision.
CSC 1051 – Data Structures and Algorithms I
CSC 1051 – Data Structures and Algorithms I
Chapter 6 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,
SELECTION STATEMENTS (1)
Control Statement Examples
Chapter 6 More Conditionals and Loops
Introduction to Classes and Methods
Chapter 6 More Conditionals and Loops
Repetition Statements
CSC 1051 – Data Structures and Algorithms I
Outline Software Development Activities
Outline Boolean Expressions The if Statement Comparing Data
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Presentation transcript:

CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: Some slides in this presentation are adapted from the slides accompanying Java Software Solutions by Lewis & Loftus CSC 1051 M.A. Papalaskari, Villanova University Selection Statements and operators

Selection structures in Java Conditional statement: Other selection structures (Chapter 6 in text) –the conditional operator –the switch statement CSC 1051 M.A. Papalaskari, Villanova University if (n > 0) System.out.print("positive"); else System.out.print("negative");

The Conditional Operator Syntax condition ? expression1 : expression2 If the condition is true, expression1 is evaluated; if it is false, expression2 is evaluated The value of the entire conditional operator is the value of the selected expression Example: Rewrite this  CSC 1051 M.A. Papalaskari, Villanova University if (n > 0) System.out.print("positive"); else System.out.print("negative");

The conditional operator is not a statement WRONG! CSC 1051 M.A. Papalaskari, Villanova University (n > 0) ? System.out.print("positive"): System.out.print("negative");

More examples: int bit = (ans.equalsIgnoreCase("Yes")? 1: 0); String result = (happiness ? "happy" : "sad"); – The conditional operator requires three operands so it is sometimes called the ternary operator CSC 1051 M.A. Papalaskari, Villanova University Hands on: try this in the Person class!Person

if (happiness) page.drawArc (x+15, y+30, 20, 10, 180, 180); else page.drawArc (x+15, y+30, 20, 10, 0, 180); The Conditional Operator Another example: Making the smiley’s happiness show… Before: using if/else CSC 1051 M.A. Papalaskari, Villanova University Hands on: try this in the Smiley class! –Rewrite using conditional operator:

Quick Check Rewrite this statement so that "Dime" is printed if num equals 1. System.out.println ("Your change is " + num + "Dimes")); CSC 1051 M.A. Papalaskari, Villanova University

Recall: Logic of an if-else statement CSC 1051 M.A. Papalaskari, Villanova University The switch Statement: multi-way branches

expression evaluated statement2 case: 2 case: 3 statement3 CSC 1051 M.A. Papalaskari, Villanova University case: 1 statement1 Note: this is a simplified flowchart of the logic of the switch statement The switch Statement: multi-way branches switch statement logic

The switch Statement - example public String toString() { String result = ""; switch (faceValue) { case 1: result = "one"; break; case 2: result = "two"; break; case 3: result = "three"; break; } return result; } CSC 1051 M.A. Papalaskari, Villanova University Hands on: try this in the Die class! Die

The switch Statement in general switch ( expression ) { case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3. default: statement-list-n } If expression matches value2, control jumps to here CSC 1051 M.A. Papalaskari, Villanova University If none of the values match the expression, control jumps to here integer, char, or enumerated types (Java 7 also allows Strings) NO floating point values NO ranges of values (eg: 0<x<10)

So… the logic of the switch is more like this: expression evaluated statements2 23 statements3 CSC 1051 M.A. Papalaskari, Villanova University 1 statements1 Note: this is still a simplified flowchart of the logic of the switch statement

CSC 1051 M.A. Papalaskari, Villanova University //******************************************************************** // 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 GradeReport.java

CSC 1051 M.A. Papalaskari, Villanova University 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. Hands on: try removing one of the break statements

//*********************************************************************** // SwitchExample.java Author: M A Papalaskari // Example of switch statement that does NOT use break after each case. //*********************************************************************** import java.util.Scanner; public class SwitchExample { // Example of using a switch statement. Counts number of digits, zeros, // whitespace, and others in a line of input. public static void main (String[] args) { Scanner scan = new Scanner(System.in); int digits = 0, zeros = 0, whitespace = 0, other = 0; System.out.print("Input line>"); String message = scan.nextLine(); int count = 0; continue CSC 1051 M.A. Papalaskari, Villanova University SwitchExample.java Another example Sample Run ¼¼ Input line>10, 9, 8, 7, 6, 5, 4, 3, 2, 1, Lift off! 11 Digits, of which 1 is a zero 11 whitespace 18 others

while (count < message.length()) { switch (message.charAt(count)) { case '0': zeros++; case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': digits++; break; case ' ': case '\t’: whitespace++; break; default: other++; } // end switch count++; } // end while System.out.print(digits + " Digit" + (digits==1 ? "" : "s")); System.out.print(", of which " + zeros); System.out.println((zeros==1 ? " is a zero " : " are zeros ")); System.out.println(whitespace + " whitespace"); System.out.println(other + " other" + (other==1 ? "" : "s")); } } CSC 1051 M.A. Papalaskari, Villanova University