
If statement in Java

if statement is used when you want to execute a block of code under any condition check. There is 4 kind of if block-
1. Simple if Statement :
if(test expression){
??.
?..
}
2. if-else statement:
if(test expression){
??.
?..
}
else{
?.
?
}
3. Nested If .. else: you can also put another if with in if block.
if(test expression){
if(test expression){
??.
?..
}
else{
??.
?..
}
}
else{
??.
?..
}
4. else if ladder:
if(condition1){
?.
}
else if(condition2){
?.
}
else if(condition3){
?.
}
else {
?..
}
Example:
import java.util.Scanner;
public class IfElse{
public static void main(String [] args){
int a=50,b;
Scanner scanner=new Scanner(System.in);
System.out.println("Input any number: ");
b=scanner.nextInt(); //input
if(a<b){
System.out.println("a is less than b");
}
else{
System.out.println("a is greater than b");
}
}
}
Description : For user input you can use Scanner object. It parses user input entered on the console or from a file. The work of Scanner is to break its input into separate tokens and then returns them one at a time.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.