Welcome back, bloggers! In my last post you learned about multidimensional array in Java.
In this post you will learn about the control statements in Java. Java supports two selection statements: if and switch. These statements allow you to control the flow of your program execution based upon the conditions known only during the run time.
In this post you will learn about the control statements in Java. Java supports two selection statements: if and switch. These statements allow you to control the flow of your program execution based upon the conditions known only during the run time.
1. If:
The if statement in Java is a conditional branch statement. It is used to route program execution through two different parts. The general form of if is:
if(condition)
statement 1;
else
statement 2;
Here, each statement may be a single statement or compound statement enclosed by curly braces(that is, block). The condition is any expression that returns a Boolean value. The else clause is optional.
The if statement works like this: If the condition is true, then statement 1 will be executed. Otherwise, statement 2 (if any) will be executed. In no cases will both of the statements be executed. Given below is an example:
int a,b;
//......
if(a>b)
System.out.println("The integer a is greater than the integer b");
else
System.out.println("The integer b is greater than the integer a");
Here, if the value of the integer a is greater than b, the first statement will be executed. If the value of the integer a is less than the integer b, the else statement will be executed.
Now comes the Nested ifs.
Nested ifs:
A nested if is an if statement that is the target of another if or else. Remember one thing, if you nest ifs, the 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. The general expression of nested if is given below:
if(condition 1)
{
executes when the Boolean condition 1 is true
if(condition 2)
{
executes when the Boolean condition 2 is true
}
}
Given below is an example of a nested if:
class test {
public static void main(String[] args) {
int a,b;
int a = 5;
int b = 10;
if(a == 5)
{
if(b == 10)
{
System.out.println("a=5 and b=10");
}
}
}
}
The if-else-if Ladder:
In the if-else-if ladder, one condition is executed from multiple statements.
Its general form is given below:
if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
.
.
.
.
.
else
statement;
Given below is an example of if-else-if ladder:
class test {
public static void main(string[] args) {
int age = 80;
if(age<40) {
System.out.println("Young");
}
else if(age>40 && age <55) {
System.out.println("Middle aged");
}
else if(age>12 && age <20) {
System.out.println("Teenager");
}
else {
System.out.println("Toddler");
}
}
}
Thanks for coming to my blog. Now you have learned about if, nested ifs and else-if-else ladder in part 1 of Control Statement in Java. In part 2, you will learn about switch statement in Java. Stay tuned!
If you find this post helpful, please share it with your friends and colleagues.
FEEL LIKE A PROGRAMMER!!!
Comments
Post a Comment