Flow Control in Java - If Else - If Else If Else, switch case statement in Java
Flow Control is a key part of all programming languages because without the flow control any of the program is useless and we don't achieve the required task.
Java has several types of Statements to do it:
one-way if statements, two-way if statements, nested if statements, switch statements,
and conditional expressions.
One-Way if Statements
A one-way if statement executes an action if and only if the condition is true. The syntax for a one-way if statement is shown below:
if (boolean-expression) {
statement(s);
}
If the boolean-expression evaluates to true, the statements in the block are executed.
As an example, see the following code:
if (r>= 0)
{
area = r * r * PI;
System.out.println("The area for the circle of radius " + r + " is " + area);
}
Two-Way if Statements
A one-way if statement takes an action if the specified condition is true. If the condition is false, nothing is done. But what if you want to take alternative actions when the condition is false? You can use a two-way if statement. The actions that a two-way if statement specifies differ based on whether the condition is true or false.
Here is the syntax for a two-way if statement:
if (boolean-expression) {
statement(s)-for-the-true-case;
}
else {
statement(s)-for-the-false-case;
}
If the boolean-expression evaluates to true, the statement(s) for the true case are executed; otherwise, the statement(s) for the false case are executed. For example, consider the following code:
if (r >= 0) {
area = r * r * PI;
System.out.println("The area for the circle of radius " +
r + " is " + area);
}
else {
System.out.println("Negative input");
}
If r >= 0 is true, area is computed and displayed; if it is false, the message "Negative input" is printed.
Nested if Statements
Nested if Statements are the statements which are inside the any other if statement and when the outer if statement is true then the inner if will be execute otherwise nothing will be execute.
Flow Control is a key part of all programming languages because without the flow control any of the program is useless and we don't achieve the required task.
Java has several types of Statements to do it:
one-way if statements, two-way if statements, nested if statements, switch statements,
and conditional expressions.
One-Way if Statements
A one-way if statement executes an action if and only if the condition is true. The syntax for a one-way if statement is shown below:
if (boolean-expression) {
statement(s);
}
If the boolean-expression evaluates to true, the statements in the block are executed.
As an example, see the following code:
if (r>= 0)
{
area = r * r * PI;
System.out.println("The area for the circle of radius " + r + " is " + area);
}
Two-Way if Statements
A one-way if statement takes an action if the specified condition is true. If the condition is false, nothing is done. But what if you want to take alternative actions when the condition is false? You can use a two-way if statement. The actions that a two-way if statement specifies differ based on whether the condition is true or false.
Here is the syntax for a two-way if statement:
if (boolean-expression) {
statement(s)-for-the-true-case;
}
else {
statement(s)-for-the-false-case;
}
If the boolean-expression evaluates to true, the statement(s) for the true case are executed; otherwise, the statement(s) for the false case are executed. For example, consider the following code:
if (r >= 0) {
area = r * r * PI;
System.out.println("The area for the circle of radius " +
r + " is " + area);
}
else {
System.out.println("Negative input");
}
If r >= 0 is true, area is computed and displayed; if it is false, the message "Negative input" is printed.
Nested if Statements
Nested if Statements are the statements which are inside the any other if statement and when the outer if statement is true then the inner if will be execute otherwise nothing will be execute.
public class DemoNestedIf
{
public static void main(String st[])
{
int user=45;
if(user<=18)
{
System.out.println("User is Under 18");
}
else if(user>18&&user<40)
{
System.out.println("User is between 19 and 39");
}
else if(user==45||user==50)
{
System.out.println("User is either 45 or 50");
}
else
{
System.out.println("User is older then 40");
}
}
}
switch Statements
The switch statement is similar as the nested if else if statements which is executed only when the single if condition is true and if any of the if condition is found true then it execute it and do not execute any other if statement. The switch case statement depends upon the case value which is passed into the switch statement.
switch (status) {
case 0:
Statement ;
break;
case 1:
Statement;
break;
case 2:
Statement;
break;
case 3:
Satement ;
break;
default:
System.out.println("Error: invalid status");
System.exit(0);
}
Conditional Expressions
You might want to assign a value to a variable that is restricted by certain conditions. For example, the following statement assigns 1 to y if x is greater than 0, and -1 to y if x is less than or equal to 0.
if (x > 0)
y = 1;
else
y = -1;
Alternatively, as in this example, you can use a conditional expression to achieve the same result.
y = (x > 0) ?1 : -1;
Conditional expressions are in a completely different style, with no explicit if in the statement.
The syntax is shown below:
In above conditional statement the result is Statement1 if the Boolean expression is true or if the Boolean expression is false the result is Satement2.
Suppose you want to assign the larger number between variable number1 and number2 to max. You can simply write a statement using the conditional expression:
max = (number1 > number2) ? number1 : number2;
For another example, the following statement displays the message “number is even” if number is even, and otherwise displays “number is odd.”
System.out.println((number % 2 == 0) ? "number is even" : "number is odd");
{ 0 comments... read them below or add one }
Post a Comment