Decision Making Statements (The If statement)
There are 4 types of if statement provided by Java as you can see above. The first type is If .
Syntax of if :-
if ( condition )
{
Statement ;
}
Flow chart
In the above flow chart we see that--- There is an if statement with a condition. If the condition is true, then statement is executed and then it exits the if statement. If the condition is false, then the statement is not executed and it directly exits if statement.
Note :- The body of if statement may execute or may not execute.
Example code :-
class A
{
public static void main(String args[])
{
int a=5;
if(a==5)
{
System.out.println("A is equal to 5");
}
}
}
--------------------------------------------------------------------------------------------------------------------------
If else
Syntax :-
if ( condition )
{
Statement 1;
}
else
{
Statement 2;
}
Flow chart :-
Here in the flow chart as you see, there is an if condition. If it gets true then statement 1 is executed and then it exits. If the condition is false, then statement 2 is executed and then it exits.
Note :- Here at least one of the statements will execute for sure.
Example code :-
class A
{
public static void main(String args[])
{
int a=5,b=6;
if(a>b)
{
System.out.println("A is greater than B");
}
else
{
System.out.println("B is greater than A");
}
}
}
--------------------------------------------------------------------------------------------------------------------------
If else if Ladder
Syntax :-
if ( condition )
{
Statement 1;
}
else if ( condition )
{
Statement 2;
}
.
.
.
.
else if ( condition )
{
Statement n;
}
Flow chart :-
Here if the condition is true, then statement 1 is executed. If the condition is false, then second condition is checked - if true, statement 2 is executed and if false then next else if condition is checked and this goes on until one of the condition matches or if statement is over.
Note:- You may use an else statement in if else if ladder. Else if cannot be written before if . There should be one if at most and else if part is optional and even else part is optional.
Example code :-
class A
{
public static void main(String args[])
{
int a=5,b=6;
if(a>b)
{
System.out.println("A is greater than B");
}
else if(b>a)
{
System.out.println("B is greater than A");
}
else
{
System.out.println("A is equal to B");
}
else
{
System.out.println("A is equal to B");
}
}
}
--------------------------------------------------------------------------------------------------------------------------
Nested if else
Syntax :-
if ( condition )
{
if ( condition )
{
Statement 1;
}
else
{
Statement 2;
}
}
else
{
if ( condition )
{
Statement 3;
}
else
{
Statement 4;
}
}
Flow chart :-
Example code :-
class A
{
public static void main(String args[])
{
int a=5,b=6,c=7;
if(a>b)
{
if(a>c)
{
if(a>c)
{
System.out.println("A is greatest");
}
else
{
System.out.println("C is greatest");
}
}
else
{
System.out.println("C is greatest");
}
}
else
{
if(b>c)
{
System.out.println("B is greatest");
}
else
{
System.out.println("C is greatest");
}
}}
else
{
System.out.println("C is greatest");
}
}
}
--------------------------------------------------------------------------------------------------------------------------
Now consider this example
int a=5;
if (a==5)
System.out.println("a is equal to 5");
Here as you can see there is no curly braces {}. This program will execute correctly. The curly braces are only necessary if there are more than one statements in the if block. But in the above example, there is only one statement in the if block. Thus we may or may not put it.
Now consider this :-
int a=6;
if (a==5)
System.out.println("a is equal to 5");
System.out.println("a is not equal to 5");
here if this is executed then the output will be :-
a is not equal to 5
It is because a is 6 so the if condition becomes false. Since there are no braces, that means there is only one statement in the if block which is the first statement.
Now consider this :-
int a=6;
if (a==5)
{
System.out.println("a is equal to 5");
System.out.println("a is not equal to 5");
}
here if this is executed then there will be no output.
It is because a is 6 so the if condition becomes false. Since there are braces, that means whole part of if block will be executed.
--------------------------------------------------------------------------------------------------------------------------
The dangling if else problem
Now let us consider this example :-
int a=5,b=6,c=7;
if(a>b)
if(a>c)
System.out.println("A is greatest");
else
System.out.println("Hello world");
In the above example there is an if statement with no braces. Now you must be considering that there are two lines in the outer if. But actually the outer if consists of only one statement that is if(a>c). The inner if consists of one statement which is System.out.println("A is greatest");
Now the question is that the else part belongs to which if part - the inner if or outer if.
This is called dangling if else problem.
if in any program :-
the number of if > the number of else
then that is dangling if else problem.
If this happens then
Java automatically considers that the else written belongs to
the last if that has been written. So here the else part
belongs to inner if.
If you want to make the
else part for the outer if then you need to use braces
int a=5,b=6,c=7;
if(a>b)
{
{
if(a>c)
System.out.println("A is greatest");
}
else
System.out.println("Hello world");
Comments
Post a Comment