Conditional Statements in Java
State whether the following statements are 'True' or
'False'
Question 1
if statement is also called as a conditional statement. True
Question 2
A conditional statement is essentially formed by using relational operators. True
Question 3
An if-else construct accomplishes 'fall through'. False
Question 4
In a switch case, when the switch value does not match with any case then the execution
transfers to the default case. True
Question 5
The break statement may not be used in a switch statement. False
Tick the correct answer
Question 1
If m, n, p are the three integers, then which of the following holds true, if(m==n)&&(n!=p)?
1. 'm' and 'n' are equal ✓
2. 'n' and 'p' are equal
3. 'm' and 'p' are equal
4. none
Question 2
A compound statement can be stated as
1. p=[Link](); q=[Link]();
2. m=++a; n=--b;
3. if(a>b)
{a++;b--;} ✓
4. none
Question 3
If((p>q)&&(q>r)) then
1. q is the smallest number
2. q is the greatest number
3. p is the greatest number ✓
4. none
(where p, q and r are three integer numbers)
Question 4
if(a<b)
c=a; else
c=b;
It can be written as:
1. c= (b<a)?a:b;
2. c= (a!=b)?a:b;
3. c= (a<b)?b:a;
4. none ✓ Question 5
If(a<b && a<c)
1. a is the greatest number
2. a is the smallest number ✓
3. b is the greatest number
4. none
(where a, b and c are three integer numbers)
Write the Java expressions for the following
1) [Link](a+b)
2) (([Link](a,3))/3)+(([Link](b,3))/4)
3) s=(u*t)+((f*[Link](t,2))/2)
4) d=[Link](([Link](l,2))+([Link](b,2)))
5) ((-b)+([Link](([Link](b,2))-(4*a))))/(2*a)
6) (([Link](a,3))+([Link](b,3)))/(([Link](a,3))-([Link](b,3)))
7) [Link]((a-b)/(a+b))
8) [Link]((a+b)/(a*b))
9)
([Link]((5*([Link](x,2)))+y))/([Link]((x+(11*([Link](y,3)))),(1/4)
)
10) (x+y)/([Link]([Link](x-y)))
Predict the output
Question 1
int m=3,n=5,p=4; if(m==n
&& n!=p)
{
[Link](m*n);
[Link](n%p);
}
if((m!=n) || (n==p))
{
[Link](m+n);
[Link](m-n);
}
Output
8
-2
Explanation
The first if condition — if(m==n && n!=p), tests false as m is not equal to n. The second if
condition — if((m!=n) || (n==p)) tests true so the statements inside its code block are
executed printing 8 and -2 to the console.
Question 2
int a=1,b=2,c=3;
switch(p) { case
1: a++; case 2:
++b; break; case
3: c--;
}
[Link](a + ","
+ b + "," +c);
(i) p = 1
Output
2,3,3
Explanation
When p is 1, case 1 is matched. a++ increments value of a to 2. As there is no break
statement, fall through to case 2 happens. ++b increments b to 3. break statement in case 2
transfers program control to println statement and we get the output as 2,3,3.
(ii) p = 3
Output
1,2,2
Explanation
When p is 3, case 3 is matched. c-- decrements c to 2. Program control moves to the
println statement and we get the output as 1,2,2.
Answer the following questions
Question 1
What is meant by 'conditional' statement? Explain.
Answer
The order in which the statements of a program are executed is known as control flow. By
default, the statements of a program are executed from top to bottom in order in which they
are written. But most of the times our programs require to alter this top to bottom control
flow based on some condition. The statements that help us to alter the control flow of the
program are known as conditional statements.
Question 2
What is the significance of [Link](0)?
Answer
[Link](0) terminates the execution of the program by stopping the Java Virtual Machine
which is executing the program. It is generally used when due to some reason it is not
possible to continue with the execution of the program. For example, if I am writing a
program to find the square root of a number and the user enters a negative number then it is
not possible for me to find the square root of a negative number. In such situations, I can use
[Link](0) to terminate the program.
Question 3
Is it necessary to include 'default' case in a switch statement? Justify.
Answer
'default' case is optional in a switch statement. It is included to takecare of the situation when
none of the case values are equal to the expression of switch statement.
Question 4
What will happen if 'break' statement is not used in a switch case? Explain.
Answer
Use of break statement in a switch case statement is optional. Omitting break statement will
lead to fall through where program execution continues into the next case and onwards till
end of switch statement is reached.
Question 5
When does 'Fall through' occur in a switch statement? Explain.
Answer
break statement at the end of case is optional. Omitting break leads to program execution
continuing into the next case and onwards till a break statement is encountered or end of
switch is reached. This is termed as Fall Through in switch case statement.
Question 6
What is a compound statement? Give an example.
Answer
Two or more statements can be grouped together by enclosing them between opening and
closing curly braces. Such a group of statements is called a compound statement.
if (a < b) {
/*
* All statements within this set of braces
* form the compound statement
*/
[Link]("a is less than b");
a = 10; b = 20;
[Link]("The value of a is " + a);
[Link]("The value of b is " + b);
}
Question 7
Explain if-else-if construct with an example.
Answer
if-else-if construct is used to test multiple conditions and then take a decision. It provides
multiple branching of control. Below is an example of if-else-if:
if (marks < 35)
[Link]("Fail"); else
if (marks < 60)
[Link]("C grade"); else
if (marks < 80)
[Link]("B grade"); else if
(marks < 95)
[Link]("A grade"); else
[Link]("A+ grade");
Question 8
Give two differences between the switch statement and the if-else statement.
Answer
switch if-else
switch can only test if the expression is if-else can test for any boolean expression like less
equal to any of its case constants than, greater than, equal to, not equal to, etc.
It is a multiple branching flow of control
It is a bi-directional flow of control statement
statement