College of Computer Studies and Engineering INFORMATION TECHNOLOGY DEPARTMENT ITC c105 – Computer Programming using Java Module 9 CONTROL STRUCTURES Learning Objectives: At the end of this lesson, the students shall be able to: 1. Learn how to use switch structure which allows a variable to be tested for equality against a list of values. 2. Create a Java program using switch structure. A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case. Syntax: switch(expression) { case value 1 : // Statements 1 break; // optional case value 2 : // Statements 2 break; // optional // You can have any number of case statements. } default : // Statements // Optional The following rules apply to a switch statement − The variable used in a switch statement can only be integers, convertible integers (byte, short, char), strings and numbers. 1|Page College of Computer Studies and Engineering INFORMATION TECHNOLOGY DEPARTMENT ITC c105 – Computer Programming using Java You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon. The value for a case must be the same data type as the variable in the switch and it must be a constant or a literal. When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached. When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement. Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached. A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case. Flow Diagram Expression True Case 1 Statement break Statement break Statement break Statement break False True Case 2 False True Case 3 False True Case 4 Statement after switch 2|Page College of Computer Studies and Engineering INFORMATION TECHNOLOGY DEPARTMENT ITC c105 – Computer Programming using Java Example 1: public class Test { public static void main(String args[]) { char grade = 'C'; switch(grade) { case 'A' : System.out.println("Excellent!"); break; case 'B' : case 'C' : System.out.println("Well done"); break; case 'D' : System.out.println("You passed"); case 'F' : System.out.println("Better try again"); break; default : System.out.println("Invalid grade"); } System.out.println("Your grade is " + grade); }} Output: 3|Page College of Computer Studies and Engineering INFORMATION TECHNOLOGY DEPARTMENT ITC c105 – Computer Programming using Java Example 2: import javax.swing.JOptionPane; public class SwitchSample1 { public static void main(String args[]) { int day=Integer.parseInt(JOptionPane.showInputDialog("Enter day: ")); String dey; switch(day) { case 1: dey = "Sunday"; break; case 2: dey = "Monday"; break; case 7: dey = "Saturday"; break; default: dey = "Invalid"; } JOptionPane.showMessageDialog(null,"Today is: " + dey); }} 4|Page College of Computer Studies and Engineering INFORMATION TECHNOLOGY DEPARTMENT ITC c105 – Computer Programming using Java Example 2: import javax.swing.JOptionPane; public class SwitchSample2 { public static void main(String args[]) { char month = JOptionPane.showInputDialog("Enter month: ").charAt(0); String dey; switch(month) { case '1': dey = "January"; break; case '2': dey = "February"; break; case '7': dey = "July"; break; default: dey = "Invalid"; } JOptionPane.showMessageDialog(null,"The month is: " + dey); }} 5|Page College of Computer Studies and Engineering INFORMATION TECHNOLOGY DEPARTMENT ITC c105 – Computer Programming using Java Example 3: import javax.swing.JOptionPane; public class SwitchSample3 { public static void main(String args[]) { char sex = JOptionPane.showInputDialog("Enter sex: ").charAt(0); String gender; switch(sex) { case 'M': case 'm': gender = "Male"; break; case 'f': case 'F': gender = "Female"; break; default: gender = "Invalid"; } JOptionPane.showMessageDialog(null,"You are: " + gender); }} 6|Page College of Computer Studies and Engineering INFORMATION TECHNOLOGY DEPARTMENT ITC c105 – Computer Programming using Java charAt( ) method is used in Example #’s 2 & 3 The method charAt(int index) returns the character at the specified index. The index value should lie between 0 and length()-1. Example: char month = JOptionPane.showInputDialog("Enter month: ").charAt(0); char month = JOptionPane.showInputDialog("Enter month: ").charAt(0); would return the first character of the input value. charAt() Method Description This method returns the character located at the String's specified index. The string indexes start from zero. Syntax Here is the syntax of this method − public char charAt(int index) Parameters Here is the detail of parameters − • index − Index of the character to be returned. • Return Value − This method returns a char at the specified index. Example public class Test { public static void main(String args[]) { String s = "Strings are immutable"; char result = s.charAt(8); System.out.println(result); }} This will produce the following result − Output a 7|Page College of Computer Studies and Engineering INFORMATION TECHNOLOGY DEPARTMENT ITC c105 – Computer Programming using Java Example 4 public class CharAtExample { public static void main(String args[]) { String str = "Welcome to string handling tutorial"; char ch1 = str.charAt(0); char ch2 = str.charAt(5); char ch3 = str.charAt(11); char ch4 = str.charAt(20); System.out.println("Character System.out.println("Character System.out.println("Character System.out.println("Character at 0 index is: "+ch1); at 5th index is: "+ch2); at 11th index is: "+ch3); at 20th index is: "+ch4); }} Character at 0 index is: W Character at 5th index is: m Character at 11th index is: s Character at 20th index is: n The method charAt(int index) returns the character at the specified index. The index value should lie between 0 and length()-1. Reference/s: JEDI Course Notes https://www.tutorialspoint.com/java/number_equals.htm https://beginnersbook.com/2013/12/java-string-charat-method-example/ 8|Page College of Computer Studies and Engineering INFORMATION TECHNOLOGY DEPARTMENT ITC c105 – Computer Programming using Java Name: Course/Year: Score: Date: SEATWORK EXERCISE MPT3 (switch) Week 10: Screen shot the output of the following snippet: 1. 9|Page College of Computer Studies and Engineering INFORMATION TECHNOLOGY DEPARTMENT ITC c105 – Computer Programming using Java 2. 10 | P a g e College of Computer Studies and Engineering INFORMATION TECHNOLOGY DEPARTMENT ITC c105 – Computer Programming using Java 3 11 | P a g e