In an if else statement we also consider the else part of the Boolean statement.
Syntax:
if(condition)
{
...................
statements;
..................
}
else
{
...................
statements;
..................
}
In this loop if the condition is true then the if block only executes and if the Boolean statement is found to be false the the else part executes. Make sure that the else part is the first statement after the body closing
of if statement.
Example:
int b = 6;
if (b%2!=0)
{
System.out.println("Value of b is an odd number");
}
else
{
System.out.println("Value of b is an even number");
}
Note:
If only one statement is present in a loop then you can avoid the brackets of the loop body.
Example
if (true)
System.out.println("Hello i am the only statement in if ");
keep in mind that if the brackets associated with any loop body missing, then compiler consider only the first statement coming after the loop statement as the loop body.
Comments
Post a Comment