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 2 of 3.
Ritesh_iiita said:
1 decade ago
@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.
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:
1 decade ago
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 ".
#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:
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.
Arun Kumar said:
1 decade ago
Can we multiple else statements?
If so which else statement will be executed in case failure of if statement?
If so which else statement will be executed in case failure of if statement?
Komal Jain said:
10 years ago
@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:
10 years ago
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:
9 years ago
One more thing that we cannot use return in conditional statement.
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?
Gayatri Walmik said:
9 years ago
Can anyone please explain why the first option is incorrect?
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers