IF control Statement

Understanding the "if" Control Statement in Java

Control statements are an essential part of programming languages and play a crucial role in determining the flow of execution in a program. One commonly used control statement in Java is the "if" statement. The "if" statement allows programmers to specify a condition that, if true, will execute a block of code.

Basic Syntax of the "if" Statement

The basic syntax of the "if" statement in Java is as follows:


if (condition) { // code block to be executed if the condition is true
}

The "condition" in the above syntax is a Boolean expression that evaluates to either true or false. For example, consider the following code snippet:

if (x > 5) {
// code to be executed if x is greater than 5
// code to be executed if the condition is true
}

Example Program to demonstrate if control statement

Program to find given number is even or odd using if control statement


Output 1


Output 2




Different Uses of "if" Control Statement

The "if" control statement in Java can be used in various ways to make decisions and control the flow of execution within a program. Simple "if" statement**: The most basic use of the "if" statement is to execute a block of code if a certain condition is

Nested "if" Statements in Java

The "if" statement in Java allows for nested expressions, which means that you can have multiple levels of conditions within each other.

Program to to Demonstrate newsted if


Output



"If" Statement with Multiple Conditions

The "if" statement in Java also supports multiple conditions using logical operators such as "&&" and "||". The "if" control statement in Java allows for multiple conditions to be evaluated and executed based on their truth values. To express unstructured control flow and accommodate for the arbitrary entering of a method at any point, Java makes use of the yield statement. The yield statement in Java introduces a form of unstructured control flow, allowing the method to be entered at any arbitrary point that it is used.

Example Program to demonstrate if control statement with multiple conditions


Output