Java If / If Else Statement In Java With Simple Example: In this article, we are going to learn how to use two different conditional statements in java. That is if and if….else, and also we see the control flow of the program execution when we are trying to execute some set of statements based on some condition, that time, we have to use the control flow statements.
Java Programs Structure
Let’s take an example of a boy’s age is greater then 18 we want to print “Major” but if his age is less than 18 then want to write “Minor”. In this case, if we’re going to print Major or minor based on the input value at that time, we have to use control flow statements.
Control Statements In Java
In this core Java tutorial post, we will see the different types of control flow statements used in the Java program as per the requirement. In Java There are mainly four types of control flow statements are present; that is:
If Statement Java
In the if statements, it consists of the keyword if with a condition, and also followed by a single statement or set of statements as shown below:
If (condition) { statement(s); }
As you see in the above syntax, the statement is only executed when the condition becomes True. If the condition gets false, then the execution completely ignores the statement body.
package java_Basics; public class If_Example { public static void main(String[] args) { int number = 99; if (number > 0) { System.out.println("Number is positive."); } System.out.println("This statement is always executed."); } }
Output:
Number is positive. This statement is always executed.
Nested if statement in Java
When we found an if statement inside another if statement then that is called Nested if statement and the syntax is something looks like this:
if(condition_1) { Statement1(s); if(condition_2) { Statement2(s); } }
If condition_1 is true, then the execution enters into the method body area and execute the statements, after that, it will check for the condition_2. If that also true then the execution will enter into the Nested if method body and execute the statement.
Example Program:
package java_Basics; public class NestedIf_Example { public static void main(String[] args) { int i = 10; if (i == 10) { // First if statement if (i < 15) System.out.println("i is smaller than 15"); // Nested - if statement // Will only be executed if statement above // it is true if (i < 12) System.out.println("i is smaller than 12 too"); else System.out.println("i is greater than 15"); } } }
Output:
i is smaller than 15 i is smaller than 12 too
Nested If Else Statements Java
The if Statement executes an only certain section of code. But there also an else block present. The statement of else block will be executed when the if the condition is false. the Syntax for if-else looks like below:
How to write an if-else statement in java
if (condition) { // statement(s); } else { // statement(s); }
if-else statement in a java example program
package java_Basics; public class IfElse_Example { public static void main(String[] args) { int i = 10; if (i < 15) System.out.println("i is smaller than 15"); else System.out.println("i is greater than 15"); } }
Output:
i is smaller than 15
If – Else If In Java
The if statement executes a particular section of code if the test expression is evaluated to true. The if statement may have an optional else block. Statements inside the body of else statement are executed if the test expression is evaluated to false. The syntax for if..esle..if it looks like below:
if (condition1) { // codes } else if(condition2) { // codes } else if (condition3) { // codes } . . else { // codes }
Note: In this control flow statement one thing you have to keep in mind that, when the condition is true that time the corresponding statement will be executed and when there is none of the condition is true that time the statements which are inside in else block will be executed.
Simple Example Program For If Else Java With Example?
package com.java.Softwaretestingblog; public class IfElse_Program { public static void main(String[] args) { // TODO Auto-generated method stub int marks=80; if(marks<50) { System.out.println("fail"); } else if(marks>=50 && marks<60) { System.out.println("D grade"); } else if(marks>=60 && marks<70) { System.out.println("C grade"); } else if(marks>=70 && marks<80) { System.out.println("B grade"); } else if(marks>=80 && marks<90) { System.out.println("A grade"); } else if(marks>=90 && marks<100) { System.out.println("A+ grade"); } else { System.out.println("Invalid!"); } } }
Output:
A grade
If you found anything incorrect or you want more information on the above discussion, then you can comment on the comment section.
Leave a Reply