Java if Statement
In this article, you will learn about Java if else statment.
Java supports two selection statements: if and switch. These statements allow you to control the
flow of your program’s execution based upon conditions known only during run time.
Syntax:
if (condition) statement1; else statement2Here, each statement may be a single statement or a compound statement enclosed in curly braces (that is, a block). The condition is any expression that returns a boolean value.
Example:
int a, b; // ... if(a < b) a = 0; else b = 0;Here, if a is less than b, then a is set to zero. Otherwise, b is set to zero. In no case are they both set to zero.
Java Nested if else
Anested if is an if statement that is the target of another if or else. Nested ifs are very common in programming. When you nest ifs, the main thing to remember is that an else statement always refers to the nearest if statement that is within the same block as the else and that is not already associated with an else.Example
if(i == 10) { if(j < 20) a = b; if(k > 100) c = d; // this if is else a = c; // associated with this else } else a = d; // this else refers to if(i == 10)As the comments indicate, the final else is not associated with if(j<20) because it is not in the same block (even though it is the nearest if without an else).
Java if-else-if Ladder
A common programming construct that is based upon a sequence of nested ifs is the if-else-if ladder.if(condition) statement; else if(condition) statement; else if(condition) statement; else statement;The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed.
Example
class IfElse { public static void main(String args[]) { int month = 4; // April String season; if(month == 12 || month == 1 || month == 2) season = "Winter"; else if(month == 3 || month == 4 || month == 5) season = "Spring"; else if(month == 6 || month == 7 || month == 8) season = "Summer"; else if(month == 9 || month == 10 || month == 11) season = "Autumn"; else season = "Bogus Month"; System.out.println("April is in the " + season + "."); } }When you run the program, the output will be:
April is in the Spring.You might want to experiment with this program before moving on. As you will find, no matter what value you give month, one and only one assignment statement within the ladder will be executed.
Java Switch Statement
The switch statement is Java’s multiway branch statement. It provides an easy way to dispatch execution to different parts of your code based on the value of an expression. As such, it often provides a better alternative than a large series of if-else-if statements.Syntex
switch (expression) { case value1: // statement sequence break; case value2: // statement sequence break; . . . case valueN: // statement sequence break; default: // default statement sequence }The expression must be of type byte, short, int, or char; each of the values specified in the case statements must be of a type compatible with the expression.
Example
class SampleSwitch { public static void main(String args[]) { for(int i=0; i<6; i++) switch(i) { case 0: System.out.println("i is zero."); break; case 1: System.out.println("i is one."); break; case 2: System.out.println("i is two."); break; case 3: System.out.println("i is three."); break; default: System.out.println("i is greater than 3."); } } }When you run the program, the output will be:
i is zero. i is one. i is two. i is three. i is greater than 3. i is greater than 3.As you can see, each time through the loop, the statements associated with the case constant that matches i are executed. All others are bypassed.
Example 2
class MissingBreak { public static void main(String args[]) { for(int i=0; i<12; i++) switch(i) { case 0: case 1: case 2: case 3: case 4: System.out.println("i is less than 5"); break; case 5: case 6: case 7: case 8: case 9: System.out.println("i is less than 10"); break; default: System.out.println("i is 10 or more"); } } }When you run the program, the output will be:
i is less than 5 i is less than 5 i is less than 5 i is less than 5 i is less than 5 i is less than 10 i is less than 10 i is less than 10 i is less than 10 i is less than 10 i is 10 or more i is 10 or more
As you can see, execution falls through each case until a break statement (or the end of the switch) is reached.
Java Nested switch Statements
You can use a switch as part of the statement sequence of an outer switch. Since a switch statement defines its own block, no conflicts arise between the case constants in the inner switch and those in the outer switch.Syntex
switch(count) { case 1: switch(target) { // nested switch case 0: System.out.println("target is zero"); break; case 1: // no conflicts with outer switch System.out.println("target is one"); break; } break; case 2: // ...
2 Comments
ReplyDeletewhat are literals in java learn literal in java Statement in free by CodeExampler website
ReplyDeletepg in noida