C Programming - Control Instructions - Discussion

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.

[A]. 1 and 2
[B]. 2 and 3
[C]. 1, 2 and 4
[D]. 2, 3, 4

Answer: Option D

Explanation:

No answer description available for this question.

Vijaykumar B said: (Mar 4, 2011)  
Option 3. Multiple statements in an if block are allowed - is also correct, because we can execute multiple statements against true value of if (condition) by placing the statements within { and }.

So all the options are correct.

Sridhar said: (Apr 30, 2011)  
Simple in under one if statement we can declare many if and else satements.

Sindhu said: (Jul 23, 2011)  
We can replace an if-else structure with a ternary operator. Isn't it so?

Gayathri.M said: (Feb 6, 2012)  
'?:' can be used instead of single if else statement and you can't use it in multiple if else statements..:)

Raju Royal said: (Feb 26, 2012)  
Conditional operator can be used for multiple if-else statements..

ex:
using if-else:-

if(x!=40)
if(x<40)
salary=4*x+100;
else
salary=4.5*x+150;
else
salary=300;

Using conditional operator:-

salary=(x!=40)?((x<40)?(4*x+100):(4.5*x+150)):300;

Madhu said: (Mar 1, 2012)  
In first option there sholud be a small mistake i.e if else statement we can insert many statements under if and else using {}.

Where as in ?: operation we can insert only one statement after ? and also :

Sample Program:

#include<stdio.h>
int main()
{
int x=10;
if(x>5)
{
printf("this is integer number:");
printf("this number is greater then5");
printf("\n");
}
else if(x==5)
{
printf("this is an integer number:");
printf("the number is equl to 5:");
printf("\n");
}
else
{
printf("number is smaller than 5");
printf("\n");
}
return(0);
}

But in ?: operation we can use only one statement after the ? and :
shown below rather than using many statements in if else statements.....

(x>5)?(printf(x is gtreater than 5")):((x==5)?printf("the number is equal to 5"):printf("the number is smaller than 5:");

Mayank said: (Jul 31, 2012)  
Finally we can find only one solution using conditional statement that is to be assigned to its L-value, not more than one result as using if-else,

Ex. if(i==2)
{a=100; b=400; c=300;}
else
{a=0;b=0;c=1}

You can't write any conditional statement.

Sudheer S said: (Sep 25, 2012)  
Friends here only only if - else can be replaced by ternary operator but not more than one it is wrong so there he specified more than one if-else.

Vimal Dahduk said: (Nov 12, 2012)  
@Madhu.

You are right for this case but many case like (return X) in if condition so ?: is not allowed if condition only.

Gautam said: (Jun 29, 2013)  
#include<stdio.h>
int main()
{
int a = 10, b,c;
a >=5 ? (b=100,c=12): (b=200,c=15);
printf("%d %d\n", b,c);
return 0;
}

We may write multiple statements in this way in ?: operator. Then why option 1 is wrong? Please explain.

Ritesh_Iiita said: (Jul 22, 2013)  
@Gautam && @Madhu:

Yes gautam you are absolutely right I tried this code and it works, here the code is:

#include<stdio.h>
int main()
{
int x=10;
/*if(x>5)
{
printf("this is integer number:\n");
printf("this number is greater than 5\n");
printf("\n");
}
else if(x==5)
{
printf("this is an integer number:\n");
printf("the number is equal to 5:\n");
printf("\n");
}
else
{
printf("number is smaller than 5\n");
printf("\n");
}*/
x>5?(printf("this is integer number:\n"),printf("this number is greater then5\n")):(x==5?(printf("this is an integer number:\n"),
printf("the number is equl to 5:\n")):printf("number is smaller than 5\n"));
return(0);
}
Output:
This is integer number:
This number is greater than 5.

//So again we are at the same place where we started anyone please explain it very clearly.

Akash said: (Mar 30, 2014)  
Guys 1st option is wrong because we can not use any loop in that operator but we can use any looping statements in if else.

#include<stdio.h>
int main()
{
int i,a=5;
if(a==5)
{
a==5?for(i=0;i<5;i++){printf("namastey\n");}:printf("error");

}
}

This code will give an error "expression syntax ".

Debendra Sahoo said: (Feb 15, 2015)  
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.

Arun Kumar said: (Jul 19, 2015)  
Can we multiple else statements?

If so which else statement will be executed in case failure of if statement?

Komal Jain said: (Nov 25, 2015)  
@Akash is right, because this might be the case when we need to use a loop under if else statement, but we can't use looping with conditional operator. Only that is why first option is not correct.

Ummul said: (Feb 22, 2016)  
Option D is correct. Since there are some if statements which do not have any else part and they cannot be converted into ?: So 3 conditions are fulfilled but first condition is not fulfilled.

Nehal said: (Feb 29, 2016)  
One more thing that we cannot use return in conditional statement.

Gayatri Walmik said: (Aug 10, 2016)  
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?

Gayatri Walmik said: (Aug 10, 2016)  
Can anyone please explain why the first option is incorrect?

Pawan Kumar Saini said: (Oct 16, 2016)  
We can't use return statements in ?: whereas we can use it in if else blocks.
So option D is true.

Shalin said: (Jun 9, 2017)  
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.

Vrushabh said: (Jul 13, 2017)  
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);

Noel said: (May 29, 2018)  
I disagree with the answer because one can write nested ternary operators equating nested if else statements.

Panchangam Chidrupi said: (Sep 18, 2018)  
What is the difference between = and == operators? Please tell me.

Hemanth Kumar.V said: (Oct 8, 2018)  
Actually, ?: this indicates that conditional operator,we use this ?: as an alternative for if-else functions limitedly.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.