conditions csci 201l jeffrey miller, ph.d. http :// www - scf. usc. edu /~ csci 201 usc csci 201l

11
Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP://WWW-SCF.USC.EDU/ ~CSCI201 USC CSCI 201L

Upload: dana-malone

Post on 04-Jan-2016

219 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

ConditionsCSCI 201L

Jeffrey Miller, Ph.D.

HTTP://WWW-SCF.USC.EDU/~CSCI201

USC CSCI 201L

Page 2: Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Outline

USC CSCI 201L 2/11

▪ Conditions▪ Program

Page 3: Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Conditional Statements

▪ Java has three conditional statements, similar to C++› if-else› switch-case› Conditional ternary operator ?:

▪ The conditional and boolean operators work similar to C++ as well

USC CSCI 201L 3/11Conditions

Page 4: Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Comparison and Boolean Operators

▪ Comparison operators

== !=

> >=

< <=› NOTE: Comparison operators only work for primitive data

types. Methods will be created for object comparisons

▪ Boolean operators• Bitwise boolean operators

&

|

USC CSCI 201L 4/11Conditions

• Logical boolean operators&&||!

Page 5: Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

if Statements

1 int num=1;2 if (num < 1) {34 } 5 else if (num > 1) {67 } 8 else {910 }

USC CSCI 201L 5/11Conditions

1 int num=1;2 if (num < 1) {34 } 5 if (num > 1) {67 } 8 if (num == 1) {910 }

• What is the difference between these two code snippets?

Page 6: Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

switch Statements

▪ switch statements can only take a byte, short, int, or char▪ The values in the cases must be hard-coded (no variables)

› The conditions must match equality

▪ You can have a default case if you want▪ Once one case becomes true, the rest of the cases will not be checked

1 switch (grade) {2 case ‘A’:3 System.out.print (“Excellent”);4 case ‘B’:5 System.out.print (“Good”);6 break;7 case ‘C’:8 System.out.print (“Eh”);9 case ‘D’:10 System.out.print (“I guess”);11 break;12 case ‘F’:13 System.out.print (“Failed”);14 } USC CSCI 201L 6/11Conditions

Page 7: Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Multi-Condition switch Statement

1 switch (grade) {2 case ‘A’:3 case ‘B’:4 case ‘C’:5 case ‘D’:6 System.out.print (“Passed”);7 break;8 case ‘F’:9 default:10 System.out.print (“Failed”);11 }

USC CSCI 201L 7/11Conditions

Page 8: Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

switch vs if

▪ It does not make a difference if you use an if statement or a switch statement› There is no change in efficiency

▪ Every switch statement can be written as an if statement, but not vice versa – why not?

USC CSCI 201L 8/11Conditions

Page 9: Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Conditional Ternary Operator

▪ There is only one ternary operator in Java (and C++)▪ The conditional ternary operator can assign a value based on the result of

a condition

1 int num = 2;2 String status;3 if (num < 3) {4 status = “low”;5 }6 else {7 status = “high”8 }910 status = (num < 3) ? “low” : “high”;

USC CSCI 201L 9/11Conditions

Page 10: Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Outline

USC CSCI 201L 10/11

▪ Conditions▪ Program

Page 11: Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Program

▪ Write a program to prompt the user for two numbers and takes one parameter off the command line to represent the operation he would like to perform with the two numbers. Perform that operation. Use a case statement to determine the operation. A sample execution is below.

USC CSCI 201L 11/11Program

c:\>java csci201.Operation /Please enter first number: 21Please enter second number: 321 / 3 = 7c:\>