switch statement

8

Upload: patrick-john-mcgee

Post on 12-Apr-2017

557 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Switch statement
Page 2: Switch statement

Today's Computing Challenge• Challenge One: Identify Switch statements in a piece of code and

understand why they are used.

• Challenge Two: Apply an Switch statement to a piece of code.

• Aspire Three: To reorder a sequence of code containing switch statements and expand the code so the application works correctly.

*Additional Extension create your own switch statements including description and comments into the code hint: use // for code comments

Page 3: Switch statement

Switch Statements• The switch statement is used far less often than

the if statement, but it is great for simplifying the decisions in your code.

*Please note that an expression cannot be a double or float value!

Page 4: Switch statement

Switch Statements Java Code

1. switch (expression) {2. case constant-1:3. statements-14. break;5. case constant-2:6. statements-27. break;8. default: // optional default case9. statements-(N+1)10. } // end of switch statement

Page 5: Switch statement

Create the following Switch Statement1. public class switchStatement {2. public static void main(String[] args) {3. int month = 8;4. String monthString;5. switch (month) {6. case 1: monthString = "January";7. break;8. case 2: monthString = "February";9. default: monthString = "Invalid month";10. break;11. }12. System.out.println(monthString);13. }14. }

Start a new java application in Netbeans and call it switchStatement

This code takes an integer and prints out the month of the year.

As you have noticed the code is incomplete!

The challenge is to include the remaining months to the code.

Once you have completed the task take a screenshot

Upload the evidence to your OneNote!

Page 6: Switch statement

Java CodeStart a new java application in Netbeans and call it rockPaperScissors

The code opposite does not work!

The package is named incorrectly!The other lines of code are correct but they are in the wrong order.

Your task is to correct and reorder the code so that the app works.

*Think methodically and sequentially and make sure you upload the evidence

Page 7: Switch statement

ExtensionAdapt this java application

When the code is functioning correctly see if you can include the code to allow the computer to play with a human!

Use your knowledge from previous lessons to form the solution!

*Remember to upload the evidence to OneNote!

Page 8: Switch statement

Plenary