Week 4 - Wednesday CS 121
Last time What did we talk about last time? if statements else statements Nested selection statements
Questions?
Project 1
if Review
if( condition ) statement; Anatomy of an if Any boolean expression The if part if( condition ) statement; Any single executable statement
Two different outcomes Anatomy of an if-else if( condition ) statement1; else statement2; Two different outcomes
An if with multiple statements if( condition ) { statement1; statement2; … statementn; } A whole bunch of statements
Nested ifs if( condition1 ) { statement1; if( condition2 ) … }
if Examples
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 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"); else System.out.println("Your base doesn't " + "belong to us"); What if you want to take care of upper and lower cases?
Upper and lower case bases using logic 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"); else System.out.println("Your base doesn't " + "belong to us"); Is there a simpler way?
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"); else System.out.println("Your base doesn't " + "belong to us");
switch statements
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"); case 'G': System.out.println("Guanine"); case 'T': System.out.println("Thymine"); default: System.out.println("Your base" + "doesn't belong to us"); break; // unnecessary } Go to SwitchMonth
Peculiarities of switch Both "Three" and "Four" are printed int data = 3; switch( data ) { case 3: System.out.println("Three"); case 4: System.out.println("Four"); break; case 5: System.out.println("Five"); } Go to SwitchAge The break is optional The default is optional too
Rules for switch The data that you are performing your switch on must be an int, a char, or a String The value for each case must be a literal Execution will jump to the case that matches If no case matches, it will go to default If there is no default, it will skip the whole switch block 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"); case 'G': case 'g': System.out.println("Guanine"); case 'T': case 't': System.out.println("Thymine"); default: System.out.println("Your base doesn't " + "belong to us"); break; // unnecessary } Go to SwitchDays
A caution about switch Using if-statements is usually safer if-statements are generally clearer and more flexible switch 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
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 2 Write a program that reads in wedding anniversaries and gives the traditional gift for that anniversary
Example 3 Write a program that will read in a positive integer and print out the corresponding ordinal number 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
Quiz
Upcoming
Next time… Review Lab 4
Reminders Keep reading Chapter 4 of the textbook Keep working on Project 1 Due this Friday! Exam 1 next Monday