Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
38 views

Java Control Statements

Java provides control flow statements called decision-making statements that control program flow based on boolean conditions. The two main decision-making statements are if statements and switch statements. If statements evaluate a boolean expression and allow code execution if the expression is true, including simple if statements, if-else statements, and if-else-if ladders. Nested if statements allow if statements within other if statements.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Java Control Statements

Java provides control flow statements called decision-making statements that control program flow based on boolean conditions. The two main decision-making statements are if statements and switch statements. If statements evaluate a boolean expression and allow code execution if the expression is true, including simple if statements, if-else statements, and if-else-if ladders. Nested if statements allow if statements within other if statements.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Java Control Statements

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.

Let’s start with Decision Making statements.

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.

If statements:
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

1. Simple if statement:
It evaluates a Boolean expression and enables the program to enter a block of
code if the expression evaluates to true.
Syntax:
if (condition) {
// block of code to be executed if the condition is true
}
Example: 1
if (20 > 18) {
System.out.println("20 is greater than 18");
}

Example: 2
int x = 20;
int y = 18;
if (x > y) {
System.out.println("x is greater than y");
}

2. If Else statements
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:
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}

Example: 1
int time = 20;
if (time < 18) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
Example:2 Check whether a number is even or odd using if...else statement.
import java.util.Scanner;

public class EvenOdd {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");


int num = sc.nextInt();

if(num % 2 == 0)
System.out.println(num + " is even");
else
System.out.println(num + " is odd");
}
}

Example:3 Check whether an alphabet is vowel or consonant using if..else


statement.
public class VowelConsonant {
public static void main(String[] args) {
char ch = 'i';
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch ==
'u' )
System.out.println(ch + " is vowel");
else
System.out.println(ch + " is consonant");
}
}

# Ternary Operator:
The ternary operator (? :) consists of three operands. It is used to evaluate
Boolean expressions. The operator decides which value will be assigned to the
variable. It is the only conditional operator that accepts three operands. It can be
used instead of the if-else statement.
Example:4 Check whether a number is even or odd using ternary operator.
import java.util.Scanner;
public class EvenOdd {

public static void main(String[] args) {


Scanner reader = new Scanner(System.in);

System.out.print("Enter a number: ");


int num = reader.nextInt();

String evenOdd = (num % 2 == 0) ? "even" : "odd";


System.out.println(num + " is " + evenOdd);

}
}

3. If else if ladder
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.

Syntax:
x:
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
Example: 1
public class CheckCity {
public static void main(String[] args) {
String city = "Delhi";

if (city == "Meerut") {
System.out.println("City is Meerut");
} else if (city == "Noida") {
System.out.println("City is Noida");
} else if (city == "Agra") {
System.out.println("City is Agra");
} else {
System.out.println(city);
}
}
}

Example:2 Check if a Number is Positive or Negative using if else…if.


public class PositiveNegative {
public static void main(String[] args) {
double number = 12.3;

// true if number is less than 0


if (number < 0.0)
System.out.println(number + " is a negative number.");

// true if number is greater than 0


else if ( number > 0.0)
System.out.println(number + " is a positive number.");

// if both test expression is evaluated to false


else
System.out.println(number + " is 0.");
}
}
Example:3 Find Largest Among three numbers using if else..if statement.
public class Largest {
public static void main(String[] args) {

int n1 = 5, n2 = 9, n3 = 2;
// Compare n1 with n2 & n3
if( n1 >= n2 && n1 >= n3)
System.out.println(n1 + " is the largest number.");
// Compare n2 with n1 & n3
else if (n2 >= n1 && n2 >= n3)
System.out.println(n2 + " is the largest number.");
// If none of the above is true, then clearly n3 is the largest.
else
System.out.println(n3 + " is the largest number.");
}
}

Exercise

# Question: Write a Java program to find whether a given letter is an alphabet or


not.

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

char c = '*';

if( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
System.out.println(c + " is an alphabet.");
else
System.out.println(c + " is not an alphabet.");
}
}
4. 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.

Syntax:
if (condition1) {
statement1; // Executes when condition1 is true

if (condition2) {
statement2; // Executes when condition2 is true
} else {
statement2; // Executes when condition2 is false
}
}

Example:1
public class Student {
public static void main(String[] args) {
String address = "Delhi, India";

if (address.endsWith("India")) {
if (address.contains("Meerut")) {
System.out.println("Your city is Meerut");
} else if (address.contains("Noida")) {
System.out.println("Your city is Noida");
} else {
System.out.println(address.split(",")[0]);
}
} else {
System.out.println("You are not living in India");
}
}
}

You might also like