Java Control Statement
The Java compiler executes code line by line, but control statements can change the flow of execution. They help break the sequential flow, jump to another part of the program, or change the direction of execution during program execution based on the condition.
It controls the direction of program execution by evaluating conditions or Control Statement.
There are different types of statement:
1. Decision-making statements
It helps a program choose between different options according to given conditions.
- if
- if-else
- else-if
- nested-if
- switch
1. if statement:
It needs a condition or boolean expression which returns boolean value(true or false). and It executes the block of code only if the condition or expression returns true value.
Syntax:
if (condition/expression) {
// This block of code to executes only if the condition is true
}Example-1:
Using boolean expression
public class HelloWorld {
public static void main(String[] args) {
int age = 18;
if (age >= 18) //(age >= 18) is an expression which returns a boolean value (true/false).
{
//This block of code will execute only if the expression returns true value.
System.out.println("Congratulation Ashish.....");
System.out.println("You are eligible to vote");
}
}
}Example-2:
Using boolean variable.
public class HelloWorld {
public static void main(String[] args) {
boolean isLoggedIn = true;
if (isLoggedIn) { //(isLoggedIn) - is a boolean variable.
System.out.println("Your is logged in");
}
}
}Example-3:
If there is only one line of statemnet the we can use if statement with out curly braces { } .
public class HelloWorld {
public static void main(String[] args) {
boolean isLoggedIn = true;
if (isLoggedIn)
System.out.println("Your is logged in");
}
}2. if-else statement:
It needs a condition or expression which returns boolean value(true or false). It contains two blocks: one executes if the expression evaluates to true; otherwise, the other block executes.
Syntax:
if (condition/expression) {
//This block executes if the condition is true
} else {
// This block executes if the condition is false
}Example:
public class HelloWorld {
public static void main(String[] args) {
int num = 9;
if (num % 2 == 0) { //It returns false
System.out.println("Even number");
} else {
System.out.println("Odd number"); // This block will execute
}
}
}3. else if statement:
In Java, an else if statement is used to check multiple conditions one after another.
Syatax:
if (condition-1/expression-1) {
//This block of code executes only if condition-1 is true
} else if (condition-2/expression-2) {
//This block of code executes if condition-1 is false and condition-2 is true
} else {
//This block of code executes if both conditions are false
}Example:
public class HelloWorld {
public static void main(String[] args) {
int marks = 82;
if (marks >= 90) {
System.out.println("Grade A");
} else if (marks >= 75) {
System.out.println("Grade B");
} else if (marks >= 50) {
System.out.println("Grade C");
} else {
System.out.println("Fail");
}
}
}4. Nested if:
Nested if means, We can keep one if statement inside another if statement.
Syntax:
if (condition-1/expression-1) {
//This block will execute if condition-1 is true
if (condition-2/expression-2) {
//This block will execute if both condition-11 and condition-2 are true
}
}Example:
public class HelloWorld {
public static void main(String[] args) {
int age = 20;
int weight = 55;
if (age >= 18) {
if (weight >= 50) {
System.out.println("Eligible for the job");
}
}
}
}5. switch statement:
When there are multiple if-else conditions, we can use a switch statement instead. It compares a single value or expression against multiple conditions or cases. It means the execution flow directly jumps to the matching condition or case.
Syntax:
switch(expression/variable) {
case x:
// code block executed if expression/variable == x
break;
case y:
// code block executed if expression/variable == y
break;
default:
// code block executed if none of the cases match
}How it works:
The switch expression is evaluated, and its result is compared with each case value. If a match is found, the corresponding block of code is executed.
The break statement ends the switch after the matched case is executed.
The default block runs if none of the cases match the expression.
Example:
public class HelloWorld {
public static void main(String[] args) {
int colorCode = 2;
switch (colorCode) {
case 1:
System.out.println("Red");
break;
case 2:
System.out.println("Green");
break;
case 3:
System.out.println("Blue");
break;
default:
System.out.println("Unknown color");
}
}
}Boolean Expression:
A boolean expression is any condition or expression that returns a Boolean value (true or false).
Example:
int a = 10;
a > 5 // true
a == 10 // true
a < 5 // falseWe can combine boolean expression using operators - &&(AND), ||(OR), !(NOT):
Example:
public class HelloWorld {
public static void main(String[] args) {
int age = 20;
boolean hasID = true;
boolean isStudent = false;
// Using && (AND) - here two conditions combined with AND operator.
if (age >= 18 && hasID) {
System.out.println("Entry allowed with ID");
}
// Using || (OR) - here two conditions combined with OR operator.
if (isStudent || age < 25) {
System.out.println("Eligible for discount");
}
// Using ! (NOT) - here NOT operator negates the boolean value.
if (!isStudent) {
System.out.println("Not a student");
}
}
}See you in next page....🙂