CSCI 1100/1202 January 28, 2002. The switch Statement The switch statement provides another means to decide which statement to execute next The switch.

Slides:



Advertisements
Similar presentations
1 Chapter 3: Program Statements Lian Yu Department of Computer Science and Engineering Arizona State University Tempe, AZ
Advertisements

Slide 1 of 64 Lecture C Lesson 3: Conditions and Loops Unit 1: The if Statement.
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.
Flow of Control (1) : Logic Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Comparing Data Comparing More than Numbers. Comparing Data When comparing data using boolean expressions, it's important to understand the nuances of.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
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.
Java Program Statements Selim Aksoy Bilkent University Department of Computer Engineering
Logical Operators and Conditional statements
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
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.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 3: Program Statements
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
© 2006 Pearson Education 1 Obj: to use compound Boolean statements HW: p.184 True/False #1 – 6 (skip 3)  Do Now: 1.Test your “Charge Account Statement”
Chapter 3: Program Statements
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
Chapter 3: Program Statements Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Chapter 5: Conditionals and loops. 2 Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing.
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,
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
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.
1 Lecture 2 b declaration and use of variables b expressions and operator precedence b introduction to objects b class libraries b flow of control b decision-making.
Chapter 5 Conditionals and Loops 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights.
Control Flow. Data Conversion Promotion happens automatically when operators in expressions convert their operands For example, if sum is a float and.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Control statements Mostafa Abdallah
Chapter 3: Program Statements Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
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)
if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false.
CprE 185: Intro to Problem Solving (using C)
Boolean Expressions and If
Programming Fundamentals
Boolean Expressions & the ‘if’ Statement
Logical Operators & Truth Tables.
Chapter 3: Program Statements
Chapter 3: Program Statements
The ‘while’ Statement September 27, 2006
Chapter 3: Program Statements
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Chap 7. Advanced Control Statements in Java
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

CSCI 1100/1202 January 28, 2002

The switch Statement The switch statement provides another means 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 list associated with the first value that matches

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 value_n : statement-listn default: statement-list } switch and case are reserved words If expression matches value2, control jumps to here

The switch Statement A switch statement can have an optional default case The default case has no associated value and simply uses the reserved word default If the default case is present, control will transfer to it if no other case value matches Though the default case can be anywhere in the switch, it is usually placed at the end If there is no default case, and no other value matches, control falls through to the statement after the switch

The switch Statement Often a break statement is used as the last statement in each case's statement list break; A break statement causes control to transfer to the end of the switch statement If a break statement is not used, the flow of control will continue into the next case Sometimes this can be helpful, but usually we only want to execute the statements associated with one case

The switch Statement The expression of a switch statement must result in an integral data type, like an integer or character; it cannot be a floating point value Note that the implicit boolean condition in a switch statement is equality - it tries to match the expression with a value You cannot perform relational checks with a switch statement See GradeReport.javaGradeReport.java

Logical Operators Boolean expressions can also use the following logical operators: ! Logical NOT && Logical AND || Logical OR They all take boolean operands and produce boolean results Logical NOT is a unary operator (it has one operand), but logical AND and logical OR are binary operators (they each have two operands)

Logical NOT The logical NOT operation is also called logical negation or logical complement If some boolean condition a is true, then !a is false; if a is false, then !a is true Logical expressions can be shown using truth tables a true false !a false true

Logical AND and Logical OR The logical and expression a && b is true if both a and b are true, and false otherwise The logical or expression a || b is true if a or b or both are true, and false otherwise

Truth Tables A truth table shows the possible true/false combinations of the terms Since && and || each have two operands, there are four possible combinations of true and false a true false b true false true false a && b true false a || b true false

Logical Operators Conditions in selection statements and loops can use logical operators to form complex expressions if (total < MAX && !found) System.out.println ("Processing…"); Logical operators have precedence relationships between themselves and other operators See Appendix D (page 549 of text)

Truth Tables Specific expressions can be evaluated using truth tables total < MAX false true found false true false true !found true false true false total < MAX && !found false true false

Comparing Characters Relational operators can be used on character data The results are based on the Unicode character set The following condition is true because the character '+' comes before the character 'J' in Unicode: if ('+' < 'J') System.out.println ("+ is less than J"); The uppercase alphabet (A-Z) and the lowercase alphabet (a-z) both appear in alphabetical order in Unicode

Comparing Strings Remember that a character string in Java is an object We cannot use the relational operators to compare strings The equals method can be called on a string to determine if two strings contain exactly the same characters in the same order The String class also contains a method called compareTo to determine if one string comes before another alphabetically (as determined by the Unicode character set)

Comparing Floating Point Values We also have to be careful when comparing two floating point values ( float or double ) for equality You should rarely use the equality operator ( == ) when comparing two floats In many situations, you might consider two floating point numbers to be "close enough" even if they aren't exactly equal To determine the ‘equality’ of two floats: if (Math.abs (f1 - f2) < ) System.out.println ("Essentially equal.");

More Operators To round out our knowledge of Java operators, let's examine a few more In particular, we will examine the: –increment and decrement operators –assignment operators –conditional operator

Increment and Decrement Operators The increment and decrement operators are arithmetic and operate on one operand The increment operator ( ++ ) adds one to its operand The decrement operator ( -- ) subtracts one from its operand The statement count++; is essentially equivalent to count = count + 1;

Increment and Decrement Operators The increment and decrement operators can be applied in prefix form (before the variable) or postfix form (after the variable) When used alone in a statement, the prefix and postfix forms are basically equivalent. That is, count++; is equivalent to ++count;

Increment and Decrement Operators When used in a larger expression, the prefix and postfix forms have a different effect In both cases the variable is incremented (decremented) But the value used in the larger expression depends on the form: Expression count++ ++count count-- --count Operation add 1 subtract 1 Value in Expression old value new value old value new value

Increment and Decrement Operators If count currently contains 45, then total = count++; assigns 45 to total and 46 to count If count currently contains 45, then total = ++count; assigns the value 46 to both total and count