Conditional Statements
Conditional Statements
Outline
1. Conditional Statements
2
Conditional Statements
❑A conditional statement allows us to choose which
statement will be executed. So, it is sometimes called as
selection statement.
❑ Conditional statement gives us the power to make decision.
❑ Java has the following conditional statements:
❖ If
❖ if-else
❖ nested-if
❖ if-else-if
❖ switch-case
❖ jump – break, continue, return
3
S
if Statement
❑ if statement is the most simple decision making statement.
❑ Syntax:
if(condition)
{
Statement 1;
Statement 2;
}
4
S
if Statement (Cont…)
❑ if is a Java reserved word.
❑ The condition must be a Boolean expression. It must
evaluate to either true or false.
❑ If we do not give the curly braces ‘{’ and ‘}’ after
if(condition), then, by default if statement considers the
immediate statement inside its block. Example:
if(condition)
statement 1; //Statement 1 will be executed
statement 2;
5
if Statement (Cont…)
6
S
if Statement (Cont…)
7
S
if Statement (Cont…)
❑ Output
20 is greater than 18
8
S
if Statement (Cont…)
9
S
if Statement (Cont…)
❑ Output
x is greater than y
10
S
else Statement
❑ Use the else statement to specify a block of code to be executed,
if the condition is false.
❑ Syntax:
if(condition)
{
Statement 1; // block of code to be executed, if the condition is true
}
else
{
Statement 2; // block of code to be executed, if the condition is false
}
11
S
else Statement (Cont…)
12
S
else Statement (Cont…)
❑ Output
Good evening.
13
S
else-if Statement
❑ Use the else if statement to specify a new condition if the
first condition is false.
❑ Syntax:
if(condition1)
{
Statement 1; // block of code to be executed, if the condition1 is true
}
else if (condition 2)
{
Statement 2; // block of code to be executed, if the condition1 is false and condition2is true
}
else
{
Statement 3; // block of code to be executed, if the condition1 is false and condition2 isfalse
}
14
S
else-if Statement (Cont…)
15
S
else-if Statement (Cont…)
❑ Output
Good evening.
16
S
Switch Statement
❑ The switch statement is a multi-way branch statement.
❑ It provides an easy way to dispatch execution to different
parts of code based on the value of the expression.
❑ The switch expression is evaluated once.
❑ The value of the expression is compared with the values of
each case.
❑ If there is a match, the associated block of code is executed.
❑ The break and default keywords are optional.
17
S
Switch Statement (Cont…)
❑ Syntax:
switch(expression)
{
case x:
Statement 1; // code block
break;
case y:
Statement 2; // code block
break;
default:
Statement 3; // code block
}
18
S
Switch Statement (Cont…)
19
S
Switch Statement (Cont…)
❑ Break Keyword
❖ When Java reaches a break keyword, it breaks out of the switch
block.
❖ This will stop the execution of more code and case testing inside the
block.
❖ When a match is found, and the job is done, it's time for a break.
20
S
Switch Statement (Cont…)
❑ Default Keyword
21
S
Switch Statement (Cont…)
❑ Some important rule:
❖Duplicate case values are not allowed.
❖The value for a case must be of the same data type as the variable in the
switch.
❖The value for a case must be a constant or a literal. Variables are not
allowed.
❖The break statement is used inside the switch to terminate a statement
sequence.
❖The break statement is optional. If omitted, execution will continue on
into the next case.
❖The default statement is optional and can appear anywhere inside the
switch block. In case, if it is not at the end, then, a break statement must
be kept after the default statement to omit the execution of the next case
statement.
22
S
Switch Statement (Cont…)
public class MyClass {
public static void main(String[] args)
{
int day = 4;
switch (day)
{
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Wednesday");
break;
case 3:
System.out.println("Thursday");
break;
case 4:
System.out.println("Friday");
break;
case 5:
System.out.println("Saturday");
break;
case 6:
System.out.println("Sunday");
break;
} 23
}}
S
Switch Statement (Cont…)
❑ Output
Friday
24
S
Switch Statement (Cont…)
25
S
Switch Statement (Cont…)
❑ Output
Looking forward to the Weekend
26