If block

if statement in Java programming language

If statement in Java programming language

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.

If statements in Java

Three type of “if” statements are mainly available in Java:

  • if statements
  • if else statements
  • if else if else statements

if statement in Java programming

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

Flowchart of if statement

Flow diagram of if – else statement in Python

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.

 

How  if statement works in Java

the flow of control of if in Java

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

 

 

if – else statement

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
}

flow chart of if else statement

Flow diagram if – else in Java

How   if -else  statement works in Java

flow of control of if else

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.

 

if else if else statement

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
}

Flowchart of if -else if -else statement

flow diagram if-else-if else

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

How   if -else  statement works in Java

The flow of control if else if

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 “ifis 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 keyword in Java language

else keyword in Java language

 

Similar post

If statements in C language

If statements in C++ language

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

 

Suggested for you

for loop in Java Language

for loop in C Language

for loop in Python Language

for loop in C++ Language

 

While loop in Java Language

While loop in C Language

While loop in Python Language

 

Nested while loop in Java

Nested while loop in C++

Nested while loop in C

Nested while loop in Python

 

 

if statement in CPP programming language
switch case statement in C language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

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

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 days ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago