DEV Community

Ilakkiya
Ilakkiya

Posted on

if,if else..,nested if statement:

1.Java if statement:

The if statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e. if a certain condition is true then a block of statements is executed otherwise not.

Syntax:

if(condition)  { 

  // Statements to execute if   

 // condition is true 


}
Enter fullscreen mode Exit fullscreen mode

Flowchart:

Image description

Example:

public class Demo
{
  public static void main(String args[])
   {
    int i = 10;
    if (i < 15)
        System.out.println("10 is less than 15");
   }
}
output:10 is less than 15
Enter fullscreen mode Exit fullscreen mode

2.Java if else statement:

The if-else statement helps you to run a specific block of a program if the condition is true or else, it will check other conditions.

Syntax:

If(condition){

// code if condition is true

}else{

//code if condition is false

}

Flowchart:

Image description

Example:

public class Age
{
public static void main(String args[])
{

int age=21;
if(age>22)
{
System.out.println("Idhaya's age is 21");
}
else
{
System.out.println("Ilakkiya's age is 21");
}
}
}
output:Ilakkiya's age is 21

3.Java Nested if statement:

Nested if in Java refers to having one if statement inside another if statement. If the outer condition is true the inner conditions are checked and executed accordingly.

Syntax:

if (condition1) {

    if (condition2) {

        if (condition3) {

            // statements;


     }
   }
 }
Enter fullscreen mode Exit fullscreen mode

Flowchart:

Image description

Example:
public class Equal
{
public static void main(String args[])
{
int a = 10;
int b = 20;
if (a == 10)
{
if (b == 20)

{
System.out.println("satisfied");
}
}
}
}
output:satisfied

TASK:
1.#Java program for if statement:

public class Mark
{
public static void main(String args[])
{
int mark=50;
if(mark>35)
System.out.println("passed");
}
}
output: passed

python program for if statement:

mark=50
if(mark>35):
print("passed")
Output: passed

2.#Java program for if else statement:

public class Fruit
{

public static void main(String args[])
{
int orange=100;
if(orange>200)
{
System.out.println("will not buy");
}
else
{
System.out.println("will buy oranges");
}
}
}
Output: will buy oranges

python program for if else statement:

orange=100
if(orange>200):
print("will not buy")
else:
print("will buy oranges")
Output:
will buy oranges

Java program for nested if statement:

public class Calculate
{
public static void main(String args[])
{
int a = 10;
int b = 20;
if (a==10)
{
if (b>10)
{
System.out.println("true");
}
else
{
System.out.println("false");
}
}
}
}

output:true

python program for nested if statement:

a = 10
b = 20
if a == 10:
if b > 10:
print("true")
else:
print("false")

output:true

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay