CompSci 125 Lecture 11 switch case The? conditional operator do while for
Announcements hw5 Due 10/4 p2 Due 10/5
switch case!
The switch case Statement Consider a simple four-function calculator 16 buttons: 0..9, +, -, /, *, =, Clear Each button press causes one of 16 code blocks to execute You could write this up with 16 if statements But switch case provides a somewhat cleaner approach Java can sometimes generate more efficient code
Example switch Statement String season;! Scanner stdin = new Scanner(System.in);! int month = stdin.nextint();! switch (month) {! case 1: System.out.println( Winter );! case 4: System.out.println( Spring );! case 7: System.out.println( Summer );! case 11: System.out.println( Fall );! }!!!
Using a default case String season;! Scanner stdin = new Scanner(System.in);! int month = stdin.nextint();! switch (month) {! case 1: System.out.println( Winter );! case 4: System.out.println( Spring );! case 7: System.out.println( Summer );! case 11: System.out.println( Fall );! default: System.out.println( Ignored );! }!!!
Multiple case Labels String season;! Scanner stdin = new Scanner(System.in);! int month = stdin.nextint();! switch (month) {! case 1:! case 2: System.out.println( Winter );! case 4:! case 5: System.out.println( Spring );! case 7:! case 8: System.out.println( Summer );! case 10:! case 11: System.out.println( Fall );! default: System.out.println( Ignored );! }!!!
Omitting the break! String season;! Scanner stdin = new Scanner(System.in);! int month = stdin.nextint();! switch (month) {! case 12: System.out.print( Solstice );! case 1:! case 2: System.out.println( Winter );! case 3: System.out.print( Equinox );! case 4:! case 5: System.out.println( Spring );! case 6: System.out.println( Solstice );! case 7:! case 8: System.out.println( Summer );! case 9: System.out.print( Equinox );! case 10:! case 11: System.out.println( Fall );! default: System.out.println( Ignored );! }!!!
Restrictions on Expression Type Early versions of Java restricted the switch expression to certain primitive types: int, byte, char and short! Also allowed the wrapper classes: Integer, Byte, Character and Short! Also allowed enumerated types Next version, Java SE7, will allow the String class
What if there s no case and no default? If no case matches the expression And no default is provided Execution continues with the first statement following the switch block
The? Conditional Operator
Conditional Operator? result = (condition)? value1 : value2 ; 1. Evaluates the boolean expression (condition) 2. If true, the result is value1 3. If false, the result is value2
Contrast? with the if Statement Imperative vs. Data Flow if (condition) result=value1; else result=value2; result = (condition)? value1 : value2;
Summary of if, if else,?, and switch!
Four Conditional Possibilities A conditional statement selects a course of action determined by a condition if (condition) statement;! if (condition) statement1; else statement2;! condition? value1 : value 2;! switch (condition) {case 1: case 2: default: }! Which one should I use?
if (condition) statement;! Condition is a boolean expression evaluating to true or false Condition determines whether to execute optional statement
if (condition) statement1; else statement2;! Condition is a boolean expression evaluating to true or false Condition selects statement1 or statement2 One of these two statements will always be executed!
condition? value1 : value 2 Condition is a boolean expression that selects either value1 or value2 Contrast with if..else which selects the next statement to execute The? conditional operator selects a value, not a statement result = condition? value1 : value2;! if (condition) result = value1; else result = value2;!
switch (condition) case Condition not restricted to a boolean expression. Often has an int value. Enumerations and Strings supported in recent Java versions. Selects one of many possible paths of execution Contrast with other conditionals whose boolean expressions limit their selection to two choices
do while!
Loops The while loop tests the continuation condition at the top of the loop, before executing statement(s) in the code block If the continuation condition fails in the first test, the code block is never executed Sometimes we want to go through the loop at least once
The Do While Loop The Do While Loop tests the continuation condition at the bottom of the loop Thus, the code block is always executed at least once
Example Do While Loop int i=20;! do {! System.out.println(i++);! } while (i<=10);! Similar to a while loop, but continuation test is at bottom Ensures the code block is executed at least once Not used a lot, but is occasionally handy
The for Loop
Loops Need to repeatedly execute a block of statement[s] Each time through the block is called an iteration or repetition Counter-Controlled repetition Sentinel-Controlled repetition
Review Sentinel-Controlled Repetition: Used when number of required iterations is unknown in advance Scanner stdin = new Scanner(System.in);! int x = stdin.nextint();! while (x>0) {! System.out.println(x*x);! x = stdin.nextint();! }! Sentinel variable controls repetition: x Continuation condition: x>0 Sentinel initialized by reading first value from stdin Sentinel update: Read from stdin inside the loop
Review Counter-Controlled Repetition: Used when number of required iterations is known in advance int i = 1;! while (i<=10) {! System.out.println(i);! i++;! }! Control variable: i Initial value of the control variable: 1 Continuation condition: i<=10 Control variable increment: 1
Counter-Controlled Repetition: The for Loop for(int i=1; i<=10; i++) {! }! System.out.println(i);! It s a shortcut for coding with a while loop Control (counter) variable: i Initial value of the control variable: 1 Continuation condition: i<=10 Control variable increment: 1
Counter-Controlled Repetition: Decrementing with a for Loop for(int i=10; i>=1; i--) {! System.out.println(i);! }! Control variable: i Initial value: 10 Increment/decrement: -1 Continuation condition: i>=1
Example: Interest double principal = 1000;! double interestrate = 0.05;! for(int i=1; i<=10; i++) {! }! principal += principal * interestrate;! System.out.println(principal);!