If statement:
if a certain condition is true then a block of statements is executed otherwise not.
Syntax:
If (condition){
//code to be executed
}
program:public class If {public static void main(String[] args) {float a =2.2f;float b =5.4f;if(b>a){System.out.println(" B is greater than a");}}}Output:B is greater than aprogram: public class If { public static void main(String[] args) { float a =2.2f; float b =5.4f; if(b>a) { System.out.println(" B is greater than a"); } } } Output: B is greater than aprogram: public class If { public static void main(String[] args) { float a =2.2f; float b =5.4f; if(b>a) { System.out.println(" B is greater than a"); } } } Output: B is greater than a
Enter fullscreen mode Exit fullscreen mode
if-else statement:
In Java, the if-else condition is a set of rules or statements,It executes one block of code if a condition is true and another block if the condition is false.
Syntax:
If(condition){
// code if condition is true
}else{
//code if condition is false
}
**program**:public class IfElse {public static void main(String[] args) {int n = 3;if (n > 5) {System.out.println("The number is greater than 5.");}else{System.out.println("The number is 5 or less.");}}}Output:The number is 5 or less.**program**: public class IfElse { public static void main(String[] args) { int n = 3; if (n > 5) { System.out.println("The number is greater than 5."); } else { System.out.println("The number is 5 or less."); } } } Output: The number is 5 or less.**program**: public class IfElse { public static void main(String[] args) { int n = 3; if (n > 5) { System.out.println("The number is greater than 5."); } else { System.out.println("The number is 5 or less."); } } } Output: The number is 5 or less.
Enter fullscreen mode Exit fullscreen mode
The else if Statement:
Use the else if statement to specify a new condition if the first condition is false.
Syntax:
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
**program:**public class Greater{public static void main (String args[]){int no1 = 1000;int no2 = 1000;if(no1>no2){System.out.println("no1 is greater");}else if(no2>no1){System.out.println("no2 is greater");}else{System.out.println("Both are equal");}}}output:Both are equal**program:** public class Greater { public static void main (String args[]) { int no1 = 1000; int no2 = 1000; if(no1>no2) { System.out.println("no1 is greater"); } else if(no2>no1) { System.out.println("no2 is greater"); } else { System.out.println("Both are equal"); } } } output: Both are equal**program:** public class Greater { public static void main (String args[]) { int no1 = 1000; int no2 = 1000; if(no1>no2) { System.out.println("no1 is greater"); } else if(no2>no1) { System.out.println("no2 is greater"); } else { System.out.println("Both are equal"); } } } output: Both are equal
Enter fullscreen mode Exit fullscreen mode
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 of Nested if *
if (condition1) {
if (condition2) {
if (condition3) {
// statements;
}
}
}
program:public class nested if{public static void main(String args[]){int a = 10;int b = 20;// Outer if conditionif (a == 10) {// Inner if conditionif (b != 20) {System.out.println("GeeksforGeeks");}else {System.out.println("GFG");}}}}program: public class nested if { public static void main(String args[]) { int a = 10; int b = 20; // Outer if condition if (a == 10) { // Inner if condition if (b != 20) { System.out.println("GeeksforGeeks"); } else { System.out.println("GFG"); } } } }program: public class nested if { public static void main(String args[]) { int a = 10; int b = 20; // Outer if condition if (a == 10) { // Inner if condition if (b != 20) { System.out.println("GeeksforGeeks"); } else { System.out.println("GFG"); } } } }
Enter fullscreen mode Exit fullscreen mode
Task: program:1
java
public class Age{public static void main (String args[]){int age1=25;int age2=21;if(age1<age2){System.out.println("The condition is false");}else{System.out.println("The condition is true");}}}output:The condition is truepublic class Age { public static void main (String args[]) { int age1=25; int age2=21; if(age1<age2) { System.out.println("The condition is false"); } else { System.out.println("The condition is true"); } } } output: The condition is truepublic class Age { public static void main (String args[]) { int age1=25; int age2=21; if(age1<age2) { System.out.println("The condition is false"); } else { System.out.println("The condition is true"); } } } output: The condition is true
Enter fullscreen mode Exit fullscreen mode
python
age1 = 25age2 = 21if age1 < age2:print("The condition is false")elif age2 < age1:print("The condition is true")output:The condition is trueage1 = 25 age2 = 21 if age1 < age2: print("The condition is false") elif age2 < age1: print("The condition is true") output: The condition is trueage1 = 25 age2 = 21 if age1 < age2: print("The condition is false") elif age2 < age1: print("The condition is true") output: The condition is true
Enter fullscreen mode Exit fullscreen mode
java
program:2public class If {public static void main(String[] args) {float a =2.2f;float b =5.4f;if(b>a){System.out.println(" B is greater than a");}}}output:B is greater than aprogram:2 public class If { public static void main(String[] args) { float a =2.2f; float b =5.4f; if(b>a) { System.out.println(" B is greater than a"); } } } output: B is greater than aprogram:2 public class If { public static void main(String[] args) { float a =2.2f; float b =5.4f; if(b>a) { System.out.println(" B is greater than a"); } } } output: B is greater than a
Enter fullscreen mode Exit fullscreen mode
python
a = 2.2b = 5.4if b > a:print("B is greater than a")output:num is greater than or equal 120a = 2.2 b = 5.4 if b > a: print("B is greater than a") output: num is greater than or equal 120a = 2.2 b = 5.4 if b > a: print("B is greater than a") output: num is greater than or equal 120
Enter fullscreen mode Exit fullscreen mode
java
program:3public class Greater{public static void main (String args[]){int no1 = 1000;int no2 = 1000;if(no1>no2){System.out.println("no1 is greater");}else if(no2>no1){System.out.println("no2 is greater");}else{System.out.println("Both are equal");}}}output:Both are equalprogram:3 public class Greater { public static void main (String args[]) { int no1 = 1000; int no2 = 1000; if(no1>no2) { System.out.println("no1 is greater"); } else if(no2>no1) { System.out.println("no2 is greater"); } else { System.out.println("Both are equal"); } } } output: Both are equalprogram:3 public class Greater { public static void main (String args[]) { int no1 = 1000; int no2 = 1000; if(no1>no2) { System.out.println("no1 is greater"); } else if(no2>no1) { System.out.println("no2 is greater"); } else { System.out.println("Both are equal"); } } } output: Both are equal
Enter fullscreen mode Exit fullscreen mode
python
no1 = 1000no2 = 1000if no1 > no2:print("no1 is greater")elif no2 > no1:print("no2 is greater")else:print("Both are equal")output:Both are equalno1 = 1000 no2 = 1000 if no1 > no2: print("no1 is greater") elif no2 > no1: print("no2 is greater") else: print("Both are equal") output: Both are equalno1 = 1000 no2 = 1000 if no1 > no2: print("no1 is greater") elif no2 > no1: print("no2 is greater") else: print("Both are equal") output: Both are equal
Enter fullscreen mode Exit fullscreen mode
References:https://www.javatpoint.com/java-if-else
References:https://www.geeksforgeeks.org/java-if-else-statement-with-examples/
References:https://www.w3schools.com/java/java_conditions.asp
暂无评论内容