Java Programming: Ternary Operator, Switch Statement, Break and Continue - Prof. Bonnie J., Exams of Computer Science

Information on the ternary operator, switch statement, break, and continue in java programming. It includes explanations, examples, and warnings about method overloading and implicit widening conversions. It is taken from the cmsc 131 fall 2006 and 2007 lecture notes.

Typology: Exams

Pre 2010

Uploaded on 07/30/2009

koofers-user-d8f
koofers-user-d8f 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMSC 131 Fall 2006
Jan Plane (adapted from Bonnie Dorr)
Lecture Set #12:
Ternary Operator,
Switch, Break, Continue
1. Method Overloading Warning
2. ternary operator: The ?:
(conditional operator)
3. switch
4. break/continue
Method Overloading
prototype:
public static void f(int x, float y)
signature:
f(int , float )
You can only overload methods if they have
different signatures.
Implicit widening conversions
Beware of subtle problems with widening
conversions
CMSC 131 Fall 2007
Jan Plane (adapted from Bonnie Dorr)
1
pf3
pf4
pf5

Partial preview of the text

Download Java Programming: Ternary Operator, Switch Statement, Break and Continue - Prof. Bonnie J. and more Exams Computer Science in PDF only on Docsity!

CMSC 131 Fall 2006 Jan Plane (adapted from Bonnie Dorr)

Lecture Set #12:

Ternary Operator,

Switch, Break, Continue

1. Method Overloading Warning

2. ternary operator: The ?:

(conditional operator)

3. switch

4. break/continue

Method Overloading

 prototype:

public static void f(int x, float y)

 signature:

f(int , float )

 You can only overload methods if they have

different signatures.

 Implicit widening conversions

 Beware of subtle problems with widening

conversions

CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)

1

CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)

2

The Conditional Operator

 The only ternary operator (has 3 operands)

 Format:

 boolean-expression?expression1:expression

 Purpose:

 test to see if boolean-expression is true or false

 whole expression takes on the value of expression1 when

boolean-expression was true

 whole expression takes on the value of expression2 when

boolean-expression was false

 See examples

CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)

3

What is another way to write

this if-else-if statement?

if (grade == ‘A’)

System.out.println (“I’m very happy”);

else if (grade == ‘B’)

System.out.println (“I’m relatively happy”);

else if (grade == ‘C’)

System.out.println (“At least I get credit”);

else

System.out.println (“Check with the professor”);

CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)

6

Case Continuation

 The control expression can have one of the following types: char, int, short, byte  not float, double, boolean, long  not a String or other object  Case continuation also called “cascading case behavior”, “falling through to the next case”, etc.

 It is occasionally handy for combining of cases e.g. case-insensitivity switch (grade) { case ‘a’: case ‘A’: System.out.println (“I’m very happy”); break; … }

 Be very careful about using this cascading behavior!

 Always insert break statements after every case  Then remove ones you do not want

CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)

7

Why Use switch?

 switch can also be implemented using if-else

 switch also restricted in terms of data types in

control statements

 Including break statements is a pain

 However

 switch often more efficient (compiler generates better

code)

 Code can be more compact because of case-continuation

behavior

 Sometimes case analysis is clearer using switch

CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)

8

More about break for loops

 break can also be used to exit immediately from any loop

 while  do-while  for

 e.g. “Read numbers from input until negative number encountered” Scanner sc = new Scanner (System.in); int n; while (true) { n = sc.nextInt (); if (n < 0) break; else ; }  Loop only terminates when break executed  This only happens when n < 0

CMSC 131 Fall 2007 Jan Plane (adapted from Bonnie Dorr)

9

Warning about break

 Undisciplined use of break can make loops

impossible to understand

 Termination of loops without break can be understood

purely by looking while, for parts

 When break included, arbitrary termination behavior can be

introduced

 Rule of thumb: use break only when loop condition

is always true (i.e. break is only way to terminate

loop)

 When you use it, make sure it has a good comment

explaining what is happening