Conditional Control Structure

Question: What are conditional statements? With which commands do you implement conditional statement in Java?

Answer: 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.

Question:When are braces optional or necessary in conditional statements?

Answer: If only one statement is to be executed depending upon a condition, giving braces is optional.

Question: What are relational operators?

Answer: The relational operator allows you to test or define some kind of relation between two entities.

Question: State the difference between = and = =.

Answer: 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.

Question: What are logical operators? Explain each of them with the help of an example.

Answer: Logical Operators are used to join more than one relational expression. The following statements explains the usage of logical operators:

  • If I have a pen AND a paper it is possible for me to write. This is an example of AND join, where both the conditions need to be satisfied for the resultant to be true. Thus only when I have both, pen and paper it will be possible for me to write. Thus AND operator is used when all conditions need to be satisfied.
  • I have a pen OR a pencil it is possible for me write. This is an example of OR join, where either of the conditions need to be satisfied for the resultant to be true. Thus if I have a pen I can write or if I have a pencil still I can write. Thus OR operator is used when either of the conditions need to be satisfied.
  • If it is NOT that, I do not have a pen, it is possible for me to write. Here see how the NOT operator is used to negate a condition.

Question: What is the function of nested if?

Answer: 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.

Question: What is a conditional operator? Explain with the help of an example.

Answer: This operator is used as an alternative to if statement. The ? and : are used as conditional or ternary operator. This operator can be used to replace if else statements of the general form:

if (condition)

statement1;

else

statement2;

The above form of if can be alternatively written using ? and : as follows:

condition? statement1 : statement2;

It works in the same way as the above given form of if. That is test-condition is evaluated and if it is true, statement1 gets executed (i.e. its value becomes the value of entire expression) otherwise statement2 gets executed (i.e. its value now becomes the value of the entire expression). For

instance, the following if statement

int c;

if (a>b)

c=a;

else

c=b;

can be alternatively written as

int c;

c = ((a>b) ? a : b);


Question: Compare if and ? :.

Answer: 


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


Question: What is the function of switch statement?

Answer: A switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via a multiway branch.

Question: State the rules, which is to be followed while using switch statement.

Answer: The following rules are to be kept in mind while using switch-case statements:

  • A case statement cannot exist by itself, outside of a switch block.
  • The break statement which appears as the last statement in a case block is one of Java’s jump statements. When a break statement is encountered in the case block, program execution jumps to the line of code following the switch block i.e. outside the body of switch statement. If break statement is missing in a case block upon completion of its execution, the control goes to the following case block, this process continues until break statement is encountered or switch block ends.
  • Braces should be used to denote the start and end of the switch block.
  • Only character constants and Integer constants are used in the case constants but allows only for equality comparisons.
  • All case labels in the same switch block may have unique values.

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

Answer: The default block gets executed when none of the case matches with the switch expression.

Question: Differentiate between if and switch statements.

Answer: 

If-else
Switch case
It checks for both relational and logical operations
It checks only for equality.
I may be used with almost any data type.
It is used only with integers and characters.
Comparison may be a variable or an expression or constant with another variable, expression or constant.
Comparison is always with a variable or expression with an integer or character constant.


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

Answer: The following example shows the formation of grade depending upon the marks obtained by students in a school.

Marks Grade

90 to 100 A

70 to 89         B

50 to 69         C

30 to 49         D

below 30 E

The corresponding code for the grading system would be:

if (marks>=90)

System.out.println(“Grade A”);

else if (marks>=70)

System.out.println(“Grade B”);

else if (marks>=50)

System.out.println(“Grade C”);

else if (marks>=30)

System.out.println(“Grade D”);

else

System.out.println(“Grade E”);


In this code if someone gets 95 the condition marks > = 90 gets satisfied and Grade A gets printed. Again if marks is 85 the marks > = 70, marks > = 50 and marks > = 30 all gets satisfied but only marks > = 70 will get executed as this the first satisfying statement in the else-if ladder and therefore Grade B gets executed. Similarly if marks is 55, Grade C gets printed. Now if marks is 15, none of the above conditions gets satisfied and therefore the statement in the else part gets executed, which prints Grade E.