+ 2
What happens when the no of if statements is more than else statements?how do a certain else block get paired with its if block
can you help me in understanding what happens in this case? if(...) a++; if(...) b++; else c++ don't mind the logic and errors it's just an example now see when will the else gets executed either when 1st if fails or 2nd if fails.
5 Réponses
+ 2
the above case is called dangling else according to this in above cases the pairing of if and else starts from the last if and first else and next last but one if and second else and so on.
0
provide an example code..
there can't be else statement without a if condition!
0
I don't understand your question.
0
ur sample code is equal to below code.
if(){
a++;
}
if(){
b++;
else{
c++;
}
else is belongs to immediate if condition. it means second if condition in this case.
it's a best practice to use curly braces( { } ) to define the scope/blocks to avoid confusion.
0
If there are more if statements than else statements then the else statement belongs to the if statement immediately above it. In your example the else statement would belong to the second if statement.