if statement in java

In the previous post, we have seen the working of for each loop in java. In this post, we will look at if statement in java.

In every programming language, at some point we need to put few conditions check or verification. if statement is used to to verify the conditions, that’s why it is called conditional statement. After condition check, it evaluates boolean value like true or false. If true, it enters the if statement block and performs necessary tasks, else exit the conditional statement.

Syntax of if statement:

if(Boolean_expression)
{
   //Statements will be executed if the Boolean expression is true
}

Example of if statement:

public class ExampleIfStatement {
	public static void main(String[] args) {
		int a = 100;
		if(a<200){
			System.out.println("Value of a : "+a);
		}		
	}
}
Output: Value of a : 100

if ..else statement:

We can perform different tasks on the basis of conditions evaluation like true or false. For that we can associate else statement with if statement. 

Example of if ..else:

public class ExampleIfStatement {
	public static void main(String[] args) {
		int a = 100;
		if(a<50){
			System.out.println("Value of a is less that actual value: "+a);
		}else{
			System.out.println("Value of a is not less that actual value: "+a);
		}
	}
}
Output: Value of a is not less that actual value: 100
Questions/Suggestions
Have any question or suggestion for us?Please feel free to post in Q&A Forum

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...

2 Responses

  1. August 10, 2015

    […] the previous post, we have seen if else statement. In this post, we will learn about elseif […]

  2. August 10, 2015

    […] In the previous post, we have seen the conditional statements. […]