What if we want to execute a different set of operations in case an if condition turns out to be False?
That is where the if-else statement comes into the picture.
Structure
The if-else statement looks something like this:
svg viewer
There’s nothing too tricky going on here. If the condition turns out to be False, the code after the else: keyword is executed.
Hence, we can now perform two different actions based on the condition’s value.
The else keyword will be on the same indentation level as the if keyword. Its body will be indented one tab to the right just like the if statement.
Here’s the if-else statement in action:
xxxxxxxxxx
num = 60
if num <= 50:
print("The number is less than or equal to 50")
else:
print("The number is greater than 50")
xxxxxxxxxx
var a = prompt("value 1")
var b = prompt("value 2")
var c = prompt("value 3")
var d = prompt("value 4")
var e = prompt("value 5")
if (a >= b && a >= c && a >= d && a >= e) {
console.log(a + " this is greater value")
}
else if (b >= a && b >= c && b >= d && b >= e) {
console.log(b + " this is greater value")
}
else if (c >= a && c >= b && c >= d && c >= e) {
console.log(c + " this is greater value")
}
else if (d >= a && d >= b && d >= c && d >= e ) {
console.log(d + " this is greater value")
}
else if(e >= a && e >= b && e >= c && e >= d){
console.log(e + " this is greater value")
}
else{"enter the valid number"}
xxxxxxxxxx
int age = 30;
if(age >= 30 && age <= 80){
System.out.println("Old");
}else if(age < 30 && >= 1){
System.out.println("Young");
}else{
System.out.println("Not Supported");
}
xxxxxxxxxx
# python program to illustrate If else statement
#!/usr/bin/python
i = 20
if (i < 15):
print("i is smaller than 15")
print("i'm in if Block")
else:
print("i is greater than 15")
print("i'm in else Block")
print("i'm not in if and not in else Block")
xxxxxxxxxx
var age=20;
if (age < 18) {
console.log("underage");
} else {
console.log("let em in!");
}
xxxxxxxxxx
public class IfElseIfExample {
public static void main(String args[]){
int num=1234;
if(num <100 && num>=1) {
System.out.println("Its a two digit number");
}
else if(num <1000 && num>=100) {
System.out.println("Its a three digit number");
}
else if(num <10000 && num>=1000) {
System.out.println("Its a four digit number");
}
else if(num <100000 && num>=10000) {
System.out.println("Its a five digit number");
}
else {
System.out.println("number is not between 1 & 99999");
}
}
}
xxxxxxxxxx
if (condition1) {
// Code to be executed if condition1 is true
} else if (condition2) {
// Code to be executed if condition1 is false and condition2 is true
} else {
// Code to be executed if neither condition1 nor condition2 is true
}
xxxxxxxxxx
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
xxxxxxxxxx
>> number=23
>> guess = input('Enter a number : ')
>> if guess == number:
>> print('Congratulations! You guessed it.')
>> elif guess < number:
**( It is giving me 'Invalid Syntax')**
>> else:
**( It is also giving me 'Invalid syntax')**