0% found this document useful (0 votes)
6 views5 pages

Conditional Statement in Java

The document provides an overview of conditional statements in Java, detailing various constructs such as if, if-else, if-else-if, and switch statements. It explains the flow of control, relational operators, and the differences between assignment and equality operators. Additionally, it discusses advantages and disadvantages of using different conditional constructs, including examples and limitations of the switch statement.

Uploaded by

sonik89300
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Conditional Statement in Java

The document provides an overview of conditional statements in Java, detailing various constructs such as if, if-else, if-else-if, and switch statements. It explains the flow of control, relational operators, and the differences between assignment and equality operators. Additionally, it discusses advantages and disadvantages of using different conditional constructs, including examples and limitations of the switch statement.

Uploaded by

sonik89300
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CONDITIONAL STATEMENT IN JAVA

THEORY
1. Name the different ways to manage the flow of control in a program.
a. Ans: Normal flow of control, Bi-directional flow of control,
multiple branching of control

2. What are conditional statements? With which commands do you


implement conditional statement in Java?
Ans: Conditional construct are specific statements that allow us to check a
condition and execute certain parts of code depending on whether the
condition is true or false. Conditional statements in Java are : if, if- else, if –
else if –else and switch case.

3. Mention one statement each to achieve:

a) Bi-directional flow of control


Ans: if-else statement

b) Multiple branching of control


Ans: switch statement
4. What is an 'if' statement? Explain with an example.
Ans: the 'if' statement helps in selecting one alternative out of the two.
The execution of 'if' statement starts with the evaluation of condition.
The 'if' statement therefore helps the programmer to test for the
condition. General form of 'if' statement. if(expression)
statementif(marks>=80) [Link]("Grade A");
5. What are relational operators?
Ans. The relational operator allows you to test or define some kind of relation
between two entities. Examples of relational operators: >=,<=,= = etc.
6. State the difference between = and = =.
Ans. The = is an assignment operator and is used to assign a value to a
variable. The == is a relational operator and is used to check for equality.

7. What is the function of nested if?


Ans. A nested if is an if statement that is the target of another if statement.
Nested if statements means an if statement inside another if statement. This
is generally used when a condition needs to be satisfied inside another
condition.

1
8. Write one advantage and one disadvantage of using ?: in place of an
if.
Ans: Advantage: It leads to a more compact program. Disadvantage:
Nested ?: becomes difficult to understand or manage.
9. Explain with an example the if-else-if construct.
Ans: if - else - if ladder 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");

10. What is a compound statement? Give an example.


Ans: 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.
Example:
int a=10,b=5;
if(a>b)
{
[Link](“a is greates”);
}
Else
[Link](“b is greatest”);
}

11. What is the function of switch statement?


Ans: Switch Case statement is a multiple branching statement where a
particular block of statements are executed out of a number of blocks as per
the user’s choice. In this system, the control goes to a specific case and
executes the statements for the given switch value. It also includes a default
2
case. The default case executed only when the switch value doesn’t match
any of the given cases.
12. What is a control variable in a switch case?
Ans: A control variable in switch case is one which guides the control
to jump on a specified case. e.g. switch(ch), here ‘ch’ is the control
variable.
13. What is a “fall through”?
Ans: The term “fall through” refers to the way the switch statement
executes its various case sections. Every statement that follows the
selected case section will be executed unless a break statement is
encountered.

14. What is the effect of absence of break in a switch statement?


Ans: Absence of break statement in a switch statement leads to situation
called “fall through” where once a matching case is found the subsequence
case blocks are executed unconditionally
15. Write one limitation and one advantage of switch statement?
Ans: Advantage: More efficient in case a value is to be tested against a set
of constants. Disadvantage: switch can test only for quality, so for the rest of
comparisons one needs to use if-else.
16. Explain, with the help of an example, the purpose of default in a switch
statement.
Ans: The default section is an optional part of the switch statement and the
statement written under default clause are executed when no matching
case is found.
switch(n)
{
case 1: [Link]("Sunday"); break;
case 2: [Link]("Monday"); break;
case 3: [Link]("Tuesday"); break;
case 4: [Link]("Wednesday"); break;
case 5: [Link]("Thursday"); break;
case 6: [Link]("Friday"); break;
case 7: [Link]("Saturday"); break;
default : [Link]("Invalid Input");
}

3
17. Give two differences between the switch statement and the if-else
statement.
switch if-else
It results in int/char type value It results in a Boolean type value

It is a multiple branching flow of control It is a bi-directional flow of


statement control statement
It can only perform tests for equality It can perform tasks on a
relational or a logical expression

A default case is applied, if no case is No default operation


available for a given switch value.

18. Compare if and ? :


If-else Ternary Operator

It is used when complex logic It is used to offer more concise,


handling is necessary. clean and
compact code
It can have multiple statements, It produces an expression, and
multiple assignments and hence a single value can, be
expressions (more than one assigned or incorporated into a
statements) in its body. larger expression.
Nested if statement is simple and Ternary operator in its nested form,
fairly easy to understand. it becomes complex and difficult to
understand.

19. What are the limitations of using break in switch case?


Ans: i) Break statement can be used only at the end of a case statement.
ii) No other statement of a specific case can be written after the break
statement.
iii) All the case of the switch block except the last case must end with
break.

4
iv) The last case does not include break because the control automatically
comes out of the switch block after execution.

20. Discuss when does an if statement prove more advantageous then switch
statement.
Ans: In the following case if statement proves to be more advantage over switch
statement: (i) When a range of values need to be tested for. (ii) When relation
between multiple variables needs to be tested. (iii) When multiple conditions
need to be tested. (iv) When expressions having a data type other then integer
or character need to be tested.

You might also like