Presentation is loading. Please wait.

Presentation is loading. Please wait.

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.

Similar presentations


Presentation on theme: "©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement."— Presentation transcript:

1 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement

2 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 3-2 The switch Statement The if - else statement allows you to make true / false branches. The switch statement allows you to use an ordinal value to determine how a program will branch. The switch statement can evaluate an integer type or character type variable and make decisions based on the value.

3 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 3-3 The switch Statement The switch statement takes the form: switch (SwitchExpression) { case CaseExpression: // place one or more statements here break; case CaseExpression: // place one or more statements here break; // case statements may be repeated //as many times as necessary default: // place one or more statements here }

4 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 3-4 The switch Statement switch (SwitchExpression) { … } The switch statement will evaluate the SwitchExpression, which can be a byte, short, int, long, or char. If you are using Java 7, the SwitchExpression can also be a string. If there is an associated case statement that matches that value, program execution will be transferred to that case statement.

5 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 3-5 The switch Statement Each case statement will have a corresponding CaseExpression that must be unique. case CaseExpression: // place one or more statements here break; If the SwitchExpression matches the CaseExpression, the Java statements between the colon and the break statement will be executed.

6 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 3-6 The case Statement The break statement ends the case statement. The break statement is optional. If a case does not contain a break, then program execution continues into the next case. –See example: NoBreaks.javaNoBreaks.java –See example: PetFood.javaPetFood.java The default section is optional and will be executed if no CaseExpression matches the SwitchExpression. See example: SwitchDemo.javaSwitchDemo.java

7 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. NoBreaks.java import java.util.Scanner; // Needed for Scanner class /** This program demonstrates the switch statement. */ public class NoBreaks { public static void main(String[] args) { int number; // A number entered by the user // Create a Scanner object for keyboard input. Scanner keyboard = new Scanner(System.in); // Get one of the numbers 1, 2, or 3 from the user. System.out.print("Enter 1, 2, or 3: "); number = keyboard.nextInt(); // Determine the number entered. switch (number) { case 1: System.out.println("You entered 1."); case 2: System.out.println("You entered 2."); case 3: System.out.println("You entered 3."); default: System.out.println("That's not 1, 2, or 3!"); }

8 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. PetFood.java import java.util.Scanner; // Needed for the Scanner class /** This program demonstrates a switch statement. */ public class PetFood { public static void main(String[] args) { String input; // To hold the user's input char foodGrade; // Grade of pet food // Create a Scanner object for keyboard input. Scanner keyboard = new Scanner(System.in);

9 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. PetFood.java (cont) // Prompt the user for a grade of pet food. System.out.println("Our pet food is available in " + "three grades:"); System.out.print("A, B, and C. Which do you want " + "pricing for? "); input = keyboard.nextLine(); foodGrade = input.charAt(0);

10 ©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. PetFood.java (cont) // Display pricing for the selected grade. switch(foodGrade) { case 'a': case 'A': System.out.println("30 cents per lb."); break; case 'b': case 'B': System.out.println("20 cents per lb."); break; case 'c': case 'C': System.out.println("15 cents per lb."); break; default: System.out.println("Invalid choice."); }


Download ppt "©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement."

Similar presentations


Ads by Google