week 4 wednesday what did we talk about last time if and
play

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


  1. Week 4 -Wednesday

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

  3.  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

  4. 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?

  5. 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?

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

  7.  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

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

  9. 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 }

  10. Both "Three" int data = 3; and "Four" switch( data ) { are printed case 3: System.out.println("Three"); case 4: System.out.println("Four"); break; case 5: System.out.println("Five"); The } break is The default optional is optional too

  11. 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

  12. 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 }

  13.  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

  14. 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

  15. Anniversary Traditional Anniversary Traditional  Write a program 1st Paper 10th Tin or Aluminum that reads in 2nd Cotton 20th China wedding 3rd Leather 25th Silver anniversaries and 4th Fruit or Flowers 30th Pearl 5th Wood 35th Coral gives the 6th Candy or Iron 40th Ruby traditional gift for 7th Wool or Copper 45th Sapphire that anniversary 8th Pottery or Bronze 50th Gold 9th Willow or Pottery 60th Diamond

  16.  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

  17.  Review for Exam 1

  18.  Study for Exam 1  Monday, online, during class time  Keep working on Project 2  Never look at another student's code

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend