Conditional Statements
● Control flow statements, change or break the flow of execution by
implementing decision making, looping, and branching your program to
execute particular blocks of code based on the conditions.
● There are 3 types of control flow statements supported by the Java
programming language.
● Decision-making statements : if-then, if-then-else, switch
● Looping statements : for, while, do-while
● Branching statements : break, continue, return
DEFINITION
Decision making structures have one or more conditions to be evaluated or
tested by the program, along with a statement or statements that are to be
executed if the condition is determined to be true, and optionally, other
statements to be executed if the condition is determined to be false.
Decision-making statements : if-then, if-then-else, switch.
DECISION MAKING
IF STATEMENT:
If statement consists a condition, followed by statement or a set of
statements as shown below:
if(condition){
Statement(s);
}
The statements gets executed only when the given condition is true. If the
condition is false then the statements inside if statement body are
completely ignored.
IF STATEMENT
public class IfStatementExample
{
public static void main(String args[])
{
int num=70;
if( num < 100 )
{ /* This println statement will only execute, * if the above condition
is true */
System.out.println("number is less than 100");
}
}
}
EXAMPLE-IF STATEMENT
IF ELSE STATEMENT:
This is how an if-else statement looks:
if(condition) {
Statement(s);
}
else {
Statement(s);
}
The statements inside “if” would execute if the condition is true, and the
statements inside “else” would execute if the condition is false.
IF -ELSE
public class IfElseExample
{ public static void main(String args[])
{
int num=120;
if( num < 50 )
{
System.out.println("num is less than 50");
}
else
{
System.out.println("num is greater than or equal 50");
}
}
}
EXAMPLE-IF-ELSE
NESTED IF STATEMENT IN JAVA:
When there is an if statement inside another if statement then it is called
the nested if statement.
if(condition_1) {
Statement1(s);
if(condition_2) {
Statement2(s);
}
}
Statement1 would execute if the condition1 is true. Statement2 would only
execute if both the conditions( condition1 and condition2) are true.
NESTED IF
public class NestedIfExample
{
public static void main(String args[])
{
int num=70;
if( num < 100 )
{ System.out.println("number is less than 100");
if(num > 50)
{
System.out.println("number is greater than 50");
}
}
}
}
EXAMPLE-NESTED IF
IF-ELSE-IF STATEMENT IN JAVA:
if-else-if statement is used when we need to check multiple conditions.It is
also known as if else if ladder. in if-else-if statement, as soon as the
condition is met, the corresponding set of statements get executed, rest
gets ignored. If none of the condition is met then the statements inside
“else” gets executed. if(condition_1) {
Statement1(s);}
else if(condition_2) {
Statement2(s);}
else{
Statement2(s);
}
}
IF-ELSE-IF
public class IfElseIfExample
{ public static void main(String args[])
{
int num=1234;
if(num <100 && num>=1)
{ System.out.println("Its a two digit number"); }
else if(num <1000 && num>=100)
{ System.out.println("Its a three digit number"); }
else if(num <10000 && num>=1000)
{ System.out.println("Its a four digit number"); }
else if(num <100000 && num>=10000)
{ System.out.println("Its a five digit number"); }
else { System.out.println("number is not between 1 & 99999");
}
}
}
EXAMPLE-NESTED IF
SWITCH CASE STATEMENT:
Switch case statement is used when we have number of options (or
choices) and we may need to perform a different task for each choice.
The syntax of Switch case statement looks like this –
switch (variable or an integer
expression)
{
case constant:
//Java code
;
case constant:
//Java code
;
default:
//Java code
; SWITCH STATEMENT
}
Which of these selection statements test only for equality?
A. If
B. Switch
C. If&switch
D. None of the above
Answer: B
QUESTION :01
Which of these are selection statements in Java?
A. If()
B. For()
C. Continue
D. break
Answer: A
QUESTION:02
Predict the output?
class selection_statements
{
public static void main(String args[])
{ A. 1
int var1 = 5;
int var2 = 6;
B. 2
if ((var2 = 1) == var1)
System.out.print(var2);
C. 3
else
D. 4
System.out.print(++var2);
}
}
Answer: B
QUESTION:03
Predict the output
if(a<=0)
{ A. 1 2
if(a==0)
{ B. 2 3
System.out.println("1 ");
} C. 1 3
else
{ D. 3
System.out.println("2 ");
}
} Answer : D
System.out.println("3 ");
QUESTION:04
Predict the output
class Test {
public
static void main(String[] args)
{ A. Hello
boolean b = true;
if (b = false) {
B. Bye
System.out.println("HELLO");
} else {
C. Compilation Error
System.out.println("BYE"); D. No output
}
}
}
Answer : B
QUESTION:05
Predict the output
class Test {
public
static void main(String[] args)
{ A. Compile time error
int x = 10;
if (++x < 10 && (x / 0 > 10)) {
B. RuntimeException:Arit
System.out.println(“Ram");
} else {
hmeticException: / by
System.out.println(“Rahim"); zero
}
} C. Ram
}
D. Rahim
Answer : D
QUESTION:06
Predict the output
public
class Test {
public
static void main(String[] args) A. HELLO
{
int a = 10, b = 20;
B. WELCOME
if (a < b) {
if (a > b) {
C. Compile time error
System.out.println("HELLO"); D. HELLO WELCOME
} else {
System.out.println("WELCOME");
}
}
}
}
Answer : B
QUESTION:07
………………….. statement provides an easy way to dispatch execution to
different parts of your code based on the value of an expression.
A. if-else
B. switch
C. If
D. while
Answer : B
QUESTION:08
State whether the following statements about switch statement are correct.
i) Switch statement often provides a better alternative than a large series of
if-else-if statements.
ii)The break statement is used inside the switch to terminate a statement
sequence.
A. True,False
B. False,True
C. True,True
D. False,False
Answer : C
QUESTION:09
The conditional statement, ………………. can only test for equality,
whereas ………………. can evaluate any type of Boolean
expression.
A. if,switch
B. Switch,if
C. while,if
D. if,while
Answer : B
QUESTION:10
Predict the output
int a=15;
int b=25; A. Error
if ((a<b) || (a=5)>15)
B. 15
system.out.println(a);
else C. 25
system.out.println(b);
D. No output
Answer : B
QUESTION:11
Predict the output
int x, y;
x=15; A. Error
y=20;
B. y is 20
if (x>15)
if(y>15) C. x is 15
{
D. No output
system.ptintln(“y is “+y);
}
else
system.out.ptintln(“x is “+x);
Answer : C
QUESTION:12
State whether the following statements about switch statement are
True or False.
i) No two case constants in the same switch can have identical
values.
ii) A switch statement is usually more efficient than a set of nested ifs.
A. True,False
B. False,True
C. True,True
D. False,False
Answer : C
QUESTION:13
Predict the output
int x=20;
int y=10; A. Error
if(x>y)
B. x is 20
{
if (y>10) C. y is 10
system.out.println(“y is “+y);
D. No output
}
else
system.out.println(“x is “+x);
Answer : D
QUESTION:14
Predict the output
Here is a segment of a program
x=1; A. x=1,y=1
y=1;
B. x=0,y=2
if(n>0)
x=x+1; C. x=2,y=1
y=y-1;
D. x=2,y-0
what will be the values of x and y if n=1.
Answer : D
QUESTION:15