[Go to site: main page, start]

0% found this document useful (0 votes)
37 views11 pages

Java Conditional Statements Explained

Uploaded by

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

Java Conditional Statements Explained

Uploaded by

ayush008.aiml
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Programming

Connect4techs

Conditional Statement

➢ Conditional Statement is also Known as Decision-Making


Statement .
➢ Conditional Statement decides which statement to execute and
when.
➢ Java provides the facility to make decisions using selection
statements.
➢ They evaluate the decision based on provided c onditions and
proceed with the flow of the program in a certain direction.

Java provides two types of conditional statements :

if Statement

switch Statement

➔ Java-if Statement :

The java if-Statement is used to test the [Link] check boolean condition:
true or false.

There are varios types of if -Statement in java.

If statement
If-else statement
If-else-if ladder
Nested if statement
Java Programming

1. Java if Statement

The syntax of an if-then statement is:

if (condition) {
// statements
}

Here, condition is a boolean expression such as age >= 18 .

• if condition evaluates to true , statements are executed

• if condition evaluates to false , statements are skipped

Working of if Statement

Working of Java if statement


Java Programming

Example 1: Java if Statement


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

int number = 10;

// checks if number is less than 0


if (number < 0) {
[Link]("The number is negative.");
}

[Link]("Statement outside if block");


}
}

Output

Statement outside if block

In the program, number < 0 is false . Hence, the code inside the body of

the if statement is skipped.

2. Java if...else (if-then-else) Statement


The if statement executes a certain section of code if the test expression is

evaluated to true . However, if the test expression is evaluated to false , it

does nothing.

In this case, we can use an optional else block. Statements inside the body

of else block are executed if the test expression is evaluated to false . This is

known as the if-...else statement in Java.

The syntax of the if...else statement is:

if (condition) {
// codes in if block
}
Java Programming

else {
// codes in else block
}

Here, the program will do one task (codes inside if block) if the condition

is true and another task (codes inside else block) if the condition is false .

Working of if...else statement

Example 2: Java if...else Statement


class Main {
public static void main(String[] args) { int number = 10;

// checks if number is greater than 0


if (number > 0) {
[Link]("The number is positive.");
}

// execute this block


// if number is not greater than 0 else {
[Link]("The number is not positive.");
}

[Link]("Statement outside if...else block");


Java Programming

}
}

Output

The number is positive.


Statement outside if...else block

In the above example, we have a variable named number. Here, the test

expression number > 0 checks if number is greater than 0.

Since the value of the number is 10 , the test expression evaluates to true .

Hence code inside the body of if is executed.

Now, change the value of the number to a negative integer. Let's say -5 .

int number = -5;

If we run the program with the new value of number, the output will be:

The number is not positive.


Statement outside if...else block

Here, the value of number is -5 . So the test expression evaluates to false .

Hence code inside the body of else is executed.

3. Java if...else...if Statement

In Java, we have an if...else...if ladder, that can be used to execute one


block of code among multiple other blocks.
Java Programming

if (condition1) {
// codes
}
else if(condition2) {
// codes
}
else if (condition3) {
// codes
}
.
.
else {
// codes
}

Here, if statements are executed from the top towards the bottom. When the

test condition is true , codes inside the body of that if block is executed. And,

program control jumps outside the if...else...if ladder.

If all test expressions are false , codes inside the body of else are executed.

Working of if...else...if ladder


Java Programming

Example 3: Java if...else...if Statement

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

int number = 0;

// checks if number is greater than 0


if (number > 0) {
[Link]("The number is positive.");
}

// checks if number is less than 0


else if (number < 0) {
[Link]("The number is negative.");
}

// if both condition is false


else {
[Link]("The number is 0.");
}
}
}
Output

The number is 0.

In the above example, we are checking whether number is

positive, negative, or zero. Here, we have two


condition expressions:

• number > 0 - checks if number is greater than 0

• number < 0 - checks if number is less than 0

Here, the value of number is 0 . So both the conditions evaluate to false .

Hence the statement inside the body of else is executed.


Java Programming

4. Java Nested if..else Statement


In Java, it is also possible to use if..else statements inside an

if...else statement. It's called the nested if...else statement.

Here's a program to find the largest of 3 numbers using the

nested if...else statement.

Example 4: Nested if...else Statement

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

// declaring double type variables


Double n1 = -1.0, n2 = 4.5, n3 = -5.3, largest;

// checks if n1 is greater than or equal to n2 if (n1 >= n2) {

// if...else statement inside the if block // checks if n1 is greater than or equal to n3 if (n1 >= n3) {
largest = n1;
}

else {
largest = n3;
}
} else {

// if..else statement inside else block


// checks if n2 is greater than or equal to n3 if (n2 >= n3) { largest = n2;
}

else {
largest = n3;
}
}

[Link]("Largest Number: " + largest);


}
}
Java Programming

Output:

Largest Number: 4.5

In the above programs, we have assigned the value of variables ourselves to


make this easier.

➔ Switch Statement :

The Switch statement allows us to execute a block of code among many


alternatives.

The syntax of the switch statement in Java is:

switch (expression) {

case value1:
// code
break;

case value2:
// code
break;

...
...

default:
// default statements
}
Java Programming

Working of switch case statement


The expression is evaluated once and compared with the values of each case.

• If expression matches with value1 , the code of case value1 are

executed. Similarly, the code of case value2 is executed if

expression matches with value2 .

• If there is no match, the code of the default case is executed.

Example: Java switch Statement


// Java Program to check the size
// using the switch...case statement

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

int number = 44;


String size;

// switch statement to check size switch (number) {

case 29: size = "Small";


break;

case 42: size = "Medium";


break;

// match the value of week case 44: size = "Large";


break;

case 48: size = "Extra Large";


break;

default:
size = "Unknown";
break;

}
[Link]("Size: " + size);
}
Java Programming

Output:

Size: Large

In the above example, we have used the switch statement to find the size.

Here, we have a variable number. The variable is compared with the value of

each case statement.

Since the value matches with 44, the code of case 44 is executed.

size = "Large"; break;

Here, the size variable is assigned with the value Large .

Common questions

Powered by AI

The Java if...else statement executes code based on boolean conditions, where it checks if a particular condition is true, executing the corresponding block, or moves to an else block when false. It is flexible and can handle complex comparisons involving boolean expressions . In contrast, the switch statement evaluates a single expression and compares it against multiple possible values, executing the block corresponding to the matching case. Switch is better suited for scenarios with discrete, known values and less effective for complex condition evaluations .

The Java if statement evaluates a boolean condition present in the 'if (condition)' clause. If the condition is true, the statements inside the if block are executed. If the condition evaluates to false, these statements are skipped, and the program continues with the next block of code outside the if statement, or no action is taken if there is no else block .

The if...else...if ladder in Java checks multiple conditions in sequence from top to bottom. It executes the block associated with the first true condition and skips the rest. If none of the conditions are true, the else block is executed if present. This construct is suitable for scenarios where multiple conditions need sequential evaluation, offering clarity over complex nested if statements .

The else block in a Java if...else statement provides an alternative action if the initial condition evaluates to false. If omitted, the program simply proceeds to the next statement after the if block if the condition is false, without performing any specific operation for that false condition .

If a break statement is omitted in a Java switch statement, execution falls through to subsequent cases regardless of their conditions until a break is encountered or the switch statement ends. This results in executing multiple case blocks consecutively, which may lead to unintended behavior unless explicitly desired for combining case outcomes .

A simple if statement evaluates a single condition to decide whether to execute a block of code, ideal for straight, uncomplicated scenarios. In contrast, a nested if statement involves placing an if or else statement within another if or else block, facilitating additional conditional checks within an existing decision path, suitable for complex situations requiring layered decision-making .

The code sequentially checks if the 'number' variable is greater than zero. Since 0 is not greater than zero, it evaluates the next condition to see if 'number' is less than zero, which is also false. Finally, it reaches the else block, which executes because all prior conditions are false, printing 'Zero' .

Using nested if...else statements to find the largest of three numbers involves first comparing the first two numbers to establish a temporary largest, then further comparisons to determine if the third number surpasses this temporary largest. Through conditional checks, updating the largest variable only if the current number exceeds the current largest, ensuring accurate identification of the largest number among all .

A nested if statement in Java is an if statement placed inside another if or else block, allowing further conditional checks within a decision path. Unlike a regular if statement that evaluates a single condition, nested if statements support hierarchical or multi-level decision-making, where subsequent conditions are only evaluated if preceding conditions are true, enabling complex logic flows .

A switch statement is less effective for range-based conditions as it is designed to match exact values, not ranges. It provides clear, straightforward code structures for discrete case matching but lacks flexibility for the conditional logic required by range checks. A series of if...else statements, however, excels in handling ranges due to its ability to evaluate boolean expressions, offering greater flexibility and readability for such conditions .

You might also like