Week 4 -Wednesday What did we talk about last time? if and else - - PowerPoint PPT Presentation

week 4 wednesday what did we talk about last time if and
SMART_READER_LITE
LIVE PREVIEW

Week 4 -Wednesday What did we talk about last time? if and else - - PowerPoint PPT Presentation

Week 4 -Wednesday What did we talk about last time? if and else examples Pitfalls 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


slide-1
SLIDE 1

Week 4 -Wednesday

slide-2
SLIDE 2

 What did we talk about last time?  if and else examples  Pitfalls

slide-3
SLIDE 3
slide-4
SLIDE 4
slide-5
SLIDE 5
slide-6
SLIDE 6

 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
slide-7
SLIDE 7

 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"); else System.out.println("Your base doesn't belong to us");

slide-8
SLIDE 8

 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"); else System.out.println("Your base doesn't belong to us");

slide-9
SLIDE 9

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");

slide-10
SLIDE 10
slide-11
SLIDE 11

 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

slide-12
SLIDE 12

switch( data ){ case value1: statements 1; case value2: statements 2; … case valuen: statements n; default: default statements; }

slide-13
SLIDE 13

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 }

slide-14
SLIDE 14

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

  • ptional

The default is optional too

slide-15
SLIDE 15
  • 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 literal
  • 3. Execution will jump to the case that matches
  • 4. If no case matches, it will go to default
  • 5. If there is no default, it will skip the whole switch block
  • 6. Execution will continue until it hits a break
slide-16
SLIDE 16

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 }

slide-17
SLIDE 17

 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

slide-18
SLIDE 18

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
slide-19
SLIDE 19

 Write a program

that reads in wedding anniversaries and gives the traditional gift for that anniversary

Anniversary Traditional Anniversary Traditional 1st Paper 10th Tin or Aluminum 2nd Cotton 20th China 3rd Leather 25th Silver 4th Fruit or Flowers 30th Pearl 5th Wood 35th Coral 6th Candy or Iron 40th Ruby 7th Wool or Copper 45th Sapphire 8th Pottery or Bronze 50th Gold 9th Willow or Pottery 60th Diamond

slide-20
SLIDE 20

 Write a program that will read in a positive integer and print

  • ut 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

slide-21
SLIDE 21
slide-22
SLIDE 22
slide-23
SLIDE 23

 Review for Exam 1

slide-24
SLIDE 24

 Study for Exam 1

  • Monday, online, during class time

 Keep working on Project 2

  • Never look at another student's code