[go: up one dir, main page]

0% found this document useful (0 votes)
3 views9 pages

if else

The document explains decision-making in Java using if-else statements, including their syntax and flow. It covers nested if statements and the if-else-if ladder for handling multiple conditions. Examples are provided to illustrate how these statements work in practice.

Uploaded by

sharmila.s
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)
3 views9 pages

if else

The document explains decision-making in Java using if-else statements, including their syntax and flow. It covers nested if statements and the if-else-if ladder for handling multiple conditions. Examples are provided to illustrate how these statements work in practice.

Uploaded by

sharmila.s
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/ 9

Java if-else

https://www.geeksforgeeks.org/loops-in-
java/?ref=lbp




Decision-making in Java helps to write decision-driven statements
and execute a particular set of code based on certain conditions.
The if statement alone tells us that if a condition is true it will
execute a block of statements and if the condition is false it
won’t. In this article, we will learn about Java if-else.
If-Else in Java
If- else together represents the set of Conditional statements in
Java that are executed according to the condition which is true.
Syntax of if-else Statement
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
Java if-else Flowchart
if-else Program in Java
Dry-Run of if-else statements
1. Program starts.
2. i is initialized to 20.
3. if-condition is checked. 20<15, yields false.
4. flow enters the else block.
4.a) "i is greater than 15" is printed
5. "Outside if-else block" is printed.
Below is the implementation of the above statements:
Java
// Java program to illustrate if-else statement

class IfElseDemo {
public static void main(String args[])
{
int i = 20;

if (i < 15)
System.out.println("i is smaller than 15");
else
System.out.println("i is greater than 15");

System.out.println("Outside if-else block");


}
}

Output
i is greater than 15
Outside if-else block

Nested if statement in Java


In Java, we can use nested if statements to create more complex
conditional logic. Nested if statements are if statements inside
other if statements.
Syntax:
if (condition1) {
// code block 1
if (condition2) {
// code block 2
}
}
Below is the implementation of Nested if statements:
Java
// Java Program to implementation
// of Nested if statements

// Driver Class
public class AgeWeightExample {
// main function
public static void main(String[] args) {
int age = 25;
double weight = 65.5;

if (age >= 18) {


if (weight >= 50.0) {
System.out.println("You are eligible to donate
blood.");
} else {
System.out.println("You must weigh at least 50
kilograms to donate blood.");
}
} else {
System.out.println("You must be at least 18 years old to
donate blood.");
}
}
}

Output
You are eligible to donate blood.
Note: The first print statement is in a block of “if” so the second
statement is not in the block of “if”. The third print statement is in
else but that else doesn’t have any corresponding “if”. That
means an “else” statement cannot exist without an “if”
statement.

Java if-else-if ladder with Examples





Decision Making in Java helps to write decision-driven
statements and execute a particular set of code based on certain
conditions.
Java if-else-if ladder is used to decide among multiple options.
The if statements are executed from the top down. As soon as
one of the conditions controlling the if is true, the statement
associated with that if is executed, and the rest of the ladder is
bypassed. If none of the conditions is true, then the final else
statement will be executed.
Syntax:
if (condition)
statement 1;
else if (condition)
statement 2;
.
.
else
statement;
Working of the if-else-if ladder:
1. Control falls into the if block.
2. The flow jumps to Condition 1.
3. Condition is tested.
1. If Condition yields true, goto Step 4.
2. If Condition yields false, goto Step 5.
4. The present block is executed. Goto Step 7.
5. The flow jumps to Condition 2.
1. If Condition yields true, goto step 4.
2. If Condition yields false, goto Step 6.
6. The flow jumps to Condition 3.
1. If Condition yields true, goto step 4.
2. If Condition yields false, execute else block.
Goto Step 7.
7. Exit the if-else-if ladder.
Flowchart if-else-if ladder:
Example 1:

Java
// Java program to illustrate if-else-if ladder
import java.io.*;

class GFG {
public static void main(String[] args)
{
// initializing expression
int i = 20;

// condition 1
if (i == 10)
System.out.println("i is 10\n");

// condition 2
else if (i == 15)
System.out.println("i is 15\n");

// condition 3
else if (i == 20)
System.out.println("i is 20\n");

else
System.out.println("i is not present\n");

System.out.println("Outside if-else-if");
}
}

Output:
i is 20

Outside if-else-if

Time Complexity: O(1)


Auxiliary Space: O(1)
Dry running
Example 1:
1. Program starts.
2. i is initialized to 20.
3. condition 1 is checked. 20 == 10, yields false.
4. condition 2 is checked. 20 == 15, yields false.
5. condition 3 is checked. 20 == 20, yields true.
5.a) "i is 20" gets printed.
6. "Outside if-else-if" gets printed.
7. Program ends.
Example 2:
Java
// Java program to illustrate if-else-if ladder

import java.io.*;

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

// initializing expression
int i = 20;

// condition 1
if (i < 10)
System.out.println("i is less than 10\n");

// condition 2
else if (i < 15)
System.out.println("i is less than 15\n");

// condition 3
else if (i < 20)
System.out.println("i is less than 20\n");

else
System.out.println("i is greater than "
+ "or equal to 20\n");

System.out.println("Outside if-else-if");
}
}

Output:
i is greater than or equal to 20

Outside if-else-if

Time Complexity: O(1)


Auxiliary Space: O(1)

You might also like