Conditional Statements:
When we need to execute a set of statements based on a
condition then we need to use control flow statements. For example,
if a number is greater than zero then we want to print “Positive Number” but if
it is less than zero then we want to print “Negative Number”.
There are 4 Conditional if
statements and 1 special Switch
Statement. Listed below are the conditional statements followed by its
syntax and usage.
1.
if statement
2.
nested if statement
3.
if-else statement
4.
if-else-if statement
5.
Switch case statement
1.If statement
If statement consists a condition, followed by statement or
a set of statements as shown below:
if(condition){
Statement(s);
}
Example Program:
public class IfStatementExample {
public static void
main(String args[]){
int num=99;
if( num < 100
){
/* This println statement will only execute,
* if the above condition is true
*/
System.out.println("number is less than
100");
}
}
}
2.Nested if statement in Java
When there is an if statement inside another if statement
then it is called the nested if statement.
The structure of nested if looks like this:
if(condition_1) {
Statement1(s);
if(condition_2) {
Statement2(s);
}
}
Example Program:
public class NestedIfExample {
public static void
main(String args[]){
int num=99;
if( num <
100 ){
System.out.println("number
is less than 100");
if(num >
50){
System.out.println("number is
greater than 50");
}
}
}
}
3. If else statement in Java
The statements inside “if” would execute if the condition is
true, and the statements inside “else” would execute if the condition is false.
This is how an if-else statement looks:
if(condition) {
Statement(s);
}
else {
Statement(s);
}
Example Program:
public class IfElseExample {
public static void
main(String args[]){
int num=111;
if( num < 100 ){
System.out.println("num
is less than 100");
}
else {
System.out.println("num
is greater than or equal 100");
}
}
}
4. if-else-if Statement
if-else-if statement is used when we need to check multiple
conditions. In this statement we have only one “if” and one “else”, however we
can have multiple “else if”. It is also known as if else if ladder. This is how it looks:
if(condition_1) {
/*if condition_1 is
true execute this*/
statement(s);
}
else if(condition_2) {
/* execute this if
condition_1 is not met and
* condition_2 is
met
*/
statement(s);
}
else if(condition_3) {
/* execute this if
condition_1 & condition_2 are
* not met and
condition_3 is met
*/
statement(s);
}
.
.
else {
/* if none of the
condition is true
* then these
statements gets executed
*/
statement(s);
}
Example Program:
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");
}
}
}
5. 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 Case statement is mostly used with
break.
Example Program:
class SwitchDemo {
public static void
main(String[] args) {
int month = 8;
switch (month)
{
case
1:
System.out.println("January"); break;
case
2:
System.out.println("February"); break;
case
3: System.out.println("March");
break;
case
4:
System.out.println("April"); break;
case
5: System.out.println("May");
break;
case
6: System.out.println("June");
break;
case
7: System.out.println("July");
break;
case
8:
System.out.println("August"); break;
case
9:
System.out.println("September"); break;
case 10:
System.out.println("October"); break;
case 11:
System.out.println("November"); break;
case 12:
System.out.println("December"); break;
default:
System.out.println("Invalid month.");break;
}
}
}