Else if and if else

i am confused that if instead of using else if statement in section 1 , 6th video, if we used if else statement what will be the difference.

@kalindiyadav5,

You can’t give condition in else statement.
Eg.
if(n<50){

}
else(n>50){

} always show an error to you in else statement.or there are chances u provide 50 as input .

u have to use it like
if(n<50){

}
else if(n==50){

}
else(n>50){

}

this won’t show an error.
you have to understand that if else work together on one condition . to use more than one condition we use else if.

what if we use the the syntax like:
if(){
}else{
}else{
}

instead of else if statement, what difference will it make?

@kalindiyadav5,
You can use multiple "else if " statements but only 1 " if " and " else " statements

@kalindiyadav5,

if(){
if(){
}
}
else{
if(){
}else{
}

}

you can use nested condition like the one mentioned above but only 1 else statement is allowed with an if statement

so using ,
if(){
}if(){
}
.
.
.
.
instead of
if(){
}else if(){
}

which is better? and why?

@kalindiyadav5,

Generally it depends on the question.

In the first example:
if(){
}if(){
}

In this the first ’ if ’ block will be executed if the condition is true. Similarly the second ’ if ’ block will be executed only if the condition is true. In case the first ’ if ’ condition is false, the second condition will be checked irrespective. But in case the first ’ if ’ block is true, the second ’ if ’ block will be checked.

In the second example:
if(){
}else if(){
}

In this the first ’ if ’ block will be executed if the condition is true. Similarly the ’ else if ’ block will be executed only if the condition is true. In case the first ’ if ’ condition is false, the second condition will be checked. But in case the first ’ if ’ block is true, the second ’ else if ’ block will not be checked.

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

1 Like

yes my doubt was cleared . thankyou very much