Answer The Following Questions: Name The Different Ways To Manage The Flow of Control in A Program
Answer The Following Questions: Name The Different Ways To Manage The Flow of Control in A Program
Question 1
Answer
Question 2
Answer
if-else statement
Answer
switch statement
Question 3
(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
Answer
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
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
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
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
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) {
Question 10
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:
Question 11
Answer
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
It is a multiple
It is a bi-directional flow of
branching flow of
control statement
control statement