Suppose x and y are variables, consider the following statement:
if ( x > 5 )
y = 1;
else if ( x < 5 )
{
if ( x < 3 )
y = 2;
else
y = 3;
}
else
y = 4;
What is the value of y if x = 6?
Answer:
y = 1
Given code snippet:
if ( x > 5 )
y = 1;
else if ( x < 5 )
{
if ( x < 3 )
y = 2;
else
y = 3;
}
else
y = 4;
The above code snippet is about the if-else condition.
The if-else statement is used to perform operations depends on the specific conditions. The operations in if block will execute if and only if the given condition is satisfied. Otherwise else conditions will execute.
Here if the condition is :
if ( x > 5 )
As the given value is 6, if the condition satisfied. So the compiler won't consider remaining else statements.
The operation in if block is ; y = 1;
So y=1
Get Answers For Free
Most questions answered within 1 hours.