Codeforcoding

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 statement in Java programming language
If statements in Java

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

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
Exit mobile version