Conditional Statements in Java
- Categories if & switch, if & switch, Java
Here we are going to learn above Java Conditional Statements. You can watch our video on Conditional Statements in Java- Click Here
Q. 1 What is a block or compound statement?
Ans. A block is a group of one or more than one statement enclosed within the curly braces. The group statement is also a closed block and enclosed between pairs of curly braces’{}’.
Example:
{ // beginning of compound block
int a=5;
a++;
System.out.println(a);
a= a+2;
System.out.println(y);
} // close of compound block
Q. 2 What is a selection statement? Name the selection statements provided by Java.
Ans. ‘Selection’ means it is the test condition given with conditional statement which decides that which statement should be executed next.
Example:
- if()
- if()-else
- switch-case
- conditional or ternary operators (? and : )
Q. 3 What is if statement?
Ans. The if statement executes a block of code only if the specified expression is true. If the value is false, then the if block is skipped and execution continues with the rest of the program. You can either have a single statement or a block of code within an if statement.
- Conditional expression must be a Boolean expression.
- There is no ; after conditional expression
if/else statement is an extension of the if statement. If the condition is false, the statements in the else block are executed.
Syntax
if (<conditional expression>)
{
<statement/s>
}
else
{
<statement/s>
}
Q. 4 What is switch statement?
Ans. It is also called case or multiple choice statements. A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.
- The variable used in a switch statement can only be a byte, short, int, or char. (NOT FLOAT)
- break statement is commonly used at end of case statements so that flow of control jumps to the next line following the switch statement.
- If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.
Syntax:
switch(expression)
{
case value :
//Statements
break; //optional
case value :
//Statements
break; //optional
//You can have any number of case statements.
default : //Optional
//Statements
}
Q. 5 What is difference between if else and switch statement?
Ans.
If else | Switch |
---|---|
Can compare with range of values | Can set condition to compare a variable with value |
Can be used for floating type nos | Cannot be used for floating point nos |
Can compare 2 variable values | Can compare a variable with constant only |
Logical expressions can be used | Logical expressions cannot be used |
Q. 6 Define fall-through with one example.
Ans. The situation which arises in the absence of break statement within switch-case block is known as fall-through situation. In this situation the control falls on all the cases till it gets a break statement otherwise after executing all the cases the control goes out of switch- case block.
Example:
int m, x= 0;
switch( m)
{
case 0:
x+= 10;
case 1:
x += 20;
case 2:
x +=30;
System.out.println("X=" + x );
break;
case 3:
x=x+6;
System.out.println('"X=" +x);
break;
}