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.
Vijaykumar b said:
1 decade ago
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.
So all the options are correct.
Sridhar said:
1 decade ago
Simple in under one if statement we can declare many if and else satements.
Sindhu said:
1 decade ago
We can replace an if-else structure with a ternary operator. Isn't it so?
Gayathri.M said:
1 decade ago
'?:' can be used instead of single if else statement and you can't use it in multiple if else statements..:)
Raju royal said:
1 decade ago
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;
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:
1 decade ago
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:");
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:
1 decade ago
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.
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:
1 decade ago
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:
1 decade ago
@Madhu.
You are right for this case but many case like (return X) in if condition so ?: is not allowed if condition only.
You are right for this case but many case like (return X) in if condition so ?: is not allowed if condition only.
Gautam said:
1 decade ago
#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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers