In this tutorial, We discuss if statement in Java programming language
In Java programming, “if statement” could be used to execute a section of code based a Boolean expression. when Boolean expression is true, ‘if“ part will be executed. when it is false, then the flow of control will exit from the “if section“. Following the exit, Statements outside of “if“ will be executed.
Three type of “if” statements are mainly available in Java:
The syntax of if statements in Java
if(test_expression) { statements //code to be executed only when condition is true } // When the condition is false, control exit from the loop
When the test expression(return Boolean value) is evaluated and it is true, codes inside the body of if statements are executed then the output will be displayed
When the test expression is evaluated and it is false, Statements inside the body of “if ” are skipped from the execution and flow of control move to the outside of if statements.
Example
Program 1
When if the statement is true:
public class if_con1{ public static void main(String args[]){ int marks=67; if (marks>=60){//evaluate test expression //if condition is true //body of if statement executes System.out.print("Your grade is B"); //if condition is false //exit from body of if statement } } }
When the above code is executed, it produces the following results:
Your grade is B
In the above program, marks are declared as 67. the test expression(marks>=60) is evaluated. if it is true, codes inside the body of if statements are executed the output will be displayed
program 2
Same conditions but when the if statement is false:
public class if_con2{ public static void main(String args[]){ int marks=57; if (marks>=60){//evaluate test expression //if condition is false //body of if statement not executes System.out.print("Your grade is B"); //exit from body of if statement } } }
When the above code is executed, it produces the following results
No output
Sometimes the if statements can have an optional part else. Typically the if statements execute a section of codes when the test expression evaluated to true. when the test expression is false, the flow of control exit from if part and codes inside the body of else statements are executed.
if else statements in Java
The syntax of if-else statements in Java
if(test_expression) { //statement(s) //code to be executed only when condition is true } else{ //when the test_expression is false //execute this statements }
program 1
class if_else{ public static void main(String args[]){ int age=20; if(age<18) { System.out.println("you can not voting"); } else{ System.out.println("you can voting"); } } }
When the above code is executed, it produces the following results:
you can voting
In the above program, age is declared as 20, when the boolean expression (age<18) is evaluated and returns false, the body of if part statements are skipped. Then the flow of control moves to else part and codes inside the body of else are executed and output will be displayed.
The syntax of if-else if-if in Java
if (Boolean_expression1){ //When the boolean expression is true, execute this part //if boolean expression is false, loop goes to next part(else if) } else if(Boolean_expression2){ //Executes when the boolean expression is true //if boolean expression is false, it goes to the next part } else(Boolean_expression3){ //Executes when the boolean expression is true //if boolean expression is false, it goes to the end }
Initially, test expression is evaluated by if part. when it returns true codes inside the body of “if ” is executed and flow of control exit from the loop. Otherwise returns false flow of control moves to the body of else if, then test expression is evaluated by else if part. when it returns true codes inside the body of else “if “ is executed. Otherwise returns false flow of control moves to the body of else and executed codes inside the body of else
Examples
Program 1
class Check_Age{ public static void main(String args[]){ int age=18; if(age<18){ System.out.println("you are a younger"); } else if(age>18){ System.out.println("you are a elder"); } else{ System.out.println("you are a teen age boy"); } } }
When the above code is executed, it produces the following results:
you are a teen age boy
In the above program, age is declared as 18. Test expression of “if ” is evaluated and returns false. statements inside the body of “if “ are skipped the execution flow of control move to else if part
then test expression of else if is evaluated and returns false. statements inside the body of “else if “ are skipped the execution flow of control move to else part
finally, else part is executed and output is displayed
program 2
class if_elseif_else{ public static void main(String args[]){ int marks=34; String grade; if(marks>=85){ System.out.println("You got merit pass"); } else if(marks>=75){ System.out.println("You got grade A"); } else if(marks>=65){ System.out.println("You got grade B"); } else if(marks>=55){ System.out.println("You got grade C"); } else if(marks>=34){ System.out.println("You got pass"); } else{ System.out.println("Try to next time"); } } }
When the above code is executed, it produces the following results:
you got pass
There are other Java language keywords that are similar to this post
If statements in Python language
Nested if statements in C language
Nested if statements in Java language
Nested if statements in C++ language
Nested if statements in Python language
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…
View Comments
If statement in java programing.
if statement in java programing
If statement is a keyword in java programming.if statement is a decision-making statement in java programming.it is the simplest form of the selection constructs. It is used to execute and skip the statement a set of a statement by checking condition.The condition is given as relational expression. If the condition is true the statement a set of statements execute after the if statement is executed.if condition is false the statement or set of statement after if the statement is executed.The preceding program displays a message such as 6+2 is false if you choose these answers it is wrong.You have used the selection statement to carry out the minor change.This section introduced selection statement.java has several types of selection statement but on this page, we study one way of the if statement.
Syntax of if statement.
If(condion)
{
Statement..1
Statement..2
Statement..3
}
example
If (radius>=0){
Area= radius*radius+pi
System.out.printin(“the area for the circle of radius “ +radius “is” area);}
Flowchart
Limitation of if statement.
If a statement is the simple selection structure but it is very limited in its use.
The statement or set of statement is executed if the condition is true but if the condition is the false the nothing is happening.
A user may went for
Execute one statement or set of statement if the condition is true.
Execute the other statement and set of statement if the condition is false.
For example
Imort java.until.Scanner;
Public class SimpleDemo{
Public static void main(string[]args)
{
Scanner input= new scanner(system.in);
System.out.printin(“enter an integers:”);
Int number=input.nextint();
If(number%5==0)
System.out,printin(“hivi”)
If(number%2==0)
System.out.printin(“hivin”);
}}
Out put
if statement in java programing