Conditional Construct

Section A

2005

Question 2
(b) What is a compound statement? Give an example. [2]

A set of statements enclosed in a pair of curly braces is known as compound statement.
Example:
if(a>b)
{
a++;
--b;
}

(e) Explain with the help of an example, the purpose of default in switch statement. [2]

The default in switch block is used to display a message or to do any other such actions if none of the case label is matched with the value of expression of switch statement.
Example:
char c=(char)br.read();
switch(c)
{
case ‘Y’: System.out.println(“Yes”);
break;
case ‘N’: System.out.println(“No”);
break;
default: System.out.print(“Wrong choice”);
}
If the user inputs a character other than Y or N then the default statement prints Wrong choice.

Question 3
(c) Explain the meaning of break and continue statements. [3]

The break is a statement that enables the execution to jump from a loop or body of switch that causes termination of loop or switch. In the following example the loop terminates when i becomes 5. The output will be 1 2 3 4
Example:
for(int i=1;;i++)
{
if(i==5)
break;
System.out.print(i+“ ”);
}

The continue is a statement that enables to skip the statements after the continue statement and to jump to the next updation. In the following example it prints odd numbers from 1 to 9. When the condition become true it skips the print statement and jumps to next iteration (updation).
Example:
for(int i=1; i<=10; i++)
{
if(i%2==0)
continue;
System.out.println(i);
}

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]

The if else if is a selection statement that is used when there are more conditions to be checked. If the first condition is true then a set of statement is selected; if the condition is false then another condition is checked. Such a way we can use as many conditions as required with if else if construct.
Example:
if(n>0)
System.out.println( “Positive”);
else if(n<0)
System.out.println( “Negative”);
else
System.out.println( “Zero”);

Question 2

(c) Rewrite the following using Ternary operator
if(income<=10000)
tax = 0 ;
else
tax=12; [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’

switch(opn)
{
case ‘a’: System.out.println(“Platform Independent”);
break;
case ‘b’: System.out.println(“Object Oriented”);
case ‘c’: System.out.println(“Robust and Secure”);
break;
default: System.out.println(“Wrong Input”);
} [2]

(i) Object Oriented
Robust and Secure
(ii) Wrong Input
(iii) Platform Independent

2013

Question 3
(c) Rewrite the following program segment using the if else statement
comm=(sale>15000)? sale*5/100 : 0; [2]

if(sale>15000)
comm=sale*5/100;
else
comm=0;

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]

String grade;
if(mark>=90)
grade=”A”;
else if(mark>=80)
grade=”B”;
else
grade=”C”;

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]

switch(var)
{
case 1: System.out.println("good");
break;
case 2: System.out.println("better ");
break;
case 3: System.out.println("best "); >
break;
default: System.out.println("Invalid ");
}

(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");
}