0% found this document useful (0 votes)
23 views23 pages

4.java - Control Statement

Uploaded by

robsonchungu5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views23 pages

4.java - Control Statement

Uploaded by

robsonchungu5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

JAVA CONTROL STATEMENT

1.7 CONTROL STATEMENTS

Java Control Statements | Control Flow in Java


Java compiler executes the code from top to bottom. The statements in the code are executed
according to the order in which they appear. However, Java provides statements that can be used
to control the flow of Java code. Such statements are called control flow statements. It is one of
the fundamental features of Java, which provides a smooth flow of program.

Java provides three types of control flow statements.

1. Decision Making statements


o if statements
o switch statement
2. Loop statements
o do while loop
o while loop
o for loop
o for-each loop
3. Jump statements
o break statement
o continue 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

Let's understand the if-statements one by one.

JAVA‟S SELECTION STATEMENT

 Java has three types of selection statements

 The if statement either performs (selects) an action, if a condition is true, or skips


it, if the condition is false.

 The if…else statement performs an action if a condition is true and performs a


different action if the condition is false.

 The switch statement performs one of many different actions, depending on the
value of an expression.

 The if statement is a single-selection statement because it selects or ignores a


single action or a single group of actions.

 The if…else statement is called a double-selection statement because it selects


between two different actions (or groups of actions).

 The switch statement is called a multiple-selection statement because it selects


among many different actions (or groups of actions).

Java If-else Statement


The Java if statement is used to test the condition. It checks Boolean condition: true or
false. There are various types of if statement in java.

 if statement

 if-else statement

 if-else-if ladder
 nested if statement

The if Statement

 The if statement allows for conditional execution.

 The statement that is included within it will be executed only if its condition is
true.

The syntax for the if


statement is if (condition)
statement;
if(condition){
//code to be executed
}
where condition is a boolean expression. A boolean expression is an
expression whose value has type boolean.

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

1. public class Student {


2. public static void main(String[] args) {
3. int x = 10;
4. int y = 12;
5. if(x+y > 20) {
6. System.out.println("x + y is greater than 20");
7. }
8. }
9. }

Output:

x + y is greater than 20

THE IF…ELSE STATEMENT

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. }

1. The syntax for the if... else statement is


if (condition) statement 1;

else statement 2;

2. The syntax for the if else statement is

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");
}
}
}

1. public class Student {


2. public static void main(String[] args) {
3. int x = 10;
4. int y = 12;
5. if(x+y < 10) {
6. System.out.println("x + y is less than 10");
7. } else {
8. System.out.println("x + y is greater than 20");
9. }
10. }
11. }

Output:

x + y is greater than 20

Using Conditional /Ternary Operator


Java includes a special ternary operator, called the conditional operator, We can also
use ternary operator (? :) to perform the task of if...else statement. It is a shorthand
way to check the condition. If the condition is true, the result of? is returned. But, if
the condition is false, the result of: is returned.

Its syntax is ( condition ? expr1 : expr2 )

Example:

public class IfElseTernaryExample {


public static void main(String[] args) {
int number=13; //Using ternary
operator
String output=(number%2==0)?"even number":"odd number";
System.out.println(output);
}
}

JAVA IF-ELSE-IF LADDER STATEMENT

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

Consider the following example.

Student.java

1. public class Student {


2. public static void main(String[] args) {
3. String city = "Delhi";
4. if(city == "Meerut") {
5. System.out.println("city is meerut");
6. }else if (city == "Noida") {
7. System.out.println("city is noida");
8. }else if(city == "Agra") {
9. System.out.println("city is agra");
10. }else {
11. System.out.println(city);
12. }
13. }
14. }

Output:

Delhi
Program to check POSITIVE, NEGATIVE or ZERO:

public class PositiveNegativeExample {

public static void main(String[] args) {

int number=-13;
if(number>0){
System.out.println("POSITIVE");
}else if(number<0){
System.out.println("NEGATIVE");
}else{
System.out.println("ZERO");
}
}
}

Java Nested if statement


In nested if-statements, the if statement can contain a if or if-else statement inside another if or
else-if statement.

 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.

Syntax of Nested if-statement is given below.


1. if(condition 1) {
2. statement 1; //executes when condition 1 is true
3. if(condition 2) {
4. statement 2; //executes when condition 2 is true
5. }
6. else{
7. statement 2; //executes when condition 2 is false
8. }
9. }
OR Syntax:

if(condition){

//code to be executed

if(condition){
//code to be executed
}}

Student.java

1. public class Student {


2. public static void main(String[] args) {
3. String address = "Delhi, India";
4. if(address.endsWith("India")) {
5. if(address.contains("Meerut")) {
6. System.out.println("Your city is Meerut");
7. }else if(address.contains("Noida")) {
8. System.out.println("Your city is Noida");
9. }else {
10. System.out.println(address.split(",")[0]);
11. }
12. }else {
13. System.out.println("You are not living in India");
14. }
15. }
16. }

Output:

Delhi

//Java Program to demonstrate the use of Nested If


Statement.
public class JavaNestedIfExample {
public static void main(String[] args) {
//Creating two variables for age and weight
int age=20;
int weight=80;
//applying condition
on age and weight
if(age>=18){
if(weight>50){
System.out.println("You are eligible to donate blood");
}
}
}}

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

Java – Decision Making (Control Statements)


There are various types of if statement in Java.
 If Statement in Java - the Java if statement tests the condition. It executes
the if block if condition is true.
Syntax:

1. if(condition) {
2. //code to be executed
3. }

Flowchart

1.Example:

1. //Java Program to demonstrate the use of if statement.


2. public class If Example {
3. public static void main (String [] args) {
4. //defining an 'age' variable
5. int age=20;
6. //checking the age
7. if(age> 18) {
8. System. out. Print ("Age is greater than 18");
9. }}}

Output:

Age is greater than 18

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.

public class if1 {


public static void main (String [] args) {
int number = 50; //Declare a numeric value
if (number > 50){ //Check the value is more 50 or not
System.out.println("The number is less than or equal to 50");
}
String password = "1234"; //Declare a string value
If (password. length() < 6) { //Check the length of the string is less than 6 or not
System.out.print("Password cannot be less than 6 characters");
}}}

Example3:

1. public class if1 {


public static void main(String[] args) {
2. int num = 12;
3. if(num <100){
4.System.out.println(“ num is less than 100");
5.}}

Java Nested if statement


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.

Syntax:
1. if(condition) {
2. //code to be executed
3. if(condition) {
4. //code to be executed
5. }}
Flow Chart

Example1:

//Java Program to demonstrate the use of Nested If Statement.


1. public class JavaNestedIfExample {
2. public static void main (String [] args) {
3. //Creating two variables for age and weight
4. int age=20;
5. int weight=80;
6. if(age>=18) { //applying condition on age and weight
7. if(weight>50) {
8. System.out.println("You are eligible to donate blood");
9. }
10. }}}
Output:
You are eligible to donate blood

Example2:

1. //Java Program to demonstrate the use of Nested If Statement.


2. public class JavaNestedIfExample2 {
3. public static void main(String [] args) {
4. //Creating two variables for age and weight
5. int age=25;
6. int weight=48;
7. //applying condition on age and weight
8. if(age>=18) {
9. if(weight>50) {
10. System.out.println("You are eligible to donate blood");
11. } else {
12. System.out.println("You are not eligible to donate blood");
13. }
14. } else {
15. System.out.println("Age must be greater than 18");
16. }}}

Output: You are not eligible to donate blood

(B). JAVA If-else Statement


JAVA if-else statement is executed whether condition is true or false. 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.
 If-else Statement in Java - The Java if statement is used to test the condition. It checks
boolean condition: true or false.

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;

if(num%2==0){ //Check if the number is divisible by 2 or not


System.out.println("num is even number");
}else{
System.out.println("num is odd number");
}}

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

10. JAVA IF-ELSE STATEMENT WITH SCANNER

import java. util.Scanner;

public class{
public static void main(String [] args){

Scanner scan = new Scanner(Syste.in);

System.out.println("Enter your marks: ");

int marks = scan.nextInt();

if(marks>=80)&&(marks<=100)){ //these two conditions has to be true to run this code

System.out.println("Well done keep it up!");

else if(marks>=600)&&(marks<=70)){

System.out.println("Good keep it up!");

Using Ternary Operator


We can also use ternary operator (? :) to perform the task of if...else statement. It is a shorthand
way to check the condition. If the condition is true, the result of? is returned. But, if the condition
is false, the result of: is returned.

Example:

1. public class IfElseTernaryExample {


2. public static void main (String [] args) {
3. int number=13;
4. String output=(number%2==0)?"even number": "
odd number"; //Using ternary operator
5. System.out.println(output);
6. }}

Output:

Odd number

(C). The if...else if...else Statement


The if...else if...else statement executes different codes for more than two conditions. OR
The if else if.....else Statement or (Java if-else-if ladder Statement)
The if-else-if ladder statement executes one condition from multiple statements.

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

The if else if-else Statement

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:

Have a good day!


Example B:
Java programming
public class Time {
public static void main (String [] args) {

int date = 21;


int t = date;
if (t < 10) {
System.out.println("Have a good morning!");
} else if (t < 20) {
System.out.println("Have a good day!");
} else {
System.out.println("Have a good night!");
}}}
………………………………………………………………………………………………………

Leap Year Example 2:


A year is leap, if it is divisible by 4 and 400. But, not by 100.

1. public class LeapYearExample {


2. public static void main (String [] args) {
3. int year=2020;
4. If (((year % 4 ==0) && (year % 100 !=0)) || (year % 400==0)){
5. System.out.println("LEAP YEAR");
6. }
7. Else {
8. System.out.println("COMMON YEAR");
9. }}}
Output: LEAP YEAR

You might also like