Follow us on Facebook

Header Ads

Welcome to JAVA POint

IF,If.....else Statement in java with Examples(JAVA)

                             IF,If.....else Statement in java with Examples

At the point when we need to execute a bunch of proclamations dependent on a condition then we need to utilize control stream explanations. For instance, in the event that a number is more noteworthy than nothing, we need to print "Positive Number" however assuming it is under nothing, we need to print "Negative Number". For this situation we have two print proclamations in the program, yet just each print articulation executes in turn dependent on the info esteem. We will perceive how to compose such sort of conditions in the java program utilizing control articulations. 

In this instructional exercise, we will see four kinds of control articulations that you can use in java programs dependent on the prerequisite: In this instructional exercise we will cover following restrictive explanations: 

a) if articulation 

b) settled if articulation 

c) if-else articulation 

d) if-else-if articulation 

On the off chance that articulation

On the off chance that articulation comprises a condition, trailed by explanation or a bunch of proclamations as demonstrated underneath: 

if(condition){ 

Statement(s); 

The assertions gets executed just when the given condition is valid. On the off chance that the condition is bogus, the explanations inside if proclamation body are totally overlooked. 

in the event that assertion stream graph 

Illustration of if explanation 

public class IfStatementExample { 

public static void main(String args[]){ 

int num=70; 

on the off chance that( num < 100 ){ 

/* This println proclamation will just execute, 

* if the above condition is valid 

*/ 

System.out.println("number is under 100"); 

Output: 

number is under 100 

Settled if explanation in Java 

When there is an in the event that assertion inside another if proclamation, it is known as the settled if explanation. 

The design of settled if resembles this: 

if(condition_1) { 

Statement1(s); 

if(condition_2) { 

Statement2(s); 

}

Statement1 would execute if the condition_1 is valid. Statement2 would possibly execute if both the conditions( condition_1 and condition_2) are valid. 

Illustration of Nested if articulation 

public class NestedIfExample { 

public static void main(String args[]){ 

int num=70; 

in the event that( num < 100 ){ 

System.out.println("number is under 100"); 

if(num > 50){ 

System.out.println("number is more noteworthy than 50"); 

Output: 

number is under 100IF,If.....else Statement in java with Examples

At the point when we need to execute a bunch of proclamations dependent on a condition then we need to utilize control stream explanations. For instance, in the event that a number is more noteworthy than nothing, we need to print "Positive Number" however assuming it is under nothing, we need to print "Negative Number". For this situation we have two print proclamations in the program, yet just each print articulation executes in turn dependent on the info esteem. We will perceive how to compose such sort of conditions in the java program utilizing control articulations. 

In this instructional exercise, we will see four kinds of control articulations that you can use in java programs dependent on the prerequisite: In this instructional exercise we will cover following restrictive explanations: 

a) if articulation 

b) settled if articulation 

c) if-else articulation 

d) if-else-if articulation 

On the off chance that articulation

On the off chance that articulation comprises a condition, trailed by explanation or a bunch of proclamations as demonstrated underneath: 

if(condition){ 

Statement(s); 

The assertions gets executed just when the given condition is valid. On the off chance that the condition is bogus, the explanations inside if proclamation body are totally overlooked. 

in the event that assertion stream graph 

Illustration of if explanation 

public class IfStatementExample { 

public static void main(String args[]){ 

int num=70; 

on the off chance that( num < 100 ){ 

/* This println proclamation will just execute, 


* if the above condition is valid 


*/ 

System.out.println("number is under 100"); 

Output: 


number is under 100 

Settled if explanation in Java 

When there is an in the event that assertion inside another if proclamation, it is known as the settled if explanation. 

The design of settled if resembles this: 

if(condition_1) { 

Statement1(s); 

if(condition_2) { 

Statement2(s); 

Statement1 would execute if the condition_1 is valid. Statement2 would possibly execute if both the conditions( condition_1 and condition_2) are valid. 

Illustration of Nested if articulation 

public class NestedIfExample { 

public static void main(String args[]){ 

int num=70; 

in the event that( num < 100 ){ 

System.out.println("number is under 100"); 

if(num > 50){ 

System.out.println("number is more noteworthy than 50"); 

Output: 

number is under 100 

number is more noteworthy than 50 

In the event that else proclamation in Java 

This is the manner by which an if-else proclamation looks: 

if(condition) { 

Statement(s); 

}

else { 

Statement(s); 

The assertions inside "if" would execute if the condition is valid, and the assertions inside "else" would execute if the condition is bogus. 

On the off chance that else stream chart

Illustration of if-else explanation 

public class IfElseExample { 

public static void main(String args[]){ 

int num=120; 

on the off chance that( num < 50 ){ 

System.out.println("num is under 50"); 

else { 

System.out.println("num is more prominent than or equivalent 50"); 

Output: 

num is more prominent than or equivalnt 50 

on the off chance that else-if Statement 

in the event that else-if explanation is utilized when we need to check different conditions.

 In this proclamation we have just one "if" and one "else", anyway we can have different "else if".

It is otherwise called if else if stepping stool. This is what it looks like: 

if(condition_1) { 

/*if condition_1 is genuine execute this*/ 

statement(s); 

else if(condition_2) 

/* execute this if condition_1 isn't met and 

* condition_2 is met 

*/ 

statement(s); 

else if(condition_3) { 

/* execute this if condition_1 and condition_2 are 

* not met and condition_3 is met 

*/ 

statement(s); 

else { 

/* if none of the condition is valid 

* at that point these assertions gets executed 

*/ 

statement(s); 

Note: The main highlight note here is that in if-else-if articulation, when the condition is met, the relating set of proclamations get executed, rest gets overlooked. 

Assuming none of the condition is met, the explanations inside "else" gets executed. 

Illustration of 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 isn't between 1 and 99999"); 

}

Output:  

number is more noteworthy than 50 

In the event that else proclamation in Java 

This is the manner by which an if-else proclamation looks: 

if(condition) { 

Statement(s); 

else { 

Statement(s); 

The assertions inside "if" would execute if the condition is valid, and the assertions inside "else" would execute if the condition is bogus. 

On the off chance that else stream chart 

Illustration of if-else explanation 

public class IfElseExample { 

public static void main(String args[]){ 

int num=120; 

on the off chance that( num < 50 ){ 

System.out.println("num is under 50"); 

else { 

System.out.println("num is more prominent than or equivalent 50"); 

Output: 

num is more prominent than or equivalent 50 

on the off chance that else-if Statement 

in the event that else-if explanation is utilized when we need to check different conditions.

 In this proclamation we have just one "if" and one "else", anyway we can have different "else if".

It is otherwise called if else if stepping stool. This is what it looks like: 

if(condition_1) { 

/*if condition_1 is genuine execute this*/ 

statement(s); 

else if(condition_2) { 

/* execute this if condition_1 isn't met and 

* condition_2 is met 

*/ 

statement(s); 

else if(condition_3) { 

/* execute this if condition_1 and condition_2 are 

* not met and condition_3 is met 


*/ 

statement(s); 

else { 

/* if none of the condition is valid 

* at that point these assertions gets executed 

*/ 

statement(s); 


Note: The main highlight note here is that in if-else-if articulation, when the condition is met, the relating set of proclamations get executed, rest gets overlooked. 

Assuming none of the condition is met, the explanations inside "else" gets executed. 

Illustration of 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 isn't between 1 and 99999"); 

Output: 


0 Comments:

Post a Comment

C language and C++ progaming