+ 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.

29th May 2017, 2:06 PM
Sumanth .p
Sumanth .p - avatar
5 odpowiedzi
+ 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.
19th Jun 2017, 1:16 AM
Sumanth .p
Sumanth .p - avatar
0
provide an example code.. there can't be else statement without a if condition!
29th May 2017, 2:13 PM
Eranga
Eranga - avatar
0
I don't understand your question.
29th May 2017, 2:49 PM
Twelfty
Twelfty - avatar
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.
29th May 2017, 3:48 PM
Eranga
Eranga - avatar
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.
29th May 2017, 7:04 PM
Shah Dynasty
Shah Dynasty - avatar