Conditional Construct
Section A
2005
Question 2
(b) What is a compound statement? Give an example. [2]
(e) Explain with the help of an example, the purpose of default in switch statement. [2]
Question 3
(c) Explain the meaning of break and continue statements. [3]
2006
Question 2
(d) Differentiate between if and switch statements. [2]
switch
1) Only equality checking can be done.
2) Only integer and character values can be checked.
3) Only one variable can be used to test.
if
1) Relational or logical expressions can be used.
2) Any type of value can be tested including floating point or string or boolean.
3) Various variables can be used for condition checking.
2007
Question 1
(e) Explain with an example the if else if construct. [2]
Example:
Question 2
tax = income<=10000 ? 0 : 12;
2009
Question 1
(c) What is the use and syntax of a ternary operator? [2]
The ? : is the ternary operator. It is used to select a value against a test expression.
If the test expression is true then the expression after the ? is selected else the expression after the : will be selected.
Syntax: expression1 ? expression2 : expression3;
E.g. : a>b ? a : b;
Question 2
(e) Give the output of the following code fragment:
when i) opn = ‘b’ ii) opn = ‘x’ iii) opn = ‘a’
2013
Question 3
(c) Rewrite the following program segment using the if else statement
comm=(sale>15000)? sale*5/100 : 0;
[2]
2014
Question 2
(c) Give two differences between the switch and if else statement
[2]
switch
1) Only equality checking can be done.
2) Only integer and character values can be checked.
if
1) Relational or logical expressions can be used.
2) Any type of value can be tested including floating point or string or boolean.
Question 3
(d) Rewrite the following program segment using if else statements instead of the ternary operator.
String grade = (mark>=90) ? “A” : (mark>=80) ? “B” : “C”;
[2]
2016
Question 3
(e) Rewrite the following using ternary operator:
if(x%2==0)
System.out.print("EVEN");
else
System.out.print("ODD");
[2]
System.out.print((x%2==0)? "EVEN":"ODD");
2018
Question 3
(f) Convert the following if else if construct into switch case:
If(var==1)
System.out.println(“good”);
else if(var==2)
System.out.println(“better”);
else if(var==3)
System.out.println(“best”);
else
System.out.println(“Invalid”); [2]
(i) Rewrite the following using ternary operator:
if(bill>10000)
discount=bill*10.0/100;
else
discount=bill*5.0/100;
[2]
discount=(bill>10000)?bill*10.0/100:bill*5.0/100;
2019
Question 2
(a) Differentiate beween if else if and switch case statements. [2]
switch
1) Only equality checking can be done.
2) Only integer and character values can be checked.
3) Only one variable can be used to test.
if
1) Relational or logical expressions can be used.
2) Any type of value can be tested including floating point or string or boolean.
3) Various variables can be used for condition checking.
2020
Question 3
(g) Rewrite the following program segment using logical operators:
if(x > 5)
if(x > y)
System.out.println(x+y); [2]
if(x>5 && x>y)
System.out.println(x+y);
(h) Convert the following if else if construct into switch case:
if (ch== 'c' || ch=='C')
System.out . print("COMPUTER");
else if (ch== 'h' || ch=='H')
System.out . print("HINDI");
else
System.out . print("PHYSICAL EDUCATION"); [2]
switch(ch)
{
case 'c':
case 'C':System.out.print("COMPUTER"); break;
case 'h':
case 'H':System.out.print("HINDI"); break;
deafult: System.out.print("PHYSICAL EDUCATION");
}