week 4 - wednesday. what did we talk about last time? if statements else statements nested...
TRANSCRIPT
The if part
Any boolean expression
Any single executable statement
Anatomy of an if
if( condition ) statement;
An if with multiple statements
if( condition ){
statement1;statement2;…statementn;
}
A whole bunch of
statements
Speed limit answer
The simplest answer:
What if we want to add a message if the speed is legal?
if( speed > 3.0e8 )System.out.println("Not so fast!");
if( speed > 3.0e8 )System.out.println("Not so fast!");
elseSystem.out.println("That speed's fine.");
DNA
Assume that you have a variable called base of type char
Let base contain one of: 'A', 'C', 'G', 'T' Write a series of if- and else-statements
that will print out the chemical name of the base denoted by the corresponding character A -> Adenine C -> Cytosine G -> Guanine T -> Thymine
Printing DNA bases
What if you want to take care of upper and lower cases?
if( base == 'A' )System.out.println("Adenine");
else if( base == 'C' )System.out.println("Cytosine");
else if( base == 'G' )System.out.println("Guanine");
else if( base == 'T' )System.out.println("Thymine");
elseSystem.out.println("Your base doesn't " + "belong to us");
Upper and lower case bases using logic
Is there a simpler way?
if( base == 'A' || base == 'a' )System.out.println("Adenine");
else if( base == 'C' || base == 'c' )System.out.println("Cytosine");
else if( base == 'G' || base == 'g' )System.out.println("Guanine");
else if( base == 'T' || base == 't' )System.out.println("Thymine");
elseSystem.out.println("Your base doesn't " + "belong to us");
Upper and lower case bases using character conversion
base = Character.toUpperCase( base );
if( base == 'A' )System.out.println("Adenine");
else if( base == 'C' )System.out.println("Cytosine");
else if( base == 'G' )System.out.println("Guanine");
else if( base == 'T' )System.out.println("Thymine");
elseSystem.out.println("Your base doesn't " + "belong to us");
if statements are okay…
But, didn't that DNA example seem a little clunky?
Surely, there is a cleaner way to express a list of possibilities
Enter: the switch statement
Anatomy of a switch statement
switch( data ){
case value1:statements 1;
case value2:statements 2;
…case valuen:
statements n;default:
default statements;}
DNA hittin' switches
switch( base ){case 'A': System.out.println("Adenine");
break;case 'C': System.out.println("Cytosine");
break;case 'G': System.out.println("Guanine");
break;case 'T': System.out.println("Thymine");
break;default: System.out.println("Your base" + "doesn't belong to us");
break; // unnecessary}
Peculiarities of switch
int data = 3;switch( data ){
case 3:System.out.println("Three");
case 4:System.out.println("Four");break;
case 5:System.out.println("Five");
}
Both "Three" and "Four" are
printed
The break is optional
The default is
optional too
Rules for switch
1. The data that you are performing your switch on must be an int, a char, or a String
2. The value for each case must be a literal3. Execution will jump to the case that
matches4. If no case matches, it will go to default5. If there is no default, it will skip the whole
switch block6. Execution will continue until it hits a break
DNA with upper and lower case
switch( base ){
case 'A': case 'a': System.out.println("Adenine");break;
case 'C': case 'c':System.out.println("Cytosine");break;
case 'G': case 'g':System.out.println("Guanine");break;
case 'T': case 't':System.out.println("Thymine");break;
default: System.out.println("Your base doesn't " + "belong to us");
break; // unnecessary}
A caution about switch
Using if-statements is usually saferif-statements are generally clearer
and more flexibleswitch statements are only for long
lists of specific cases Be careful about inconsistent use of break
Example 1
Write a program that reads in various ages and prints out any special abilities you gain at that age
16 Drive a car
17 Watch R-rated movies
18 Vote and smoke
21 Drink
25 Rent cars
30 Be a senator
35 Be president
Example 2
Write a program that reads in wedding anniversaries and gives the traditional gift for that anniversary
Anniversary Traditional
1st Paper
2nd Cotton
3rd Leather
4th Fruit or Flowers
5th Wood
6th Candy or Iron
7th Wool or Copper
8th Pottery or Bronze
9th Willow or Pottery
10th Tin or Aluminum
20th China
25th Silver
30th Pearl
35th Coral
40th Ruby
45th Sapphire
50th Gold
60th Diamond
Example 3
Write a program that will read in a positive integer and print out the corresponding ordinal number
Number Ordinal Number Ordinal
1 1st 11 11th
2 2nd 12 12th
3 3rd 13 13th
4 4th 21 21st
5 5th 22 22nd
6 6th 23 23rd