Download presentation
Presentation is loading. Please wait.
Published bySharyl Myrtle Foster Modified over 9 years ago
1
Week 4 - Wednesday
2
What did we talk about last time? if statements else statements Nested selection statements
6
The if part Any boolean expression Any single executable statement if( condition ) statement;
7
Two different outcomes if( condition ) statement1; else statement2;
8
if( condition ) { statement1; statement2; … statementn; } A whole bunch of statements
9
if( condition1 ) { statement1; if( condition2 ) { if( condition3 ) statement2; … }
11
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!"); if( speed > 3.0e8 ) System.out.println("Not so fast!"); else System.out.println("That speed's fine."); if( speed > 3.0e8 ) System.out.println("Not so fast!"); else System.out.println("That speed's fine.");
12
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
13
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"); 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");
14
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"); 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");
15
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"); 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");
17
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
18
switch( data ) { case value1: statements 1; case value2: statements 2; … case valuen: statements n; default: default statements; }
19
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 } 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 }
20
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
21
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
22
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 } 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 }
23
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
24
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
25
Write a program that reads in wedding anniversaries and gives the traditional gift for that anniversary
26
Write a program that will read in a positive integer and print out the corresponding ordinal number NumberOrdinalNumberOrdinal 11st1111th 22nd1212th 33rd1313th 44th2121st 55th2222nd 66th2323rd
29
Review Lab 4
30
Keep reading Chapter 4 of the textbook Keep working on Project 1 Due this Friday! Exam 1 next Monday Office hours this Friday from 3:30-5pm are canceled due to travel
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.