0% found this document useful (0 votes)
18 views6 pages

Answer The Following Questions: Name The Different Ways To Manage The Flow of Control in A Program

Chapter 7 discusses conditional statements in Java, including flow control mechanisms like if-else and switch statements. It explains constructs such as nested if, if-else-if, and the purpose of the default case in switch statements, as well as the concept of fall through. The chapter also highlights differences between switch and if-else statements, and introduces jump statements like break and continue.

Uploaded by

Soham Saha
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)
18 views6 pages

Answer The Following Questions: Name The Different Ways To Manage The Flow of Control in A Program

Chapter 7 discusses conditional statements in Java, including flow control mechanisms like if-else and switch statements. It explains constructs such as nested if, if-else-if, and the purpose of the default case in switch statements, as well as the concept of fall through. The chapter also highlights differences between switch and if-else statements, and introduces jump statements like break and continue.

Uploaded by

Soham Saha
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
You are on page 1/ 6

Ch 7 Conditional statements in Java

Answer the Following Questions

Question 1

Name the different ways to manage the flow of control in a program.

Answer

Normal flow of control, Bi-directional flow of control, Multiple branching of control

Question 2

Mention one statement each to achieve:

(a) Bi-directional flow of control

Answer

if-else statement

(b) Multiple branching of control

Answer

switch statement

Question 3

Explain the following statements with their constructs:

(a) nested if

Answer

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

Answer

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

Answer

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;

Question 4

Differentiate between if and switch statement.

Answer

Following are the difference between if and switch statement:

1. switch can only test for equality whereas if can test for any Boolean expression.
2. switch tests the same expression against constant values while if-else-if ladder can use
different expression involving unrelated variables.
3. switch expression must only evaluate to byte, short, int, char, String or an enum. if doesn’t
have such limitations.
4. A switch statement will run much faster than the equivalent program written using the if-else-
if ladder

Question 5

What is the purpose of switch statement in a program?

Answer

switch statement in a program, is used for multi-way branch. It compares its expression to
multiple case values for equality and executes the case whose value is equal to the
expression of switch. If none of the cases match, default case is executed. If default case is
absent then none of the statements from switch are executed.

Question 6

Explain with the help of an example, the purpose of default in a switch statement.

Answer

When none of the case values are equal to the expression of switch statement then default
case is executed. In the example below, value of number is 4 so case 0, case 1 and case 2 are
not equal to number. Hence the default case will get executed printing "Value of number is
greater than two" to the console.

int number = 4;

switch(number) {
case 0:
System.out.println("Value of number is zero");
break;

case 1:
System.out.println("Value of number is one");
break;

case 2:
System.out.println("Value of number is two");
break;

default:
System.out.println("Value of number is greater than two");
break;

Question 7

Is it necessary to use 'break' statement in a switch case statement? 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 8

Explain 'Fall through' with reference to a switch case statement.

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 9

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) {

System.out.println("a is less than b");


a = 10;
b = 20;
System.out.println("The value of a is " + a);
System.out.println("The value of b is " + b);

Question 10

Explain with an example the if-else-if construct.

Answer

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)


System.out.println("Fail");
else if (marks < 60)
System.out.println("C grade");
else if (marks < 80)
System.out.println("B grade");
else if (marks < 95)
System.out.println("A grade");
else
System.out.println("A+ grade");

Question 11

Name two jump statements and their use.

Answer

i)break statement, it is used to jump out of a switch statement or a loop.

ii)continue statement, it is used to skip the current iteration of the loop and start the next
iteration.
Question 12

Give two differences between the switch statement and the if-else statement.

Answer

switch if-else

switch can only test if if-else can test for any


the expression is boolean expression like
equal to any of its less than, greater than,
case constants equal to, not equal to, etc.

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

You might also like