C Programming - Control Instructions - Discussion
Discussion Forum : Control Instructions - Point Out Correct Statements (Q.No. 4)
4.
Which of the following statements are correct about an if-else statements in a C-program?
1: | Every if-else statement can be replaced by an equivalent statements using ?: operators |
2: | Nested if-else statements are allowed. |
3: | Multiple statements in an if block are allowed. |
4: | Multiple statements in an else block are allowed. |
Discussion:
25 comments Page 1 of 3.
Vrushabh said:
8 years ago
I think all four options are correct. because we can replace every if-else with ?: even nested ones also.
eg. if(a==5){
if(b==5){
printf("%d",b);
}
else{
printf("%d",b+1);
}
}
else{
printf("%d',a);
}
It can be replaced by.
(a==5)?(b==5?printf("%d",b):printf("%d",b+1)):printf("%d',a);
eg. if(a==5){
if(b==5){
printf("%d",b);
}
else{
printf("%d",b+1);
}
}
else{
printf("%d',a);
}
It can be replaced by.
(a==5)?(b==5?printf("%d",b):printf("%d",b+1)):printf("%d',a);
(4)
Hemanth kumar.v said:
7 years ago
Actually, ?: this indicates that conditional operator,we use this ?: as an alternative for if-else functions limitedly.
(1)
Noel said:
7 years ago
I disagree with the answer because one can write nested ternary operators equating nested if else statements.
(1)
Shalin said:
8 years ago
return x>y?1:0;
It will work.
I think question is misprinted, first one is correct
Tell me a single example which cannot be converted.
It will work.
I think question is misprinted, first one is correct
Tell me a single example which cannot be converted.
(1)
Debendra sahoo said:
1 decade ago
Here all options are correct, we can replace if else by conditional operator like:
if(a>b)
printf("hello");
else
printf("India");
Same can be implemented a>b? printf("hello") : printf("India").
Rest all options can be also implemented. So all are correct.
if(a>b)
printf("hello");
else
printf("India");
Same can be implemented a>b? printf("hello") : printf("India").
Rest all options can be also implemented. So all are correct.
Panchangam Chidrupi said:
7 years ago
What is the difference between = and == operators? Please tell me.
Pawan kumar saini said:
9 years ago
We can't use return statements in ?: whereas we can use it in if else blocks.
So option D is true.
So option D is true.
Gayatri Walmik said:
9 years ago
Can anyone please explain why the first option is incorrect?
Gayatri Walmik said:
9 years ago
We can use ?: for nested if else statements too. Like if (x>5? (x>10?x=1:x=2) :x=3).
What does it mean by multiple if else statements?
What does it mean by multiple if else statements?
Nehal said:
9 years ago
One more thing that we cannot use return in conditional statement.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers