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); } } }
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); } } }
2 Responses
[…] the previous post, we have seen if else statement. In this post, we will learn about elseif […]
[…] In the previous post, we have seen the conditional statements. […]