answersLogoWhite

0


Best Answer

Here is an example:

if (exp1) if (exp2) stmt1 else stmt2

It could mean different things, like:

if (exp1) { if (exp2) stmt1 else stmt2 }

if (exp1) { if (exp2) stmt1 } else stmt2;

if (exp1) { if (exp2) stmt1 else stmt2 } else stmt2

Well, the first is the good answer, but it can be confusing, so when in doubt use {brackets}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is dangling else in c language?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What are the disadvantages of nested if in C?

Nothing, but be careful with the dangling else's: if (cond1) if (cond2) stmt1; else stmt2; means: if (cond1) { if (cond2) stmt1; else stmt2; } not this: if (cond1) { if (cond2) stmt1; } else stmt2;


How do you prevent a dangling else?

AnswerA "dangling else" is basically a bit of ambiguity as to how an if-then-else statement will be parsed - specifically, when the else clause should end.A dangling else can look something like this:void doStuff(int a) {if(a == 10)printf("TEN");elseprintf("NOT TEN");a = 100;}The above code looks perfectly normal. If a is 10, then print "TEN" otherwise print "NOT TEN" and set a to 100.With certain languages (C/C++ and Java included), that code would not quite work as described. The way the code is indented can make someone believe that it is doing something else.According to the way C/C++ and Java would interpret the above code, it should look more like this:void doStuff(int a) {if(a == 10) {printf("TEN");}else {printf("NOT TEN");}a = 100;}Which means that no matter what, a would be set to 100 at the end of the function call, which is not how the indentation made it look in the original example.Keep in mind that both examples are identical in function if interpreted by a C/C++ or Java style compiler.AnswerThis problem arises with nested if-s:if (cond1) if (cond2) stmt1; else stmt2;It can be eitherif (cond1) { if (cond2) stmt1; else stmt2; }orif (cond1) { if (cond2) stmt1; } else stmt2;It is important to rembember that the compiler does not know which of those you meant, so it choose the first interpretation: the dangling else belongs to the inner if.


What is theRealtime use of C language?

Writing programs in it... what else it could be? its a mathematical oriented language used for efficient number crunching


Why you use if and else statement in c language program?

There is no "elseif" statement in C. You can only use "else" and "if" separately. This is a good reason for switch/case/break.


Why c language has name c why not a?

C-language was derived from B-language.

Related questions

What are the disadvantages of nested if in C?

Nothing, but be careful with the dangling else's: if (cond1) if (cond2) stmt1; else stmt2; means: if (cond1) { if (cond2) stmt1; else stmt2; } not this: if (cond1) { if (cond2) stmt1; } else stmt2;


What is else if in C programming language?

Statements. Typical usage: if (<condition>) <statement>; else <statement>;


What is dangling if else problem?

The dangling else problem occurs when an if-else construct is ambiguous, and it is unclear which if statement should be associated with the else statement. This can lead to unintended behavior in the code if not handled properly. It is recommended to use braces to explicitly define the scope of if-else blocks to avoid this issue.


Why you use else function in c language?

You cannot have a function named else, because it is a reserved word.


How do you prevent a dangling else?

AnswerA "dangling else" is basically a bit of ambiguity as to how an if-then-else statement will be parsed - specifically, when the else clause should end.A dangling else can look something like this:void doStuff(int a) {if(a == 10)printf("TEN");elseprintf("NOT TEN");a = 100;}The above code looks perfectly normal. If a is 10, then print "TEN" otherwise print "NOT TEN" and set a to 100.With certain languages (C/C++ and Java included), that code would not quite work as described. The way the code is indented can make someone believe that it is doing something else.According to the way C/C++ and Java would interpret the above code, it should look more like this:void doStuff(int a) {if(a == 10) {printf("TEN");}else {printf("NOT TEN");}a = 100;}Which means that no matter what, a would be set to 100 at the end of the function call, which is not how the indentation made it look in the original example.Keep in mind that both examples are identical in function if interpreted by a C/C++ or Java style compiler.AnswerThis problem arises with nested if-s:if (cond1) if (cond2) stmt1; else stmt2;It can be eitherif (cond1) { if (cond2) stmt1; else stmt2; }orif (cond1) { if (cond2) stmt1; } else stmt2;It is important to rembember that the compiler does not know which of those you meant, so it choose the first interpretation: the dangling else belongs to the inner if.


What is theRealtime use of C language?

Writing programs in it... what else it could be? its a mathematical oriented language used for efficient number crunching


Why you use if and else statement in c language program?

There is no "elseif" statement in C. You can only use "else" and "if" separately. This is a good reason for switch/case/break.


How do you use the word dangling in a sentence?

The climber was left dangling for his life on the edge of the cliff.She did not like the way the bracelet was dangling when she drove.Somehow, there is a sock dangling from the tree.


Why c language has name c why not a?

C-language was derived from B-language.


What is previous language of c language?

language before c language is pascal


What is C language what does it do?

C Language is First Step of Programming Language, Help for C Language you are show the correct answer


What is the antonym of dangling?

The antonym of "dangling" could be "secure" or "fixed."