The if statement is a powerful decision making statement and used to control the flow of execution of statements.
Syntax:-
if(condition)
{
Block of Statement
}
Statement;
Note:-
# Condition can be expression or relation.
# If body of statement is only one statement than braces ({}) are not needed.
# Body of the statement may have one or more statement.
Execution:-
If condition is true than block of statement is executed otherwise statement next to if is executed.
Example:-
Program to display entered number
import java.util.Scanner;
class group{
public static void main(String arg[]){
int num1;
Scanner num = new Scanner(System.in);
System.out.println("Enter a number");
num1 = num.nextInt();
if(num1 == 0)
System.out.println("The entered nubmer is : 0");
if(num1 == 1)
System.out.println("The entered nubmer is: 1");
if(num1 == 2)
System.out.println("The entered nubmer is: 2");
if(num1 == 3)
System.out.println("The entered nubmer is: 3");
if(num1 == 4)
System.out.println("The entered nubmer is: 4");
if(num1 == 5)
System.out.println("The entered nubmer is: 5");
if(num1 > 5)
System.out.println("The entered number is greater than 5");
}
}
Syntax:-
if(condition)
{
Block of Statement
}
Statement;
Note:-
# Condition can be expression or relation.
# If body of statement is only one statement than braces ({}) are not needed.
# Body of the statement may have one or more statement.
Execution:-
If condition is true than block of statement is executed otherwise statement next to if is executed.
Example:-
Program to display entered number
import java.util.Scanner;
class group{
public static void main(String arg[]){
int num1;
Scanner num = new Scanner(System.in);
System.out.println("Enter a number");
num1 = num.nextInt();
if(num1 == 0)
System.out.println("The entered nubmer is : 0");
if(num1 == 1)
System.out.println("The entered nubmer is: 1");
if(num1 == 2)
System.out.println("The entered nubmer is: 2");
if(num1 == 3)
System.out.println("The entered nubmer is: 3");
if(num1 == 4)
System.out.println("The entered nubmer is: 4");
if(num1 == 5)
System.out.println("The entered nubmer is: 5");
if(num1 > 5)
System.out.println("The entered number is greater than 5");
}
}
Previous Code:-
Conditional operator
List Code:-
Java codes
Next Code:-
if-else statement
Java Programs:-
List of Java Programs
Conditional operator
List Code:-
Java codes
Next Code:-
if-else statement
Java Programs:-
List of Java Programs
Leave reply
Add your comments here