switch statements

Information about switch statements

Published on July 9, 2014

Author: nantha98

Source: authorstream.com

Content

PowerPoint Presentation: 1 Selection Structure using switch statement CBOP3203 – Object Oriented Programming Learning Outcomes: 2 Learning Outcomes By the end of this part, you should be able to: Describe the meaning of switch statement in Java Write Java programs using switch statement Difference between if-else and switch statements: Difference between if-else and switch statements Both if-else and switch statements are used in selection control structure. if-else is used when one set of instructions must be selected from many possible alternatives. On the other hand, the switch statement provides an alternative to the if-else statement for cases that compare the value of an integer expression to a specific value. It means a switch statement allows you to test the value of an expression and, depending on that value, to jump to some location within the switch statement. General form of switch statement: General form of switch statement Problem: Problem Let us see one example where switch statement can be used. Problem: The variable N has a value. if the value of N is 1, set x=10 and display the value of x. On the hand, if the value of N is 2, set x=20 and display the value of x. But, if the value of N is 3, set x=30 and display the value of x. The algorithm for the problem is shown below: : The algorithm for the problem is shown below: if N=1, set x=10 and then display x if N=2, set x=20 and then display x if N=3, set x=30 and then display x Flow chart for the problem:: Flow chart for the problem: Solution: Solution class UsingSwitch1{ public static void main (String[] args){ int N=3; int x; switch(N){ case (1): x=10; System.out.println(x);break; case (2): x=20; System.out.println(x);break; case (3): x=30; System.out.println(x);break; }//switch }//main }//class Problem (Homework): Problem (Homework) In the previous problem, there is no default statement. This makes sense as the default statement is optional. Your task : Include a default statement in the previous problem and assign 40 to the variable x and then display the value of x. Solution: Solution class UsingSwitch2{ public static void main (String[] args){ int N=3; int x; switch(N){ case (1): x=10; System.out.println(x);break; case (2): x=20; System.out.println(x);break; case (3): x=30; System.out.println(x);break; default: x=40; System.out.println(x);break; }//switch }//main }//class 10 Invalid switch Statements: Invalid switch Statements When using switch statement, you need to be careful as there are certain things that you cannot do with switch statement. Example of invalid switch statements are shown on the next slides Invalid switch Statement (I): Invalid switch Statement (I) Test variable must be of int , char , short or byte date type. double values are not allowed in the switch statement Invalid switch statement Invalid switch Statement (II): Invalid switch Statement (II) Invalid switch statement case cannot have same values Invalid switch Statement (III): Invalid switch Statement (III) Invalid switch statement break statement is missing in the case statements Summary: 15 Summary On the other hand, the switch statement provides an alternative to the if-else statement for cases that compare the value of an integer expression to a specific value. It means a switch statement allows you to test the value of an expression and, depending on that value, to jump to some location within the switch statement. When using switch statement, the keyword case is an important component that will follow the switch statement

Related presentations


Other presentations created by nantha98

Using access modifiers
07. 07. 2014
0 views

Using access modifiers

Object and classes
09. 07. 2014
0 views

Object and classes

09. 07. 2014
0 views

"while" loops

GUI-based Applets
07. 07. 2014
0 views

GUI-based Applets

Java Operators
29. 07. 2014
0 views

Java Operators

Data Types and Variables
29. 07. 2014
0 views

Data Types and Variables