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
}
Flowchart:
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
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:
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;
}
}
}
Flowchart:
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
Top comments (0)