If Else - More Examples

Fri 20 July 2018, in category Conditional

conditional, else, if

An if...else condition allows you to check for a given condition if its true, that statement is executed, otherwise the else statement is executed.
In the examples below, we're checking an integer variable i to see if it's less than or greater or equal to 10.

Java

if (i < 10) {
    System.out.println("i is Less than 10!");
}
else if (i >= 10 && i < 20){
    System.out.println("i is between 10 and 20!")
}
else {
    System.out.println("i is Greater Than or Equal to 20!");
}

C#

if (i < 10) {
    Console.WriteLine("i is Less than 10!");
}
else if (i >=10 && i < 20){
    Console.WriteLine("is is between 10 and 20!")
}
else {
    Console.WriteLine("i is Greater Than or Equal to 20!");
}

Python

if i < 10:
    print("i is Less than 10!")
elif i >= 10 and i < 20:
    print("i is between 10 and 20!")
else:
    print("i is Greater Than or Equal to 10!")

JavaScript

if (i < 10) {
    console.log("i is Less than 10!")
}
else if (i >= 10 && i < 20){
    console.log("i is between 10 and 20!")
}
else {
    console.log("i is Greater Than or Equal to 10!")
}

Go

if i < 10 {
    fmt.Println("i is Less than 10!")
} else if i >= 10 && i < 20 {
    fmt.Println("i is between 10 and 20!")
} else {
    fmt.Println("i is Greater Than or Equal to 10!")
}