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.
Else if and if else
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
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?
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.
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.
yes my doubt was cleared . thankyou very much