IF,else and If else program
01.What is if condition?
Only if the condition is true.
if conditional syntax:
if()
{
}
02.What is else statement?
Used with if to run an alternative block if the condition is false.
else conditional syntax:
else
{
}
03.What is if else condition?
if...else is the full structure that checks a condition and handles both true and false cases.
You can think of if...else as a decision-making tool:
if: do something if true
else: do something else if false.
if else syntax:
if else()
{
}
04.What is else if condition?
Checks additional conditions.
Example:
public class ATM Withdrawal {
public static void main(String[] args) {
int balance = 10000;
int amount = 5000;
// If statement
if(amount <= 0)//false
{
System.out.println("Invalid amount, Please enter an amount grater than zero");
}
//else if statement
else if(amount > balance)//false
{
System.out.println("Insufficient balance");
}
// else statement
else {
System.out.println("Collect your cash " + amount);
}
}
amount == statement (True or false statement)
int amount = 20000;
if(amount != 30000)
{
System.out.println("True"+ amount);
}
else
{
System.out.println("Not Equal");
}
if condition (True)statement
if(true)
{
System.out.println("Collect your cash" + amount);
}
else
{
System.out.println("Fail");//Dead code
}
if condition (False)statement
if(false)
{
System.out.println("Collect your cash" + amount);//Dead code
}
else
{
System.out.println("Fail");
}
Top comments (0)