4.java - Control Statement
4.java - Control Statement
Decision-Making statements:
As the name suggests, decision-making statements decide which statement to execute and when.
Decision-making statements evaluate the Boolean expression and control the program flow
depending upon the result of the condition provided. There are two types of decision-making
statements in Java, i.e., If statement and switch statement.
1) If Statement:
In Java, the "if" statement is used to evaluate a condition. The control of the program is diverted
depending upon the specific condition. The condition of the If statement gives a Boolean value,
either true or false. In Java, there are four types of if-statements given below.
1. Simple if statement
2. if-else statement
3. if-else-if ladder
4. Nested if-statement
The switch statement performs one of many different actions, depending on the
value of an expression.
if statement
if-else statement
if-else-if ladder
nested if statement
The if Statement
The statement that is included within it will be executed only if its condition is
true.
Example
if(age>18){
System.out.print("Age is greater than 18");
}
//Java Program to demonstate the use of if
statement.
public class IfExample {
public static void main(String[] args) {
//defining an 'age'
variable int age = 20;
//checking the age
if(age>18){
System.out.print("Age is greater than 18");
}}}
Student.java
Output:
x + y is greater than 20
The if...else statement is the if statement with an added else clause. It works the same way
as the if statement except that, when the condition is false the statement within the else
clause executes.
The if-else statement is an extension to the if-statement, which uses another block
of code, i.e., else block. The else block is executed if the condition of the if-block
is evaluated as false.
Syntax:
1. if(condition) {
2. statement 1; //executes when condition is true
3. }
4. else{
5. statement 2; //executes when condition is false
6. }
else statement 2;
if(condition){
//code if condition is true
}else{
//code if condition is false
}
EXAMPLE
//A Java Program to demonstrate the use of if-else statement.
//It is a program of odd and even number.
public class IfElseExample {
public static void main(String[]args) {
//defining a variable
int number=13;
//Check if the number is divisible by 2 or not
if(number%2==0){
System.out.println("even number");
}else{
System.out.println("odd number");
}
}
}
Output:
x + y is greater than 20
Example:
The if-else-if statement contains the if-statement followed by multiple else-if statements. In other
words, we can say that it is the chain of if-else statements that create a decision tree where the
program may enter in the block of code where the condition is true. We can also define an else
statement at the end of the chain.
The if-else-if ladder statement executes one condition from multiple statements.
Syntax of if-else-if statement is given below.
1. if(condition 1) {
2. statement 1; //executes when condition 1 is true
3. }
4. else if(condition 2) {
5. statement 2; //executes when condition 2 is true
6. }
7. else {
8. statement 2; //executes when all the conditions are false
9. }
OR Syntax:
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
} else
if(condition3){
//code to be executed if condition3 is true
} ..
.
else{
//code to be executed if all the conditions are false
}
EXAMPLE
Student.java
Output:
Delhi
Program to check POSITIVE, NEGATIVE or ZERO:
int number=-13;
if(number>0){
System.out.println("POSITIVE");
}else if(number<0){
System.out.println("NEGATIVE");
}else{
System.out.println("ZERO");
}
}
}
The nested if statement represents the if block within another if block. Here, the inner
if block condition executes only when outer if block condition is true.
if(condition){
//code to be executed
if(condition){
//code to be executed
}}
Student.java
Output:
Delhi
JAVA If Else
JAVA if else statement is used to test condition. There are various ways to use if statement in
JAVA.
• If
• if-else
• if-else-if
• nested if
(A) JAVA If Statement
1. if(condition) {
2. //code to be executed
3. }
Flowchart
1.Example:
Output:
Example2:
Use of simple ‘if’ statement
The following code shows the simple use of the ‘if’ statement. The first ‘if’ condition
checks the value of the number is more than 50 or not. The second ‘if’ condition checks
the length of a string is less than 6 or not.
Example3:
Syntax:
1. if(condition) {
2. //code to be executed
3. if(condition) {
4. //code to be executed
5. }}
Flow Chart
Example1:
Example2:
Syntax:
1. if(condition) {
2. //code if condition is true
3. } else {
4. //code if condition is false
5. }}
Flowchart
Example1
public class if1 {
public static void main(String[] args) {
int num =12;
Output:
even number
Example2
1. //A Java Program to demonstrate the use of if-else statement.
2. //It is a program of odd and even number.
3. public class If Else Example {
4. public static void main (String [] args) {
5. //defining a variable
6. int number=13;
7. //Check if the number is divisible by 2 or not
8. if(number%2==0) {
9. System.out.println("even number");
10. } else {
11. System.out.println("odd number");
12. }}}
Output:
odd number
OR
public class{
public static void main(String [] args){
else if(marks>=600)&&(marks<=70)){
Example:
Output:
Odd number
Syntax:
1. if(condition1) {
2. //code to be executed if condition1 is true
3. } else if(condition2) {
4. //code to be executed if condition2 is true
5. }
6. else if(condition3) {
7. //code to be executed if condition3 is true
8. }
9. else {
10. //code to be executed if all the conditions are false
11. }
Flowchart1 Flowchart2
Example1:
//Java Program to demonstrate the use of If else-if ladder.
//It is a program of grading system for fail, D grade, C grade, B grade, A grade and
A+.
public class IfElseIfExample {
public static void main(String [] args) {
int marks=65;
if(marks<50) {
System.out.println("fail");
}
else if(marks>=50 && marks<60) {
System.out.println("D grade");
}
else if(marks>=60 && marks<70) {
System.out.println("C grade");
}
else if(marks>=70 && marks<80) {
System.out.println("B grade");
}
else if(marks>=80 && marks<90) {
System.out.println("A grade");
}else if(marks>=90 && marks<100){
System.out.println("A+ grade");
}else {
System.out.println("Invalid!");
}}}
Output: C grade
………………………………………………………………………………………………………
Example A:
Output "Have a good morning!" if the current time is less than 10, and "Have a good day!"
if the current time is less than 20. Otherwise it will output "Have a good night!":
This is PHP programming.
<? php
$t = date("H");
if ($t < "10") {
echo "Have a good morning!";
} else if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}?>
Output
The hour (of the server) is 10, and will give the following message: