Sunday, March 1, 2020

Using If-Then-Else and Switch in Conditional Statements

Using If-Then-Else and Switch in Conditional Statements Conditional statements in a computer program support decisions based on a certain condition. If the condition is met, or true, a certain piece of code is executed. For example, you want to convert user-entered text to lowercase. Execute the code only if the user entered capitalized text. If not, you dont want to execute the code because it will lead to a runtime error. There are two main conditional statements used in Java:  the if-then and  if-then-else statements, and the switch statement. The If-Then and If-Then-Else Statements The most basic flow control statement in Java is if-then: if [something] is true, do [something]. This statement is a good choice for simple decisions. The basic structure of an if statement starts with the word if, followed by the statement to test, followed by curly braces that wrap the action to take if the statement is true. It looks like this: if (  statement  ) {// do something here....} This statement can also be extended to do something else if the condition is false: if (  statement  ) { // do something here...}else {// do something else...} For example, if you are determining whether someone is old enough to drive, you might have a statement that says if your age is 16 or older, you can drive; else, you cannot drive. int age 17;if age 16 {System.out.println(You can drive.);}else  {System.out.println(You are not old enough to drive.) There is no limit to the number of else statements you can add.   Conditional Operators In the example above, we used a single operator. These are the standard operators you can use: equal to: less than: more than: greater than or equal to: less than or equal to: In addition to these, there are four more operators used with conditional statements: and: not:!  or: ||is equal to:    For example, the driving age is considered to be from age 16 to age 85, in which case the AND operator can be used. else if ( age 16   age 85 ) This will return true only if both conditions are met. The operators NOT, OR, and IS EQUAL TO can be used in a similar way. The Switch Statement The switch statement provides an effective way to deal with a section of code that could branch in multiple directions based on a single variable. It does not support the conditional operators the if-then statement does, nor can it handle multiple variables. It is, however, a preferable choice when the condition will be met by a single variable because it can improve performance and is easier to maintain.   Heres an example: switch ( single_variable ) {case value://code_here;break;case value://code_here;break;default://set a default;} Note that you start with the switch, provide a single variable and then set out your choices using the term case. The keyword break completes each case of the switch statement. The default value is optional, but good practice. For example, this switch prints the lyric of the song  Twelve Days of Christmas  given a provided day. int day 5; String lyric ;  // empty string to hold the lyric switch (day) {case 1: lyric A partridge in a pear tree.;break;case 2:lyric 2 turtle doves;break;case 3:lyric 3 French hens;break;case 4:lyric 4 calling birds;break;case 5:lyric 5 gold rings;break;case 6:lyric 6 geese-a-laying;break;case 7:lyric 7 swans-a-swimming;break;case 8:lyric 8 maids-a-milking;break;case 9:lyric 9 ladies dancing;break;case 10:lyric 10 Lords-a-leaping;break;case 11:lyric 11 pipers piping;break;case 12:lyric 12 drummers drumming;break;default:lyric There are only 12 days.;break;}System.out.println(lyric); In this example, the value to test is an integer. Java SE 7 and later support a string object in the expression. For example:String day second;String lyric ;  // empty string to hold the lyric switch (day) {case first:lyric A partridge in a pear tree.;break;case second:lyric 2 turtle doves;break;case third:lyric 3 French hens;break;// etc.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.