Le-8 conditional constructs practice questions
Le-8 conditional constructs practice questions
Question 1
<=
||
&&
?:✓
Question 2
int a = 1;
int b = 2;
if (a == b)
else
No output
Q4) if ((a > b) && (a > c)), then which of the following statements is true?
a compound statement ✓
an empty statement
a null statement
a void statement
i. if (x == y) if (x == z) x = 1; else y = 1; else z = 1;
Answer
if (x == y)
if (x == z)
x = 1;
else
y = 1;
else
z = 1;
Answer
if (x == y)
{
if (y == z)
x = 1;
y = 2;
else
z = 1;
Answer
if (num1 != num2)
x = 1;
y = 2;
else
x = 1;
if (num1 == num2)
z = 3;
}
Q 8)
Write an if statement to find the smallest of the three given integers using the min() method of the
Math class.
Answer
System.out.println(a);
else
System.out.println(Math.min(b, c));
(a) nested if
We can write an if-else statement within another if-else
statement. We call this nested if. It has the following syntax:
if (condition 1) {
if (condition 2) {
Statement a;
Statement b;
..
}
else {
Statement c;
Statement d;
..
}
}
else {
if (condition 3) {
Statement e;
Statement f;
..
}
else {
Statement g;
Statement h;
..
}
}
(b) if - else
if - else statement is used to execute one set of statements when
the condition is true and another set of statements when the
condition is false. It has the following syntax:
if (condition 1) {
Statement a;
Statement b;
..
}
else {
Statement c;
Statement d;
..
}
(c) if - else - if
if - else - if ladder construct is used to test multiple conditions and
then take a decision. It provides multiple branching of control. It
has the following syntax:
if (condition)
statement;
else if (condition)
statement;
else if (condition)
statement;
..
..
else
statement;